Skip to content

Commit

Permalink
More Test coverage and a minor "use" fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
h4cc committed Sep 1, 2014
1 parent 933066e commit 9c8af4c
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 20 deletions.
17 changes: 0 additions & 17 deletions Fixtures/FixtureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions ORM/MongoDBODMSchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Command/LoadSetsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
);
}

Expand Down
61 changes: 61 additions & 0 deletions Tests/ORM/DoctrineORMSchemaToolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the h4cc/AliceFixtureBundle package.
*
* (c) Julius Beckmann <[email protected]>
*
* 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 <[email protected]>
* @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();
}
}

180 changes: 180 additions & 0 deletions Tests/ORM/DoctrineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

/*
* This file is part of the h4cc/AliceFixtureBundle package.
*
* (c) Julius Beckmann <[email protected]>
*
* 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 <[email protected]>
* @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);
}
}

61 changes: 61 additions & 0 deletions Tests/ORM/MongoDBODMSchemaToolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the h4cc/AliceFixtureBundle package.
*
* (c) Julius Beckmann <[email protected]>
*
* 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 <[email protected]>
* @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();
}
}

0 comments on commit 9c8af4c

Please sign in to comment.