PdoWriterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Writer;
  3. use Ddeboer\DataImport\Writer\PdoWriter;
  4. class PdoWriterTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @var \PDO
  8. */
  9. private $pdo;
  10. public function setUp()
  11. {
  12. $this->pdo = new \PDO('sqlite::memory:');
  13. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //important
  14. $this->pdo->exec('DROP TABLE IF EXISTS `example`');
  15. $this->pdo->exec('CREATE TABLE `example` (a TEXT, b TEXT)');
  16. }
  17. public function testValidWriteItem()
  18. {
  19. $writer = new PdoWriter($this->pdo, 'example');
  20. $writer->prepare();
  21. $writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
  22. $writer->finish();
  23. $stmnt = $this->pdo->query('SELECT * FROM `example`');
  24. $this->assertEquals(
  25. array(array('a'=>'foo', 'b'=>'bar')),
  26. $stmnt->fetchAll(\PDO::FETCH_ASSOC),
  27. 'database does not contain expected row'
  28. );
  29. }
  30. public function testValidWriteMultiple()
  31. {
  32. $writer = new PdoWriter($this->pdo, 'example');
  33. $writer->prepare();
  34. $writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
  35. $writer->writeItem(array('a' => 'cat', 'b' => 'dog'));
  36. $writer->writeItem(array('a' => 'ac', 'b' => 'dc'));
  37. $writer->finish();
  38. $stmnt = $this->pdo->query('SELECT * FROM `example`');
  39. $this->assertEquals(
  40. array(array('a'=>'foo', 'b'=>'bar'), array('a'=>'cat', 'b'=>'dog'), array('a'=>'ac', 'b'=>'dc')),
  41. $stmnt->fetchAll(\PDO::FETCH_ASSOC),
  42. 'database does not contain all expected rows'
  43. );
  44. }
  45. /**
  46. * @expectedException \Ddeboer\DataImport\Exception\WriterException
  47. */
  48. public function testWriteTooManyValues()
  49. {
  50. $writer = new PdoWriter($this->pdo, 'example');
  51. $writer->prepare();
  52. $writer->writeItem(array('foo', 'bar', 'baz')); //expects two
  53. $writer->finish();
  54. }
  55. /**
  56. * @expectedException \Ddeboer\DataImport\Exception\WriterException
  57. */
  58. public function testWriteToNonexistentTable()
  59. {
  60. $writer = new PdoWriter($this->pdo, 'foobar');
  61. $writer->prepare();
  62. $writer->writeItem(array('foo', 'bar'));
  63. $writer->finish();
  64. }
  65. /**
  66. * Tests PDO instance with silent errors.
  67. *
  68. * @expectedException \Ddeboer\DataImport\Exception\WriterException
  69. */
  70. public function testStatementCreateFailureWithNoException()
  71. {
  72. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
  73. $writer = new PdoWriter($this->pdo, 'foob`ar');
  74. $writer->prepare();
  75. $writer->writeItem(array('foo', 'bar'));
  76. $writer->finish();
  77. }
  78. /**
  79. * Tests PDO instance with silent errors. First inert prepares the statement, second creates an exception.
  80. *
  81. * @expectedException \Ddeboer\DataImport\Exception\WriterException
  82. */
  83. public function testWriteFailureWithNoException()
  84. {
  85. $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
  86. $writer = new PdoWriter($this->pdo, 'example');
  87. $writer->prepare();
  88. $writer->writeItem(array('foo', 'bar'));
  89. $writer->writeItem(array('foo', 'bar', 'baz'));
  90. $writer->finish();
  91. }
  92. }