StatementDecoratorTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Database\StatementInterface;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests StatementDecorator class
  22. */
  23. class StatementDecoratorTest extends TestCase
  24. {
  25. /**
  26. * Tests that calling lastInsertId will proxy it to
  27. * the driver's lastInsertId method
  28. */
  29. public function testLastInsertId(): void
  30. {
  31. $statement = $this->getMockBuilder(StatementInterface::class)->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->assertSame(2, $statement->lastInsertId('users'));
  38. }
  39. /**
  40. * Tests that calling lastInsertId will get the last insert id by
  41. * column name
  42. */
  43. public function testLastInsertIdWithReturning(): void
  44. {
  45. $internal = $this->getMockBuilder(StatementInterface::class)->getMock();
  46. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  47. $statement = new StatementDecorator($internal, $driver);
  48. $internal->expects($this->once())->method('columnCount')
  49. ->will($this->returnValue(1));
  50. $internal->expects($this->once())->method('fetch')
  51. ->with('assoc')
  52. ->will($this->returnValue(['id' => 2]));
  53. $driver->expects($this->never())->method('lastInsertId');
  54. $this->assertSame(2, $statement->lastInsertId('users', 'id'));
  55. }
  56. /**
  57. * Tests that the statement will not be executed twice if the iterator
  58. * is requested more than once
  59. */
  60. public function testNoDoubleExecution(): void
  61. {
  62. $inner = $this->getMockBuilder(StatementInterface::class)->getMock();
  63. $driver = $this->getMockBuilder('Cake\Database\DriverInterface')->getMock();
  64. $statement = new StatementDecorator($inner, $driver);
  65. $inner->expects($this->once())
  66. ->method('execute')
  67. ->will($this->returnValue(true));
  68. $this->assertSame($inner, $statement->getIterator());
  69. $this->assertSame($inner, $statement->getIterator());
  70. }
  71. }