AbstractTestCase.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Test
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once "Zend/Db/Table/Abstract.php";
  23. require_once "Zend/Db/Table.php";
  24. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTableDataSet.php";
  25. require_once "PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php";
  26. require_once "Zend/Test/PHPUnit/Db/SimpleTester.php";
  27. require_once "Zend/Test/PHPUnit/Db/DataSet/DbRowset.php";
  28. /**
  29. * @category Zend
  30. * @package Zend_Test
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Test
  35. */
  36. abstract class Zend_Test_PHPUnit_Db_Integration_AbstractTestCase extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * @var Zend_Db_Adapter_Abstract
  40. */
  41. protected $dbAdapter;
  42. public function testZendDbTableDataSet()
  43. {
  44. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  45. $dataSet->addTable($this->createFooTable());
  46. $dataSet->addTable($this->createBarTable());
  47. $this->assertEquals(
  48. "foo", $dataSet->getTableMetaData('foo')->getTableName()
  49. );
  50. $this->assertEquals(
  51. "bar", $dataSet->getTableMetaData("bar")->getTableName()
  52. );
  53. $this->assertEquals(array("foo", "bar"), $dataSet->getTableNames());
  54. }
  55. public function testZendDbTableEqualsXmlDataSet()
  56. {
  57. $fooTable = $this->createFooTable();
  58. $fooTable->insert(array("id" => null, "foo" => "foo", "bar" => "bar", "baz" => "baz"));
  59. $fooTable->insert(array("id" => null, "foo" => "bar", "bar" => "bar", "baz" => "bar"));
  60. $fooTable->insert(array("id" => null, "foo" => "baz", "bar" => "baz", "baz" => "baz"));
  61. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  62. $dataSet->addTable($fooTable);
  63. $xmlDataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  64. dirname(__FILE__)."/_files/sqliteIntegrationFixture.xml"
  65. );
  66. if (method_exists($xmlDataSet, 'assertEquals')) {
  67. $this->assertTrue($xmlDataSet->assertEquals($dataSet));
  68. } else {
  69. $this->assertTrue($xmlDataSet->matches($dataSet));
  70. }
  71. }
  72. /**
  73. * @return Zend_Test_PHPUnit_Db_Connection
  74. */
  75. public function getConnection()
  76. {
  77. return new Zend_Test_PHPUnit_Db_Connection($this->dbAdapter, 'foo');
  78. }
  79. public function testSimpleTesterSetupAndRowsetEquals()
  80. {
  81. $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  82. dirname(__FILE__)."/_files/sqliteIntegrationFixture.xml"
  83. );
  84. $fooDataTable = $dataSet->getTable("foo");
  85. $tester = new Zend_Test_PHPUnit_Db_SimpleTester($this->getConnection());
  86. $tester->setUpDatabase($dataSet);
  87. $fooTable = $this->createFooTable();
  88. $rows = $fooTable->fetchAll();
  89. $this->assertEquals(3, count($rows));
  90. $rowsetTable = new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rows);
  91. if (method_exists($rowsetTable, 'assertEquals')) {
  92. $rowsetTable->assertEquals($fooDataTable);
  93. } else {
  94. $rowsetTable->matches($fooDataTable);
  95. }
  96. }
  97. /**
  98. * @return Zend_Test_PHPUnit_Db_TableFoo
  99. */
  100. public function createFooTable()
  101. {
  102. $table = new Zend_Test_PHPUnit_Db_TableFoo(array('db' => $this->dbAdapter));
  103. return $table;
  104. }
  105. /**
  106. * @return Zend_Test_PHPUnit_Db_TableBar
  107. */
  108. public function createBarTable()
  109. {
  110. $table = new Zend_Test_PHPUnit_Db_TableBar(array('db' => $this->dbAdapter));
  111. return $table;
  112. }
  113. }
  114. class Zend_Test_PHPUnit_Db_TableFoo extends Zend_Db_Table_Abstract
  115. {
  116. protected $_name = "foo";
  117. protected $_primary = "id";
  118. }
  119. class Zend_Test_PHPUnit_Db_TableBar extends Zend_Db_Table_Abstract
  120. {
  121. protected $_name = "bar";
  122. protected $_primary = "id";
  123. }