Skip to content

Commit

Permalink
Fixed multiple implementations use-case
Browse files Browse the repository at this point in the history
Added new assertion method for instances returned in unit tests
  • Loading branch information
AshleyDawson committed Apr 30, 2015
1 parent 7e46215 commit 0db7d58
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
8 changes: 4 additions & 4 deletions lib/AbstractMultiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
abstract class AbstractMultiBundle extends Bundle
{
/**
* @var bool
* @var bool[]
*/
private static $_hasRegisteredSelf = false;
private static $_hasRegisteredSelfFlags = array();

/**
* Register this bundle collection into the Symfony kernel, e.g.
Expand Down Expand Up @@ -74,9 +74,9 @@ public static function registerInto(array &$bundles, $env = null)
}

// Register myself
if ( ! self::$_hasRegisteredSelf) {
if ( ! isset(self::$_hasRegisteredSelfFlags[$calledClass])) {
$dependencies[] = new $calledClass();
self::$_hasRegisteredSelf = true;
self::$_hasRegisteredSelfFlags[$calledClass] = true;
}

// Remove duplicates
Expand Down
87 changes: 71 additions & 16 deletions tests/AbstractMultiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ protected static function getBundles()
}
}

/**
* Class DummyParentDependantsAltBundle
*
* @package AshleyDawson\MultiBundle\Tests
*/
class DummyParentDependantsAltBundle extends AbstractMultiBundle
{
protected function __construct()
{
}

/**
* @return \Symfony\Component\HttpKernel\Bundle\BundleInterface[]
*/
protected static function getBundles()
{
return array(
new \AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne(),
new \AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo(),
new \AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree(),
new \AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleFour(),
);
}
}

/**
* Class DummyParentGroupedDependantsBundle
*
Expand Down Expand Up @@ -103,7 +128,7 @@ public function testRegisterIntoNoDependantsNoPrevious()

$this->assertCount(1, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\DummyParentNoDependantsBundle', $bundles[0]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentNoDependantsBundle', $bundles);
}

public function testRegisterIntoNoDependantsPrevious()
Expand All @@ -117,9 +142,9 @@ public function testRegisterIntoNoDependantsPrevious()

$this->assertCount(3, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles[0]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles[1]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\DummyParentNoDependantsBundle', $bundles[2]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentNoDependantsBundle', $bundles);
}

public function testRegisterIntoDependantsNoPrevious()
Expand All @@ -130,10 +155,10 @@ public function testRegisterIntoDependantsNoPrevious()

$this->assertCount(4, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles[0]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles[1]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles[2]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\DummyParentDependantsBundle', $bundles[3]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentDependantsBundle', $bundles);
}

public function testRegisterIntoDependantsPrevious()
Expand All @@ -147,10 +172,10 @@ public function testRegisterIntoDependantsPrevious()

$this->assertCount(4, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles[0]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles[1]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles[2]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\DummyParentDependantsBundle', $bundles[3]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentDependantsBundle', $bundles);
}

public function testRegisterIntoGroupedDependantsNoPrevious()
Expand All @@ -161,17 +186,17 @@ public function testRegisterIntoGroupedDependantsNoPrevious()

$this->assertCount(3, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles[0]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles[1]);
$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\DummyParentGroupedDependantsBundle', $bundles[2]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentGroupedDependantsBundle', $bundles);

$bundles = array();

DummyParentGroupedDependantsBundle::registerInto($bundles, 'dev');

$this->assertCount(1, $bundles);

$this->assertInstanceOf('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles[0]);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles);
}

public function testEnvironmentNotFound()
Expand All @@ -191,4 +216,34 @@ public function testInvalidGetBundlesReturnType()

DummyInvalidReturnTypeBundle::registerInto($bundles);
}

public function testMoreThanOneMultiBundleRegistrationNoPrevious()
{
$bundles = array();

DummyParentDependantsBundle::registerInto($bundles);

DummyParentDependantsAltBundle::registerInto($bundles);

$this->assertCount(6, $bundles);

$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleOne', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleTwo', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleThree', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentDependantsBundle', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\Fixture\DummyBundleFour', $bundles);
$this->_assertInstanceInArray('AshleyDawson\MultiBundle\Tests\DummyParentDependantsAltBundle', $bundles);
}

private function _assertInstanceInArray($expected, array $haystack)
{
foreach ($haystack as $item) {
if ($expected == get_class($item)) {
$this->assertTrue(true, sprintf('Instance "%s" found in array', $expected));
return;
}
}

$this->assertTrue(false, sprintf('Instance "%s" not found in array', $expected));
}
}
15 changes: 15 additions & 0 deletions tests/Fixture/DummyBundleFour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AshleyDawson\MultiBundle\Tests\Fixture;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class DummyBundleFour
*
* @package AshleyDawson\MultiBundle\Tests\Fixture
*/
class DummyBundleFour extends Bundle
{

}

0 comments on commit 0db7d58

Please sign in to comment.