123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace Ddeboer\DataImport\Tests\Writer;
- use Ddeboer\DataImport\Writer\DoctrineWriter;
- use Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity;
- class DoctrineWriterTest extends \PHPUnit_Framework_TestCase
- {
- public function testWriteItem()
- {
- $em = $this->getEntityManager();
- $em->expects($this->once())
- ->method('persist');
- $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
- $association = new TestEntity();
- $item = array(
- 'firstProperty' => 'some value',
- 'secondProperty' => 'some other value',
- 'firstAssociation'=> $association
- );
- $writer->writeItem($item);
- }
- protected function getEntityManager()
- {
- $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
- ->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference'))
- ->disableOriginalConstructor()
- ->getMock();
- $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
- ->disableOriginalConstructor()
- ->getMock();
- $metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
- ->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
- ->disableOriginalConstructor()
- ->getMock();
- $metadata->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
- $metadata->expects($this->any())
- ->method('getFieldNames')
- ->will($this->returnValue(array('firstProperty', 'secondProperty')));
- $metadata->expects($this->any())
- ->method('getAssociationNames')
- ->will($this->returnValue(array('firstAssociation')));
- $metadata->expects($this->any())
- ->method('getAssociationMappings')
- ->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'))));
- $configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
- ->setMethods(array('getConnection'))
- ->disableOriginalConstructor()
- ->getMock();
- $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
- ->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery'))
- ->disableOriginalConstructor()
- ->getMock();
- $connection->expects($this->any())
- ->method('getConfiguration')
- ->will($this->returnValue($configuration));
- $connection->expects($this->any())
- ->method('getDatabasePlatform')
- ->will($this->returnSelf());
- $connection->expects($this->any())
- ->method('getTruncateTableSQL')
- ->will($this->returnValue('TRUNCATE SQL'));
- $connection->expects($this->any())
- ->method('executeQuery')
- ->with('TRUNCATE SQL');
- $em->expects($this->once())
- ->method('getRepository')
- ->will($this->returnValue($repo));
- $em->expects($this->once())
- ->method('getClassMetadata')
- ->will($this->returnValue($metadata));
- $em->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($connection));
- $self = $this;
- $em->expects($this->any())
- ->method('persist')
- ->will($this->returnCallback(function ($argument) use ($self) {
- $self->assertNotNull($argument->getFirstAssociation());
- return true;
- }));
- return $em;
- }
- public function testLoadAssociationWithoutObject()
- {
- $em = $this->getEntityManager();
- $em->expects($this->once())
- ->method('persist');
- $em->expects($this->once())
- ->method('getReference');
- $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
- $item = array(
- 'firstProperty' => 'some value',
- 'secondProperty' => 'some other value',
- 'firstAssociation' => 'firstAssociationId'
- );
- $writer->writeItem($item);
- }
- public function testLoadAssociationWithPresetObject()
- {
- $em = $this->getEntityManager();
- $em->expects($this->once())
- ->method('persist');
- $em->expects($this->never())
- ->method('getReference');
- $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
- $association = new TestEntity();
- $item = array(
- 'firstProperty' => 'some value',
- 'secondProperty' => 'some other value',
- 'firstAssociation' => $association,
- );
- $writer->writeItem($item);
- }
- /**
- * Test to make sure that we are clearing the write entity
- */
- public function testFlushAndClear()
- {
- $em = $this->getEntityManager();
- $em->expects($this->once())
- ->method('clear')
- ->with($this->equalTo('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
- $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
- $writer->finish();
- }
- }
|