AbstractTestCase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "Zend/Test/PHPUnit/Db/SimpleTester.php";
  26. require_once "Zend/Test/PHPUnit/Db/DataSet/DbRowset.php";
  27. /**
  28. * @category Zend
  29. * @package Zend_Test
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Test
  34. */
  35. abstract class Zend_Test_PHPUnit_Db_Integration_AbstractTestCase extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * @var Zend_Db_Adapter_Abstract
  39. */
  40. protected $dbAdapter;
  41. public function testZendDbTableDataSet()
  42. {
  43. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  44. $dataSet->addTable($this->createFooTable());
  45. $dataSet->addTable($this->createBarTable());
  46. $this->assertEquals(
  47. "foo", $dataSet->getTableMetaData('foo')->getTableName()
  48. );
  49. $this->assertEquals(
  50. "bar", $dataSet->getTableMetaData("bar")->getTableName()
  51. );
  52. $this->assertEquals(array("foo", "bar"), $dataSet->getTableNames());
  53. }
  54. public function testZendDbTableEqualsXmlDataSet()
  55. {
  56. $fooTable = $this->createFooTable();
  57. $fooTable->insert(array("id" => null, "foo" => "foo", "bar" => "bar", "baz" => "baz"));
  58. $fooTable->insert(array("id" => null, "foo" => "bar", "bar" => "bar", "baz" => "bar"));
  59. $fooTable->insert(array("id" => null, "foo" => "baz", "bar" => "baz", "baz" => "baz"));
  60. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  61. $dataSet->addTable($fooTable);
  62. $xmlDataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  63. dirname(__FILE__)."/_files/sqliteIntegrationFixture.xml"
  64. );
  65. if (method_exists($xmlDataSet, 'assertEquals')) {
  66. $this->assertTrue($xmlDataSet->assertEquals($dataSet));
  67. } else {
  68. $this->assertTrue($xmlDataSet->matches($dataSet));
  69. }
  70. }
  71. /**
  72. * @return Zend_Test_PHPUnit_Db_Connection
  73. */
  74. public function getConnection()
  75. {
  76. return new Zend_Test_PHPUnit_Db_Connection($this->dbAdapter, 'foo');
  77. }
  78. public function testSimpleTesterSetupAndRowsetEquals()
  79. {
  80. $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  81. dirname(__FILE__)."/_files/sqliteIntegrationFixture.xml"
  82. );
  83. $fooDataTable = $dataSet->getTable("foo");
  84. $tester = new Zend_Test_PHPUnit_Db_SimpleTester($this->getConnection());
  85. $tester->setUpDatabase($dataSet);
  86. $fooTable = $this->createFooTable();
  87. $rows = $fooTable->fetchAll();
  88. $this->assertEquals(3, count($rows));
  89. $rowsetTable = new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rows);
  90. if (method_exists($rowsetTable, 'assertEquals')) {
  91. $rowsetTable->assertEquals($fooDataTable);
  92. } else {
  93. $rowsetTable->matches($fooDataTable);
  94. }
  95. }
  96. /**
  97. * @return Zend_Test_PHPUnit_Db_TableFoo
  98. */
  99. public function createFooTable()
  100. {
  101. $table = new Zend_Test_PHPUnit_Db_TableFoo(array('db' => $this->dbAdapter));
  102. return $table;
  103. }
  104. /**
  105. * @return Zend_Test_PHPUnit_Db_TableBar
  106. */
  107. public function createBarTable()
  108. {
  109. $table = new Zend_Test_PHPUnit_Db_TableBar(array('db' => $this->dbAdapter));
  110. return $table;
  111. }
  112. }
  113. class Zend_Test_PHPUnit_Db_TableFoo extends Zend_Db_Table_Abstract
  114. {
  115. protected $_name = "foo";
  116. protected $_primary = "id";
  117. }
  118. class Zend_Test_PHPUnit_Db_TableBar extends Zend_Db_Table_Abstract
  119. {
  120. protected $_name = "bar";
  121. protected $_primary = "id";
  122. }