diff --git a/Fixtures/FixtureManager.php b/Fixtures/FixtureManager.php index 897f2f7..9a15584 100644 --- a/Fixtures/FixtureManager.php +++ b/Fixtures/FixtureManager.php @@ -185,23 +185,6 @@ private function createNeededLoaders(FixtureSet $set) return $loaders; } - /** - * Helper for a "intersect" of loaded objects. - * - * @param $base - * @param $extra - * @return mixed - */ - private function removeLocalReferences($base, $extra) { - $intersect = $base; - foreach ($base as $key => $value){ - if (!in_array($value, $extra)){ - unset($intersect[$key]); - } - } - return $intersect; - } - /** * Returns the ORM. * diff --git a/ORM/MongoDBODMSchemaTool.php b/ORM/MongoDBODMSchemaTool.php index 84addf4..2a44cf4 100644 --- a/ORM/MongoDBODMSchemaTool.php +++ b/ORM/MongoDBODMSchemaTool.php @@ -11,7 +11,7 @@ namespace h4cc\AliceFixturesBundle\ORM; -use Doctrine\Bundle\MongoDBBundle\ManagerRegistry; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ODM\MongoDB\DocumentManager; class MongoDBODMSchemaTool implements SchemaToolInterface @@ -35,7 +35,7 @@ public function dropSchema() // NOT Dropping Databases, because of potential permission problems. // (After dropping your own database, only a admin can recreate it.) - $schemaManager->dropDatabases(); + //$schemaManager->dropDatabases(); }); } diff --git a/Tests/Command/LoadSetsCommandTest.php b/Tests/Command/LoadSetsCommandTest.php index 7734511..241b15c 100644 --- a/Tests/Command/LoadSetsCommandTest.php +++ b/Tests/Command/LoadSetsCommandTest.php @@ -93,13 +93,14 @@ public function testLoadWithoutDefaultManager() /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage File 'Tests/testdata/InvalidSet.php' does not return a FixtureSetInterface. */ public function testLoadExceptionNotAFixtureSetInterface() { $tester = new CommandTester($this->command); $tester->execute( - array('command' => $this->command->getName(), 'sets' => array(__DIR__ . '/../../testdata/InvalidSet.php')) + array('command' => $this->command->getName(), 'sets' => array('Tests/testdata/InvalidSet.php')) ); } diff --git a/Tests/ORM/DoctrineORMSchemaToolTest.php b/Tests/ORM/DoctrineORMSchemaToolTest.php new file mode 100644 index 0000000..9f804f3 --- /dev/null +++ b/Tests/ORM/DoctrineORMSchemaToolTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace h4cc\AliceFixturesBundle\Tests; + +use h4cc\AliceFixturesBundle\ORM\DoctrineORMSchemaTool; + +/** + * Class DoctrineORMSchemaToolTest + * + * @author Julius Beckmann + * @covers h4cc\AliceFixturesBundle\ORM\DoctrineORMSchemaTool + */ +class DoctrineORMSchemaToolTest extends \PHPUnit_Framework_TestCase +{ + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $managerRegistryMock; + + public function setUp() + { + $this->managerRegistryMock = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') + ->setMethods(array('getManagers')) + ->getMockForAbstractClass(); + + $this->managerRegistryMock->expects($this->any()) + ->method('getManagers') + ->will($this->returnValue(array())); + } + + public function testConstruct() + { + $schemaTool = new DoctrineORMSchemaTool($this->managerRegistryMock); + + $this->assertInstanceOf('\h4cc\AliceFixturesBundle\ORM\SchemaToolInterface', $schemaTool); + } + + public function testDropSchema() + { + $schemaTool = new DoctrineORMSchemaTool($this->managerRegistryMock); + + // Not testing any further here for now, because mocking for DoctrineSchemaTool needs some effort. + $schemaTool->dropSchema(); + } + + public function testCreateSchema() + { + $schemaTool = new DoctrineORMSchemaTool($this->managerRegistryMock); + + // Not testing any further here for now, because mocking for DoctrineSchemaTool needs some effort. + $schemaTool->createSchema(); + } +} + \ No newline at end of file diff --git a/Tests/ORM/DoctrineTest.php b/Tests/ORM/DoctrineTest.php new file mode 100644 index 0000000..754fa89 --- /dev/null +++ b/Tests/ORM/DoctrineTest.php @@ -0,0 +1,180 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace h4cc\AliceFixturesBundle\Tests; + +use h4cc\AliceFixturesBundle\ORM\Doctrine; + +/** + * Class DoctrineTest + * + * @author Julius Beckmann + * @covers h4cc\AliceFixturesBundle\ORM\Doctrine + */ +class DoctrineTest extends \PHPUnit_Framework_TestCase +{ + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $managerMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $managerRegistryMock; + + private $objects = array(); + private $testObject; + + public function setUp() + { + $this->managerMock = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager') + ->setMethods(array('persist', 'find', 'remove', 'merge', 'detach', 'flush')) + ->getMockForAbstractClass(); + + $this->managerRegistryMock = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') + ->setMethods(array('getManagerForClass')) + ->getMockForAbstractClass(); + + $this->managerRegistryMock->expects($this->any()) + ->method('getManagerForClass') + ->will($this->returnValue($this->managerMock)); + + $this->objects = range('a', 'c'); + $this->objects[] = $this->testObject = new \stdClass(); + } + + public function testConstruct() + { + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $this->assertInstanceOf('\h4cc\AliceFixturesBundle\ORM\ORMInterface', $doctrine); + } + + public function testPersist() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('persist'); + $this->managerMock->expects($this->once())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $doctrine->persist($this->objects); + } + + public function testPersistWithoutFlush() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('persist'); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, false); + + $doctrine->persist($this->objects); + } + + public function testMerge() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('merge') + ->will($this->returnValueMap(array( + array('a', 'a_merged'), + array('b', 'b_merged'), + array('c', 'c_merged'), + array($this->testObject, 'testObject_merged'), + ))); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $mergedObjects = $doctrine->merge($this->objects); + + $this->assertEquals(array('a_merged', 'b_merged', 'c_merged', 'testObject_merged'), $mergedObjects); + } + + public function testDetach() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('detach'); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $doctrine->detach($this->objects); + } + + public function testRemove() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('merge'); + $this->managerMock->expects($this->exactly(count($this->objects)))->method('remove'); + $this->managerMock->expects($this->once())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $doctrine->remove($this->objects); + } + + public function testRemoveWithoutFlush() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('merge'); + $this->managerMock->expects($this->exactly(count($this->objects)))->method('remove'); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, false); + + $doctrine->remove($this->objects); + } + + public function testFind() + { + $this->managerMock->expects($this->exactly(count($this->objects)))->method('find') + ->will($this->returnValueMap(array( + array('Some/Class', 0, 'a'), + array('Some/Class', 1, 'b'), + array('Some/Class', 2, 'c'), + array('Some/Class', 3, $this->testObject), + ))); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + foreach($this->objects as $index => $expectedEntity) { + $entity = $doctrine->find('Some/Class', $index); + $this->assertEquals($expectedEntity, $entity); + } + } + + /** + * @expectedException \UnexpectedValueException + * @expectedExceptionMessage Entity with Id 42 and Class Some/Class not found + */ + public function testFindNotFoundException() + { + $this->managerMock->expects($this->once())->method('find') + ->will($this->returnValueMap(array( + array('Some/Class', 42, null), + ))); + $this->managerMock->expects($this->never())->method('flush'); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $doctrine->find('Some/Class', 42); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage No ObjectManager for class Some/Class + */ + public function testNoObjectManager() + { + $this->managerRegistryMock = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') + ->setMethods(array('getManagerForClass')) + ->getMockForAbstractClass(); + $this->managerRegistryMock->expects($this->once())->method('getManagerForClass')->with('Some/Class')->willReturn(null); + + $doctrine = new Doctrine($this->managerRegistryMock, true); + + $doctrine->find('Some/Class', 42); + } +} + \ No newline at end of file diff --git a/Tests/ORM/MongoDBODMSchemaToolTest.php b/Tests/ORM/MongoDBODMSchemaToolTest.php new file mode 100644 index 0000000..fb764d9 --- /dev/null +++ b/Tests/ORM/MongoDBODMSchemaToolTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace h4cc\AliceFixturesBundle\Tests; + +use h4cc\AliceFixturesBundle\ORM\MongoDBODMSchemaTool; + +/** + * Class MongoDBODMSchemaToolTest + * + * @author Julius Beckmann + * @covers h4cc\AliceFixturesBundle\ORM\MongoDBODMSchemaTool + */ +class MongoDBODMSchemaToolTest extends \PHPUnit_Framework_TestCase +{ + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $managerRegistryMock; + + public function setUp() + { + $this->managerRegistryMock = $this->getMockBuilder('\Doctrine\Common\Persistence\ManagerRegistry') + ->setMethods(array('getManagers')) + ->getMockForAbstractClass(); + + $this->managerRegistryMock->expects($this->any()) + ->method('getManagers') + ->will($this->returnValue(array())); + } + + public function testConstruct() + { + $schemaTool = new MongoDBODMSchemaTool($this->managerRegistryMock); + + $this->assertInstanceOf('\h4cc\AliceFixturesBundle\ORM\SchemaToolInterface', $schemaTool); + } + + public function testDropSchema() + { + $schemaTool = new MongoDBODMSchemaTool($this->managerRegistryMock); + + // Not testing any further here for now, because mocking for DoctrineSchemaTool needs some effort. + $schemaTool->dropSchema(); + } + + public function testCreateSchema() + { + $schemaTool = new MongoDBODMSchemaTool($this->managerRegistryMock); + + // Not testing any further here for now, because mocking for DoctrineSchemaTool needs some effort. + $schemaTool->createSchema(); + } +} + \ No newline at end of file