StatementDecoratorTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * @return void
  30. */
  31. public function testLastInsertId()
  32. {
  33. $statement = $this->getMockBuilder(StatementInterface::class)->getMock();
  34. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  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->getMockBuilder(StatementInterface::class)->getMock();
  50. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  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->getMockBuilder(StatementInterface::class)->getMock();
  69. $driver = $this->getMockBuilder('Cake\Database\DriverInterface')->getMock();
  70. $statement = new StatementDecorator($inner, $driver);
  71. $inner->expects($this->once())
  72. ->method('execute')
  73. ->will($this->returnValue(true));
  74. $this->assertSame($inner, $statement->getIterator());
  75. $this->assertSame($inner, $statement->getIterator());
  76. }
  77. }