resultTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-02-06 at 20:48:50.
  4. */
  5. class PDO_Result_Test extends PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Result_PDO_Driver
  9. */
  10. protected $object;
  11. /**
  12. * Sets up the fixture, for example, opens a network connection.
  13. * This method is called before a test is executed.
  14. */
  15. protected function setUp()
  16. {
  17. $db = new PDO('sqlite::memory:');
  18. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. $db->exec("CREATE TABLE fairies(id INT,name VARCHAR(255))");
  20. $db->exec("INSERT INTO fairies (id,name) VALUES (1,'Tinkerbell')");
  21. $db->exec("INSERT INTO fairies (id,name) VALUES (2,'Trixie')");
  22. $q = $db->prepare("SELECT * from fairies");
  23. $q->execute();
  24. $this->object = new \PHPixie\DB\PDO\Result($q);
  25. }
  26. /**
  27. * Tears down the fixture, for example, closes a network connection.
  28. * This method is called after a test is executed.
  29. */
  30. protected function tearDown()
  31. {
  32. }
  33. /**
  34. * @covers Result_PDO_Driver::rewind
  35. * @todo Implement testRewind().
  36. */
  37. public function testRewind()
  38. {
  39. $except = false;
  40. $this->object->valid();
  41. $this->object->rewind();
  42. $this->object->next();
  43. try {
  44. $this->object->rewind();
  45. } catch (Exception $e) {
  46. $except = true;
  47. }
  48. $this->assertEquals(true, $except);
  49. }
  50. /**
  51. * @covers Result_PDO_Driver::current
  52. */
  53. public function testCurrent()
  54. {
  55. $this->assertEquals($this->object->current()->name, 'Tinkerbell');
  56. }
  57. /**
  58. * @covers Result_PDO_Driver::valid
  59. */
  60. public function testVaid()
  61. {
  62. $this->assertEquals($this->object->valid(), true);
  63. }
  64. /**
  65. * @covers Result_PDO_Driver::key
  66. */
  67. public function testKey()
  68. {
  69. $this->assertEquals($this->object->key(), 0);
  70. }
  71. /**
  72. * @covers Result_PDO_Driver::key
  73. */
  74. public function testGet()
  75. {
  76. $this->assertEquals($this->object->get('id'), 1);
  77. }
  78. /**
  79. * @covers Result_PDO_Driver::as_array
  80. */
  81. public function testAs_Array()
  82. {
  83. $arr = $this->object->as_array();
  84. $this->assertArrayHasKey(0, $arr);
  85. $this->assertArrayHasKey('name', (array) $arr[0]);
  86. $this->assertEquals($arr[0]->name, 'Tinkerbell');
  87. $this->assertArrayHasKey(1, $arr);
  88. $this->assertArrayHasKey('id', (array) $arr[1]);
  89. $this->assertEquals($arr[1]->id, 2);
  90. }
  91. public function testIterator()
  92. {
  93. $this->assertEquals($this->object->valid(), true);
  94. $this->assertEquals($this->object->get('id'), 1);
  95. foreach ($this->object as $key => $row)
  96. {
  97. if ($key == 0)
  98. {
  99. $this->assertEquals($row->name, 'Tinkerbell');
  100. $this->assertEquals($row->id, 1);
  101. }
  102. if ($key == 1)
  103. {
  104. $this->assertEquals($row->name, 'Trixie');
  105. $this->assertEquals(2, $this->object->get('id'));
  106. $this->assertEquals($row->id, 2);
  107. }
  108. }
  109. $this->assertEquals(false, $this->object->valid());
  110. $this->assertEquals(null, $this->object->get('id'));
  111. $this->assertEquals(null, $this->object->current());
  112. $this->object->next();
  113. $this->object->next();
  114. $this->object->next();
  115. $this->assertEquals(1, $this->object->key());
  116. }
  117. }