QueryRegressionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. use Cake\Utility\Time;
  21. /**
  22. * Contains regression test for the Query builder
  23. *
  24. */
  25. class QueryRegressionTest extends TestCase {
  26. /**
  27. * Fixture to be used
  28. *
  29. * @var array
  30. */
  31. public $fixtures = ['core.user', 'core.article', 'core.tag', 'core.articles_tag', 'core.author'];
  32. /**
  33. * Tear down
  34. *
  35. * @return void
  36. */
  37. public function tearDown() {
  38. parent::tearDown();
  39. TableRegistry::clear();
  40. }
  41. /**
  42. * Test for https://github.com/cakephp/cakephp/issues/3087
  43. *
  44. * @return void
  45. */
  46. public function testSelectTimestampColumn() {
  47. $table = TableRegistry::get('users');
  48. $user = $table->find()->where(['id' => 1])->first();
  49. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  50. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  51. }
  52. /**
  53. * Tests that EagerLoader does not try to create queries for associations having no
  54. * keys to compare against
  55. *
  56. * @return void
  57. */
  58. public function testEagerLoadingFromEmptyResults() {
  59. $table = TableRegistry::get('Articles');
  60. $table->belongsToMany('ArticlesTags');
  61. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  62. $this->assertEmpty($results);
  63. }
  64. /**
  65. * Tests that duplicate aliases in contain() can be used, even when they would
  66. * naturally be attached to the query instead of eagerly loaded. What should
  67. * happen here is that One of the duplicates will be changed to be loaded using
  68. * an extra query, but yielding the same results
  69. *
  70. * @return void
  71. */
  72. public function testDuplicateAttachableAliases() {
  73. TableRegistry::get('Stuff', ['table' => 'tags']);
  74. TableRegistry::get('Things', ['table' => 'articles_tags']);
  75. $table = TableRegistry::get('Articles');
  76. $table->belongsTo('Authors');
  77. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  78. $table->Authors->target()->hasOne('Stuff', [
  79. 'foreignKey' => 'id',
  80. 'propertyName' => 'favorite_tag'
  81. ]);
  82. $table->Things->target()->belongsTo('Stuff', [
  83. 'foreignKey' => 'tag_id',
  84. 'propertyName' => 'foo']
  85. );
  86. $results = $table->find()
  87. ->contain(['Authors.Stuff', 'Things.Stuff'])
  88. ->toArray();
  89. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  90. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  91. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  92. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  93. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  94. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  95. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  96. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  97. }
  98. }