DoctrineWriterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Writer;
  3. use Ddeboer\DataImport\Writer\DoctrineWriter;
  4. use Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity;
  5. class DoctrineWriterTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testWriteItem()
  8. {
  9. $em = $this->getEntityManager();
  10. $em->expects($this->once())
  11. ->method('persist');
  12. $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
  13. $association = new TestEntity();
  14. $item = array(
  15. 'firstProperty' => 'some value',
  16. 'secondProperty' => 'some other value',
  17. 'firstAssociation'=> $association
  18. );
  19. $writer->writeItem($item);
  20. }
  21. protected function getEntityManager()
  22. {
  23. $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
  24. ->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference'))
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
  31. ->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $metadata->expects($this->any())
  35. ->method('getName')
  36. ->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
  37. $metadata->expects($this->any())
  38. ->method('getFieldNames')
  39. ->will($this->returnValue(array('firstProperty', 'secondProperty')));
  40. $metadata->expects($this->any())
  41. ->method('getAssociationNames')
  42. ->will($this->returnValue(array('firstAssociation')));
  43. $metadata->expects($this->any())
  44. ->method('getAssociationMappings')
  45. ->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'))));
  46. $configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
  47. ->setMethods(array('getConnection'))
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
  51. ->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery'))
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $connection->expects($this->any())
  55. ->method('getConfiguration')
  56. ->will($this->returnValue($configuration));
  57. $connection->expects($this->any())
  58. ->method('getDatabasePlatform')
  59. ->will($this->returnSelf());
  60. $connection->expects($this->any())
  61. ->method('getTruncateTableSQL')
  62. ->will($this->returnValue('TRUNCATE SQL'));
  63. $connection->expects($this->any())
  64. ->method('executeQuery')
  65. ->with('TRUNCATE SQL');
  66. $em->expects($this->once())
  67. ->method('getRepository')
  68. ->will($this->returnValue($repo));
  69. $em->expects($this->once())
  70. ->method('getClassMetadata')
  71. ->will($this->returnValue($metadata));
  72. $em->expects($this->any())
  73. ->method('getConnection')
  74. ->will($this->returnValue($connection));
  75. $self = $this;
  76. $em->expects($this->any())
  77. ->method('persist')
  78. ->will($this->returnCallback(function ($argument) use ($self) {
  79. $self->assertNotNull($argument->getFirstAssociation());
  80. return true;
  81. }));
  82. return $em;
  83. }
  84. public function testLoadAssociationWithoutObject()
  85. {
  86. $em = $this->getEntityManager();
  87. $em->expects($this->once())
  88. ->method('persist');
  89. $em->expects($this->once())
  90. ->method('getReference');
  91. $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
  92. $item = array(
  93. 'firstProperty' => 'some value',
  94. 'secondProperty' => 'some other value',
  95. 'firstAssociation' => 'firstAssociationId'
  96. );
  97. $writer->writeItem($item);
  98. }
  99. public function testLoadAssociationWithPresetObject()
  100. {
  101. $em = $this->getEntityManager();
  102. $em->expects($this->once())
  103. ->method('persist');
  104. $em->expects($this->never())
  105. ->method('getReference');
  106. $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
  107. $association = new TestEntity();
  108. $item = array(
  109. 'firstProperty' => 'some value',
  110. 'secondProperty' => 'some other value',
  111. 'firstAssociation' => $association,
  112. );
  113. $writer->writeItem($item);
  114. }
  115. /**
  116. * Test to make sure that we are clearing the write entity
  117. */
  118. public function testFlushAndClear()
  119. {
  120. $em = $this->getEntityManager();
  121. $em->expects($this->once())
  122. ->method('clear')
  123. ->with($this->equalTo('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
  124. $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
  125. $writer->finish();
  126. }
  127. }