TruncateTest.php 4.1 KB

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