QueryRegressionTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  63. * Tests that duplicate aliases in contain() can be used, even when they would
  64. * naturally be attached to the query instead of eagerly loaded. What should
  65. * happen here is that One of the duplicates will be changed to be loaded using
  66. * an extra query, but yielding the same results
  67. *
  68. * @return void
  69. */
  70. public function testDuplicateAttachableAliases() {
  71. TableRegistry::get('Stuff', ['table' => 'tags']);
  72. TableRegistry::get('Things', ['table' => 'articles_tags']);
  73. $table = TableRegistry::get('Articles');
  74. $table->belongsTo('Authors');
  75. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  76. $table->Authors->target()->hasOne('Stuff', [
  77. 'foreignKey' => 'id',
  78. 'propertyName' => 'favorite_tag'
  79. ]);
  80. $table->Things->target()->belongsTo('Stuff', [
  81. 'foreignKey' => 'tag_id',
  82. 'propertyName' => 'foo']
  83. );
  84. $results = $table->find()
  85. ->contain(['Authors.Stuff', 'Things.Stuff'])
  86. ->toArray();
  87. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  88. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  89. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  90. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  91. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  92. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  93. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  94. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  95. }
  96. }