StatementDecoratorTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. /**
  19. * Tests StatementDecorator class
  20. */
  21. class StatementDecoratorTest extends TestCase
  22. {
  23. /**
  24. * Tests that calling lastInsertId will proxy it to
  25. * the driver's lastInsertId method
  26. *
  27. * @return void
  28. */
  29. public function testLastInsertId()
  30. {
  31. $statement = $this->getMockBuilder('\PDOStatement')->getMock();
  32. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  33. $statement = new StatementDecorator($statement, $driver);
  34. $driver->expects($this->once())->method('lastInsertId')
  35. ->with('users')
  36. ->will($this->returnValue(2));
  37. $this->assertEquals(2, $statement->lastInsertId('users'));
  38. }
  39. /**
  40. * Tests that calling lastInsertId will get the last insert id by
  41. * column name
  42. *
  43. * @return void
  44. */
  45. public function testLastInsertIdWithReturning()
  46. {
  47. $internal = $this->getMockBuilder('\PDOStatement')->getMock();
  48. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  49. $statement = new StatementDecorator($internal, $driver);
  50. $internal->expects($this->once())->method('columnCount')
  51. ->will($this->returnValue(1));
  52. $internal->expects($this->once())->method('fetch')
  53. ->with('assoc')
  54. ->will($this->returnValue(['id' => 2]));
  55. $driver->expects($this->never())->method('lastInsertId');
  56. $this->assertEquals(2, $statement->lastInsertId('users', 'id'));
  57. }
  58. /**
  59. * Tests that the statement will not be executed twice if the iterator
  60. * is requested more than once
  61. *
  62. * @return void
  63. */
  64. public function testNoDoubleExecution()
  65. {
  66. $inner = $this->getMockBuilder('\PDOStatement')->getMock();
  67. $driver = $this->getMockBuilder('\Cake\Database\Driver')->getMock();
  68. $statement = new StatementDecorator($inner, $driver);
  69. $inner->expects($this->once())->method('execute');
  70. $this->assertSame($inner, $statement->getIterator());
  71. $this->assertSame($inner, $statement->getIterator());
  72. }
  73. }