StatementDecoratorTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Statement;
  16. use Cake\Database\Statement\StatementDecorator;
  17. use Cake\TestSuite\TestCase;
  18. use \PDO;
  19. /**
  20. * Tests StatementDecorator class
  21. *
  22. */
  23. class StatemetDecoratorTest extends TestCase
  24. {
  25. /**
  26. * Tests that calling lastInsertId will proxy it to
  27. * the driver's lastInsertId method
  28. *
  29. * @return void
  30. */
  31. public function testLastInsertId()
  32. {
  33. $statement = $this->getMock('\PDOStatement');
  34. $driver = $this->getMock('\Cake\Database\Driver');
  35. $statement = new StatementDecorator($statement, $driver);
  36. $driver->expects($this->once())->method('lastInsertId')
  37. ->with('users')
  38. ->will($this->returnValue(2));
  39. $this->assertEquals(2, $statement->lastInsertId('users'));
  40. }
  41. /**
  42. * Tests that calling lastInsertId will get the last insert id by
  43. * column name
  44. *
  45. * @return void
  46. */
  47. public function testLastInsertIdWithReturning()
  48. {
  49. $internal = $this->getMock('\PDOStatement');
  50. $driver = $this->getMock('\Cake\Database\Driver');
  51. $statement = new StatementDecorator($internal, $driver);
  52. $internal->expects($this->once())->method('columnCount')
  53. ->will($this->returnValue(1));
  54. $internal->expects($this->once())->method('fetch')
  55. ->with('assoc')
  56. ->will($this->returnValue(['id' => 2]));
  57. $driver->expects($this->never())->method('lastInsertId');
  58. $this->assertEquals(2, $statement->lastInsertId('users', 'id'));
  59. }
  60. /**
  61. * Tests that the statement will not be executed twice if the iterator
  62. * is requested more than once
  63. *
  64. * @return void
  65. */
  66. public function testNoDoubleExecution()
  67. {
  68. $inner = $this->getMock('\PDOStatement');
  69. $driver = $this->getMock('\Cake\Database\Driver');
  70. $statement = new StatementDecorator($inner, $driver);
  71. $inner->expects($this->once())->method('execute');
  72. $this->assertSame($inner, $statement->getIterator());
  73. $this->assertSame($inner, $statement->getIterator());
  74. }
  75. }