-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from adamlundrigan/feature/service-locator-aware
Make fixture loader service locator aware
- Loading branch information
Showing
6 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/DoctrineDataFixtureModule/Loader/ServiceLocatorAwareLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the LGPL. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
namespace DoctrineDataFixtureModule\Loader; | ||
|
||
use Doctrine\Common\DataFixtures\Loader as BaseLoader; | ||
use Zend\ServiceManager\ServiceLocatorAwareInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
|
||
/** | ||
* Doctrine fixture loader which is ZF2 Service Locator-aware | ||
* Will inject the service locator instance into all SL-aware fixtures on add | ||
* | ||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
* @link www.doctrine-project.org | ||
* @author Adam Lundrigan <adam@lundrigan.ca> | ||
*/ | ||
class ServiceLocatorAwareLoader extends BaseLoader | ||
{ | ||
/** | ||
* @var ServiceLocatorInterface | ||
*/ | ||
protected $serviceLocator; | ||
|
||
public function __construct(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$this->serviceLocator = $serviceLocator; | ||
} | ||
|
||
/** | ||
* Add a fixture object instance to the loader. | ||
* | ||
* @param FixtureInterface $fixture | ||
*/ | ||
public function addFixture(FixtureInterface $fixture) | ||
{ | ||
if ($fixture instanceof ServiceLocatorAwareInterface) { | ||
$fixture->setServiceLocator($this->serviceLocator); | ||
} | ||
parent::addFixture($fixture); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tests/DoctrineDataFixtureTest/Loader/ServiceLocatorAwareLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the LGPL. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
namespace DoctrineDataFixtureTest\Loader; | ||
|
||
use Doctrine\Common\DataFixtures\Loader; | ||
use Zend\ServiceManager\ServiceManager; | ||
use Zend\Mvc\Service\ServiceManagerConfig; | ||
use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader; | ||
|
||
/** | ||
* Test Service Locator-aware fixture loader | ||
* | ||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
* @link www.doctrine-project.org | ||
* @author Adam Lundrigan <adam@lundrigan.ca> | ||
*/ | ||
class ServiceLocatorAwareLoaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* Ensures that ServiceLocatorAwareLoader does not affect loading of | ||
* fixtures that are not SL-aware | ||
*/ | ||
public function testLoadingFixtureWhichIsNotServiceLocatorAware() | ||
{ | ||
$fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL\FixtureA'; | ||
$serviceLocator = new ServiceManager(new ServiceManagerConfig()); | ||
|
||
$loader = new ServiceLocatorAwareLoader($serviceLocator); | ||
$loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/NoSL'); | ||
$fixtures = $loader->getFixtures(); | ||
|
||
$this->assertArrayHasKey($fixtureClassName, $fixtures); | ||
$fixture = $fixtures[$fixtureClassName]; | ||
$this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture); | ||
$this->assertNotInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture); | ||
} | ||
|
||
/** | ||
* Ensures that the Service Locator instance passed into the ServiceLocatorAwareLoader | ||
* actually makes it to the SL-aware fixtures loaded | ||
*/ | ||
public function testLoadingFixtureWhichIsServiceLocatorAware() | ||
{ | ||
$fixtureClassName = 'DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL\FixtureA'; | ||
$serviceLocator = new ServiceManager(new ServiceManagerConfig()); | ||
|
||
$loader = new ServiceLocatorAwareLoader($serviceLocator); | ||
$loader->loadFromDirectory(__DIR__ . '/../TestAsset/Fixtures/HasSL'); | ||
$fixtures = $loader->getFixtures(); | ||
|
||
$this->assertArrayHasKey($fixtureClassName, $fixtures); | ||
$fixture = $fixtures[$fixtureClassName]; | ||
$this->assertInstanceOf('Doctrine\Common\DataFixtures\FixtureInterface', $fixture); | ||
$this->assertInstanceOf('Zend\ServiceManager\ServiceLocatorAwareInterface', $fixture); | ||
$this->assertSame($serviceLocator, $fixture->getServiceLocator()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/DoctrineDataFixtureTest/TestAsset/Fixtures/HasSL/FixtureA.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
namespace DoctrineDataFixtureTest\TestAsset\Fixtures\HasSL; | ||
|
||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
use Zend\ServiceManager\ServiceLocatorAwareInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class FixtureA implements FixtureInterface, ServiceLocatorAwareInterface | ||
{ | ||
/** | ||
* @var ServiceLocatorInterface | ||
*/ | ||
protected $serviceLocator = null; | ||
|
||
|
||
public function load(ObjectManager $manager) | ||
{ | ||
} | ||
|
||
/** | ||
* Set service locator | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return mixed | ||
*/ | ||
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$this->serviceLocator = $serviceLocator; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get service locator | ||
* | ||
* @return ServiceLocatorInterface | ||
*/ | ||
public function getServiceLocator() | ||
{ | ||
return $this->serviceLocator; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/DoctrineDataFixtureTest/TestAsset/Fixtures/NoSL/FixtureA.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
namespace DoctrineDataFixtureTest\TestAsset\Fixtures\NoSL; | ||
|
||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Doctrine\Common\DataFixtures\FixtureInterface; | ||
|
||
class FixtureA implements FixtureInterface | ||
{ | ||
public function load(ObjectManager $manager) | ||
{ | ||
} | ||
} |