StatementDecoratorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  19. * Tests StatementDecorator class
  20. */
  21. class StatemetDecoratorTest 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. }