TruncateTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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-2012 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/Test/DbAdapter.php";
  23. require_once "Zend/Test/PHPUnit/Db/Operation/Truncate.php";
  24. require_once "PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php";
  25. /**
  26. * @category Zend
  27. * @package Zend_Test
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Test
  32. */
  33. class Zend_Test_PHPUnit_Db_Operation_TruncateTest extends PHPUnit_Framework_TestCase
  34. {
  35. private $operation = null;
  36. public function setUp()
  37. {
  38. $this->operation = new Zend_Test_PHPUnit_Db_Operation_Truncate();
  39. }
  40. public function testTruncateTablesExecutesAdapterQuery()
  41. {
  42. $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(dirname(__FILE__)."/_files/truncateFixture.xml");
  43. $testAdapter = $this->getMock('Zend_Test_DbAdapter');
  44. $testAdapter->expects($this->at(0))
  45. ->method('quoteIdentifier')
  46. ->with('bar')->will($this->returnValue('bar'));
  47. $testAdapter->expects($this->at(1))
  48. ->method('query')
  49. ->with('TRUNCATE bar');
  50. $testAdapter->expects($this->at(2))
  51. ->method('quoteIdentifier')
  52. ->with('foo')->will($this->returnValue('foo'));
  53. $testAdapter->expects($this->at(3))
  54. ->method('query')
  55. ->with('TRUNCATE foo');
  56. $connection = new Zend_Test_PHPUnit_Db_Connection($testAdapter, "schema");
  57. $this->operation->execute($connection, $dataSet);
  58. }
  59. public function testTruncateTableInvalidQueryTransformsException()
  60. {
  61. $this->setExpectedException('PHPUnit_Extensions_Database_Operation_Exception');
  62. $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(dirname(__FILE__)."/_files/insertFixture.xml");
  63. $testAdapter = $this->getMock('Zend_Test_DbAdapter');
  64. $testAdapter->expects($this->any())->method('query')->will($this->throwException(new Exception()));
  65. $connection = new Zend_Test_PHPUnit_Db_Connection($testAdapter, "schema");
  66. $this->operation->execute($connection, $dataSet);
  67. }
  68. public function testInvalidConnectionGivenThrowsException()
  69. {
  70. $this->setExpectedException("Zend_Test_PHPUnit_Db_Exception");
  71. $dataSet = $this->getMock('PHPUnit_Extensions_Database_DataSet_IDataSet');
  72. $connection = $this->getMock('PHPUnit_Extensions_Database_DB_IDatabaseConnection');
  73. $this->operation->execute($connection, $dataSet);
  74. }
  75. /**
  76. * @group ZF-7936
  77. */
  78. public function testTruncateAppliedToTablesInReverseOrder()
  79. {
  80. $testAdapter = new Zend_Test_DbAdapter();
  81. $connection = new Zend_Test_PHPUnit_Db_Connection($testAdapter, "schema");
  82. $dataSet = new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(dirname(__FILE__)."/_files/truncateFixture.xml");
  83. $this->operation->execute($connection, $dataSet);
  84. $profiler = $testAdapter->getProfiler();
  85. $queries = $profiler->getQueryProfiles();
  86. $this->assertEquals(2, count($queries));
  87. $this->assertContains('bar', $queries[0]->getQuery());
  88. $this->assertContains('foo', $queries[1]->getQuery());
  89. }
  90. }