Skip to content

Commit

Permalink
BehaviorLocator looks in dev packages too
Browse files Browse the repository at this point in the history
  • Loading branch information
mringler committed Mar 4, 2021
1 parent 394a6ce commit 143b262
Show file tree
Hide file tree
Showing 4 changed files with 588 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/Propel/Generator/Util/BehaviorLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function getBehaviors()
if ($lock === null) {
$this->behaviors = [];
} else {
$this->behaviors = $this->loadBehaviors($lock);
$this->behaviors = $this->loadBehaviorsFromLockFile($lock);
}

// find behavior in composer.json (useful when developing a behavior)
Expand Down Expand Up @@ -198,20 +198,23 @@ private function getCoreBehavior($name)
}

/**
* Finds all behaviors by parsing composer.lock file
* Finds all behaviors in composer.lock file
*
* @param \Symfony\Component\Finder\SplFileInfo $composerLock
*
* @return array
*/
private function loadBehaviors($composerLock)
private function loadBehaviorsFromLockFile(SplFileInfo $composerLock): array
{
$behaviors = [];

$json = json_decode($composerLock->getContents(), true);

if (isset($json['packages'])) {
foreach ($json['packages'] as $package) {
foreach (['packages', 'packages-dev'] as $packageSectionName) {
if (!isset($json[$packageSectionName])) {
continue;
}
foreach ($json[$packageSectionName] as $package) {
$behavior = $this->loadBehavior($package);

if ($behavior !== null) {
Expand Down
9 changes: 8 additions & 1 deletion tests/Fixtures/behavior-installer/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 35 additions & 15 deletions tests/Propel/Tests/Generator/Behavior/BehaviorLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,26 @@ protected function setUp(): void
*/
public function testBehaviorLocatorWithComposerLock()
{
$configOptions['propel']['paths']['composerDir'] = __DIR__ . '/../../../../Fixtures/behavior-installer';
$config = new QuickGeneratorConfig($configOptions);
$locator = new BehaviorLocator($config);

// test found behaviors
$locator = $this->getLocatorForLocation(__DIR__ . '/../../../../Fixtures/behavior-installer');
$behaviors = $locator->getBehaviors();
$this->assertSame(1, count($behaviors));

$this->assertTrue(array_key_exists('l10n', $behaviors));
$this->assertSame('gossi/propel-l10n-behavior', $behaviors['l10n']['package']);
$this->assertCount(2, $behaviors);

// test class name
$this->assertSame('\\gossi\\propel\\behavior\\l10n\\L10nBehavior', $locator->getBehavior('l10n'));
$this->assertArrayHasKey('l10n', $behaviors);
$this->assertEquals('gossi/propel-l10n-behavior', $behaviors['l10n']['package']);
$this->assertEquals('\\gossi\\propel\\behavior\\l10n\\L10nBehavior', $locator->getBehavior('l10n'));

$this->assertArrayHasKey('le-dev-behavior', $behaviors);
}

/**
* @return void
*/
public function testBehaviorLocatorWithComposerJson()
{
$configOptions['propel']['paths']['composerDir'] = __DIR__ . '/../../../../Fixtures/behavior-development';
$config = new QuickGeneratorConfig($configOptions);
$locator = new BehaviorLocator($config);

// test found behaviors
$locator = $this->getLocatorForLocation(__DIR__ . '/../../../../Fixtures/behavior-development');
$behaviors = $locator->getBehaviors();

$this->assertSame(1, count($behaviors));

$this->assertTrue(array_key_exists('collection', $behaviors));
Expand All @@ -68,4 +62,30 @@ public function testBehaviorLocatorWithComposerJson()
// test class name
$this->assertSame('\\Propel\\Behavior\\Collection\\CollectionBehavior', $locator->getBehavior('collection'));
}

/**
* @return void
*/
public function testBehaviorLocatorSkipsMissingSectionsInLockfile()
{
$locator = $this->getLocatorForLocation(__DIR__ . '/../../Resources/dummy_composer_lock_without_packages-dev');
$behaviors = $locator->getBehaviors();

$this->assertIsArray($behaviors);
$this->assertCount(1, $behaviors);
$this->assertArrayHasKey('l10n', $behaviors);
}

/**
* @param string $composerDirPath
*
* @return \Propel\Generator\Util\BehaviorLocator behaviors
*/
private function getLocatorForLocation(string $composerDirPath)
{
$configOptions['propel']['paths']['composerDir'] = $composerDirPath;
$config = new QuickGeneratorConfig($configOptions);

return new BehaviorLocator($config);
}
}
Loading

0 comments on commit 143b262

Please sign in to comment.