DoctrineReaderTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Reader;
  3. use Ddeboer\DataImport\Reader\DoctrineReader;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\Tools\Setup;
  6. use Ddeboer\DataImport\Tests\Fixtures\Entity\User;
  7. class DoctrineReaderTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testGetFields()
  10. {
  11. $fields = $this->getReader()->getFields();
  12. $this->assertInternalType('array', $fields);
  13. $this->assertEquals(array('id', 'username'), $fields);
  14. }
  15. public function testCount()
  16. {
  17. $this->assertEquals(100, $this->getReader()->count());
  18. }
  19. public function testIterate()
  20. {
  21. $i = 1;
  22. foreach ($this->getReader() as $data) {
  23. $this->assertInternalType('array', $data);
  24. $this->assertEquals('user' . $i, $data['username']);
  25. $i++;
  26. }
  27. }
  28. protected function getReader()
  29. {
  30. $em = $this->getEntityManager();
  31. for ($i = 1; $i <= 100; $i++) {
  32. $user = new User();
  33. $user->setUsername('user'. $i);
  34. $em->persist($user);
  35. }
  36. $em->flush();
  37. return new DoctrineReader($em, 'Ddeboer\DataImport\Tests\Fixtures\Entity\User');
  38. }
  39. protected function getEntityManager()
  40. {
  41. $dbParams = array(
  42. 'driver' => 'pdo_sqlite',
  43. );
  44. $paths = array(
  45. __DIR__.'/../Fixtures/Entity'
  46. );
  47. $config = Setup::createAnnotationMetadataConfiguration($paths, true);
  48. $em = EntityManager::create($dbParams, $config);
  49. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
  50. $schemaTool->createSchema(
  51. array(
  52. $em->getMetadataFactory()->getMetadataFor('Ddeboer\DataImport\Tests\Fixtures\Entity\User')
  53. )
  54. );
  55. return $em;
  56. }
  57. }