StatementDecoratorTest.php 2.8 KB

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