PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Db/Sql/InsertTest.php

http://github.com/zendframework/zf2
PHP | 357 lines | 225 code | 59 blank | 73 comment | 0 complexity | 35c035c1639a65264130894d44742f8d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Db\Sql;
  10. use Zend\Db\Sql\Insert;
  11. use Zend\Db\Sql\Select;
  12. use Zend\Db\Sql\Expression;
  13. use Zend\Db\Sql\TableIdentifier;
  14. use ZendTest\Db\TestAsset\TrustingSql92Platform;
  15. class InsertTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var Insert
  19. */
  20. protected $insert;
  21. /**
  22. * Sets up the fixture, for example, opens a network connection.
  23. * This method is called before a test is executed.
  24. */
  25. protected function setUp()
  26. {
  27. $this->insert = new Insert;
  28. }
  29. /**
  30. * @covers Zend\Db\Sql\Insert::into
  31. */
  32. public function testInto()
  33. {
  34. $this->insert->into('table', 'schema');
  35. $this->assertEquals('table', $this->insert->getRawState('table'));
  36. $tableIdentifier = new TableIdentifier('table', 'schema');
  37. $this->insert->into($tableIdentifier);
  38. $this->assertEquals($tableIdentifier, $this->insert->getRawState('table'));
  39. }
  40. /**
  41. * @covers Zend\Db\Sql\Insert::columns
  42. */
  43. public function testColumns()
  44. {
  45. $this->insert->columns(array('foo', 'bar'));
  46. $this->assertEquals(array('foo', 'bar'), $this->insert->getRawState('columns'));
  47. }
  48. /**
  49. * @covers Zend\Db\Sql\Insert::values
  50. */
  51. public function testValues()
  52. {
  53. $this->insert->values(array('foo' => 'bar'));
  54. $this->assertEquals(array('foo'), $this->insert->getRawState('columns'));
  55. $this->assertEquals(array('bar'), $this->insert->getRawState('values'));
  56. // test will merge cols and values of previously set stuff
  57. $this->insert->values(array('foo' => 'bax'), Insert::VALUES_MERGE);
  58. $this->insert->values(array('boom' => 'bam'), Insert::VALUES_MERGE);
  59. $this->assertEquals(array('foo', 'boom'), $this->insert->getRawState('columns'));
  60. $this->assertEquals(array('bax', 'bam'), $this->insert->getRawState('values'));
  61. $this->insert->values(array('foo' => 'bax'));
  62. $this->assertEquals(array('foo'), $this->insert->getRawState('columns'));
  63. $this->assertEquals(array('bax'), $this->insert->getRawState('values'));
  64. }
  65. /**
  66. * @covers Zend\Db\Sql\Insert::values
  67. */
  68. public function testValuesThrowsExceptionWhenNotArrayOrSelect()
  69. {
  70. $this->setExpectedException(
  71. 'Zend\Db\Sql\Exception\InvalidArgumentException',
  72. 'values() expects an array of values or Zend\Db\Sql\Select instance'
  73. );
  74. $this->insert->values(5);
  75. }
  76. /**
  77. * @covers Zend\Db\Sql\Insert::values
  78. */
  79. public function testValuesThrowsExceptionWhenSelectMergeOverArray()
  80. {
  81. $this->insert->values(array('foo' => 'bar'));
  82. $this->setExpectedException(
  83. 'Zend\Db\Sql\Exception\InvalidArgumentException',
  84. 'A Zend\Db\Sql\Select instance cannot be provided with the merge flag'
  85. );
  86. $this->insert->values(new Select, Insert::VALUES_MERGE);
  87. }
  88. /**
  89. * @covers Zend\Db\Sql\Insert::values
  90. */
  91. public function testValuesThrowsExceptionWhenArrayMergeOverSelect()
  92. {
  93. $this->insert->values(new Select);
  94. $this->setExpectedException(
  95. 'Zend\Db\Sql\Exception\InvalidArgumentException',
  96. 'An array of values cannot be provided with the merge flag when a Zend\Db\Sql\Select instance already exists as the value source'
  97. );
  98. $this->insert->values(array('foo' => 'bar'), Insert::VALUES_MERGE);
  99. }
  100. /**
  101. * @covers Zend\Db\Sql\Insert::values
  102. * @group ZF2-4926
  103. */
  104. public function testEmptyArrayValues()
  105. {
  106. $this->insert->values(array());
  107. $this->assertEquals(array(), $this->readAttribute($this->insert, 'columns'));
  108. }
  109. /**
  110. * @covers Zend\Db\Sql\Insert::prepareStatement
  111. */
  112. public function testPrepareStatement()
  113. {
  114. $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
  115. $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
  116. $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
  117. $mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
  118. $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
  119. $pContainer = new \Zend\Db\Adapter\ParameterContainer(array());
  120. $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
  121. $mockStatement->expects($this->at(1))
  122. ->method('setSql')
  123. ->with($this->equalTo('INSERT INTO "foo" ("bar", "boo") VALUES (?, NOW())'));
  124. $this->insert->into('foo')
  125. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));
  126. $this->insert->prepareStatement($mockAdapter, $mockStatement);
  127. // with TableIdentifier
  128. $this->insert = new Insert;
  129. $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
  130. $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
  131. $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
  132. $mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
  133. $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
  134. $pContainer = new \Zend\Db\Adapter\ParameterContainer(array());
  135. $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
  136. $mockStatement->expects($this->at(1))
  137. ->method('setSql')
  138. ->with($this->equalTo('INSERT INTO "sch"."foo" ("bar", "boo") VALUES (?, NOW())'));
  139. $this->insert->into(new TableIdentifier('foo', 'sch'))
  140. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));
  141. $this->insert->prepareStatement($mockAdapter, $mockStatement);
  142. }
  143. /**
  144. * @covers Zend\Db\Sql\Insert::prepareStatement
  145. */
  146. public function testPrepareStatementWithSelect()
  147. {
  148. $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
  149. $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
  150. $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
  151. $mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
  152. $mockStatement = new \Zend\Db\Adapter\StatementContainer();
  153. $select = new Select('bar');
  154. $this->insert
  155. ->into('foo')
  156. ->columns(array('col1'))
  157. ->select($select->where(array('x'=>5)))
  158. ->prepareStatement($mockAdapter, $mockStatement);
  159. $this->assertEquals(
  160. 'INSERT INTO "foo" ("col1") SELECT "bar".* FROM "bar" WHERE "x" = ?',
  161. $mockStatement->getSql()
  162. );
  163. $parameters = $mockStatement->getParameterContainer()->getNamedArray();
  164. $this->assertSame(array('subselect1where1'=>5), $parameters);
  165. }
  166. /**
  167. * @covers Zend\Db\Sql\Insert::getSqlString
  168. */
  169. public function testGetSqlString()
  170. {
  171. $this->insert->into('foo')
  172. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
  173. $this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam") VALUES (\'baz\', NOW(), NULL)', $this->insert->getSqlString(new TrustingSql92Platform()));
  174. // with TableIdentifier
  175. $this->insert = new Insert;
  176. $this->insert->into(new TableIdentifier('foo', 'sch'))
  177. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
  178. $this->assertEquals('INSERT INTO "sch"."foo" ("bar", "boo", "bam") VALUES (\'baz\', NOW(), NULL)', $this->insert->getSqlString(new TrustingSql92Platform()));
  179. // with Select
  180. $this->insert = new Insert;
  181. $select = new Select();
  182. $this->insert->into('foo')->select($select->from('bar'));
  183. $this->assertEquals('INSERT INTO "foo" SELECT "bar".* FROM "bar"', $this->insert->getSqlString(new TrustingSql92Platform()));
  184. // with Select and columns
  185. $this->insert->columns(array('col1', 'col2'));
  186. $this->assertEquals('INSERT INTO "foo" ("col1", "col2") SELECT "bar".* FROM "bar"', $this->insert->getSqlString(new TrustingSql92Platform()));
  187. }
  188. /**
  189. * @covers Zend\Db\Sql\Insert::__set
  190. */
  191. public function test__set()
  192. {
  193. $this->insert->foo = 'bar';
  194. $this->assertEquals(array('foo'), $this->insert->getRawState('columns'));
  195. $this->assertEquals(array('bar'), $this->insert->getRawState('values'));
  196. }
  197. /**
  198. * @covers Zend\Db\Sql\Insert::__unset
  199. */
  200. public function test__unset()
  201. {
  202. $this->insert->foo = 'bar';
  203. $this->assertEquals(array('foo'), $this->insert->getRawState('columns'));
  204. $this->assertEquals(array('bar'), $this->insert->getRawState('values'));
  205. unset($this->insert->foo);
  206. $this->assertEquals(array(), $this->insert->getRawState('columns'));
  207. $this->assertEquals(array(), $this->insert->getRawState('values'));
  208. }
  209. /**
  210. * @covers Zend\Db\Sql\Insert::__isset
  211. */
  212. public function test__isset()
  213. {
  214. $this->insert->foo = 'bar';
  215. $this->assertTrue(isset($this->insert->foo));
  216. }
  217. /**
  218. * @covers Zend\Db\Sql\Insert::__get
  219. */
  220. public function test__get()
  221. {
  222. $this->insert->foo = 'bar';
  223. $this->assertEquals('bar', $this->insert->foo);
  224. }
  225. /**
  226. * @group ZF2-536
  227. */
  228. public function testValuesMerge()
  229. {
  230. $this->insert->into('foo')
  231. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
  232. $this->insert->into('foo')
  233. ->values(array('qux' => 100), Insert::VALUES_MERGE);
  234. $this->assertEquals('INSERT INTO "foo" ("bar", "boo", "bam", "qux") VALUES (\'baz\', NOW(), NULL, \'100\')', $this->insert->getSqlString(new TrustingSql92Platform()));
  235. }
  236. /**
  237. * @coversNothing
  238. */
  239. public function testSpecificationconstantsCouldBeOverridedByExtensionInPrepareStatement()
  240. {
  241. $replace = new Replace();
  242. $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
  243. $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
  244. $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
  245. $mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
  246. $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
  247. $pContainer = new \Zend\Db\Adapter\ParameterContainer(array());
  248. $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
  249. $mockStatement->expects($this->at(1))
  250. ->method('setSql')
  251. ->with($this->equalTo('REPLACE INTO "foo" ("bar", "boo") VALUES (?, NOW())'));
  252. $replace->into('foo')
  253. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));
  254. $replace->prepareStatement($mockAdapter, $mockStatement);
  255. // with TableIdentifier
  256. $replace = new Replace();
  257. $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
  258. $mockDriver->expects($this->any())->method('getPrepareType')->will($this->returnValue('positional'));
  259. $mockDriver->expects($this->any())->method('formatParameterName')->will($this->returnValue('?'));
  260. $mockAdapter = $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockDriver));
  261. $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
  262. $pContainer = new \Zend\Db\Adapter\ParameterContainer(array());
  263. $mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
  264. $mockStatement->expects($this->at(1))
  265. ->method('setSql')
  266. ->with($this->equalTo('REPLACE INTO "sch"."foo" ("bar", "boo") VALUES (?, NOW())'));
  267. $replace->into(new TableIdentifier('foo', 'sch'))
  268. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));
  269. $replace->prepareStatement($mockAdapter, $mockStatement);
  270. }
  271. /**
  272. * @coversNothing
  273. */
  274. public function testSpecificationconstantsCouldBeOverridedByExtensionInGetSqlString()
  275. {
  276. $replace = new Replace();
  277. $replace->into('foo')
  278. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
  279. $this->assertEquals('REPLACE INTO "foo" ("bar", "boo", "bam") VALUES (\'baz\', NOW(), NULL)', $replace->getSqlString(new TrustingSql92Platform()));
  280. // with TableIdentifier
  281. $replace = new Replace();
  282. $replace->into(new TableIdentifier('foo', 'sch'))
  283. ->values(array('bar' => 'baz', 'boo' => new Expression('NOW()'), 'bam' => null));
  284. $this->assertEquals('REPLACE INTO "sch"."foo" ("bar", "boo", "bam") VALUES (\'baz\', NOW(), NULL)', $replace->getSqlString(new TrustingSql92Platform()));
  285. }
  286. }
  287. class Replace extends Insert
  288. {
  289. const SPECIFICATION_INSERT = 'replace';
  290. protected $specifications = array(
  291. self::SPECIFICATION_INSERT => 'REPLACE INTO %1$s (%2$s) VALUES (%3$s)',
  292. self::SPECIFICATION_SELECT => 'REPLACE INTO %1$s %2$s %3$s',
  293. );
  294. protected function processreplace(\Zend\Db\Adapter\Platform\PlatformInterface $platform, \Zend\Db\Adapter\Driver\DriverInterface $driver = null, \Zend\Db\Adapter\ParameterContainer $parameterContainer = null)
  295. {
  296. return parent::processInsert($platform, $driver, $parameterContainer);
  297. }
  298. }