QueryRegressionTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Query;
  17. use Cake\ORM\Table;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Contains regression test for the Query builder
  22. *
  23. */
  24. class QueryRegressionTest extends TestCase {
  25. /**
  26. * Fixture to be used
  27. *
  28. * @var array
  29. */
  30. public $fixtures = ['core.user', 'core.article', 'core.tag', 'core.articles_tag', 'core.author'];
  31. /**
  32. * Tear down
  33. *
  34. * @return void
  35. */
  36. public function tearDown() {
  37. TableRegistry::clear();
  38. }
  39. /**
  40. * Test for https://github.com/cakephp/cakephp/issues/3087
  41. *
  42. * @return void
  43. */
  44. public function testSelectTimestampColumn() {
  45. $table = TableRegistry::get('users');
  46. $user = $table->find()->where(['id' => 1])->first();
  47. $this->assertEquals(new \DateTime('2007-03-17 01:16:23'), $user->created);
  48. $this->assertEquals(new \DateTime('2007-03-17 01:18:31'), $user->updated);
  49. }
  50. /**
  51. * Tests that EagerLoader does not try to create queries for associations having no
  52. * keys to compare against
  53. *
  54. * @return void
  55. */
  56. public function testEagerLoadingFromEmptyResults() {
  57. $table = TableRegistry::get('Articles');
  58. $table->belongsToMany('ArticlesTags');
  59. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  60. $this->assertEmpty($results);
  61. }
  62. public function testDuplicateAttachableAliases() {
  63. TableRegistry::get('Stuff', ['table' => 'tags']);
  64. TableRegistry::get('Things', ['table' => 'articles_tags']);
  65. $table = TableRegistry::get('Articles');
  66. $table->belongsTo('Authors');
  67. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  68. $table->Authors->target()->hasOne('Stuff', [
  69. 'foreignKey' => 'id',
  70. 'propertyName' => 'favorite_tag'
  71. ]);
  72. $table->Things->target()->belongsTo('Stuff', [
  73. 'foreignKey' => 'tag_id',
  74. 'propertyName' => 'foo']
  75. );
  76. $results = $table->find()
  77. ->contain(['Authors.Stuff', 'Things.Stuff'])
  78. ->hydrate(false)
  79. ->toArray();
  80. }
  81. }