QueryRegressionTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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\ORM;
  16. use Cake\Core\Plugin;
  17. use Cake\I18n\Time;
  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. /**
  27. * Fixture to be used
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.articles',
  33. 'core.tags',
  34. 'core.articles_tags',
  35. 'core.authors',
  36. 'core.authors_tags',
  37. 'core.comments',
  38. 'core.featured_tags',
  39. 'core.special_tags',
  40. 'core.tags_translations',
  41. 'core.translates',
  42. 'core.users'
  43. ];
  44. public $autoFixtures = false;
  45. /**
  46. * Tear down
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Test for https://github.com/cakephp/cakephp/issues/3087
  57. *
  58. * @return void
  59. */
  60. public function testSelectTimestampColumn()
  61. {
  62. $this->loadFixtures('Users');
  63. $table = TableRegistry::get('users');
  64. $user = $table->find()->where(['id' => 1])->first();
  65. $this->assertEquals(new Time('2007-03-17 01:16:23'), $user->created);
  66. $this->assertEquals(new Time('2007-03-17 01:18:31'), $user->updated);
  67. }
  68. /**
  69. * Tests that EagerLoader does not try to create queries for associations having no
  70. * keys to compare against
  71. *
  72. * @return void
  73. */
  74. public function testEagerLoadingFromEmptyResults()
  75. {
  76. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  77. $table = TableRegistry::get('Articles');
  78. $table->belongsToMany('ArticlesTags');
  79. $results = $table->find()->where(['id >' => 100])->contain('ArticlesTags')->toArray();
  80. $this->assertEmpty($results);
  81. }
  82. /**
  83. * Tests that eagerloading belongsToMany with find list fails with a helpful message.
  84. *
  85. * @expectedException \InvalidArgumentException
  86. * @return void
  87. */
  88. public function testEagerLoadingBelongsToManyList()
  89. {
  90. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  91. $table = TableRegistry::get('Articles');
  92. $table->belongsToMany('Tags', [
  93. 'finder' => 'list'
  94. ]);
  95. $table->find()->contain('Tags')->toArray();
  96. }
  97. /**
  98. * Tests that duplicate aliases in contain() can be used, even when they would
  99. * naturally be attached to the query instead of eagerly loaded. What should
  100. * happen here is that One of the duplicates will be changed to be loaded using
  101. * an extra query, but yielding the same results
  102. *
  103. * @return void
  104. */
  105. public function testDuplicateAttachableAliases()
  106. {
  107. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  108. TableRegistry::get('Stuff', ['table' => 'tags']);
  109. TableRegistry::get('Things', ['table' => 'articles_tags']);
  110. $table = TableRegistry::get('Articles');
  111. $table->belongsTo('Authors');
  112. $table->hasOne('Things', ['propertyName' => 'articles_tag']);
  113. $table->Authors->target()->hasOne('Stuff', [
  114. 'foreignKey' => 'id',
  115. 'propertyName' => 'favorite_tag'
  116. ]);
  117. $table->Things->target()->belongsTo('Stuff', [
  118. 'foreignKey' => 'tag_id',
  119. 'propertyName' => 'foo'
  120. ]);
  121. $results = $table->find()
  122. ->contain(['Authors.Stuff', 'Things.Stuff'])
  123. ->order(['Articles.id' => 'ASC'])
  124. ->toArray();
  125. $this->assertCount(5, $results);
  126. $this->assertEquals(1, $results[0]->articles_tag->foo->id);
  127. $this->assertEquals(1, $results[0]->author->favorite_tag->id);
  128. $this->assertEquals(2, $results[1]->articles_tag->foo->id);
  129. $this->assertEquals(1, $results[2]->articles_tag->foo->id);
  130. $this->assertEquals(3, $results[2]->author->favorite_tag->id);
  131. $this->assertEquals(3, $results[3]->articles_tag->foo->id);
  132. $this->assertEquals(3, $results[3]->author->favorite_tag->id);
  133. }
  134. /**
  135. * Test for https://github.com/cakephp/cakephp/issues/3410
  136. *
  137. * @return void
  138. */
  139. public function testNullableTimeColumn()
  140. {
  141. $this->loadFixtures('Users');
  142. $table = TableRegistry::get('users');
  143. $entity = $table->newEntity(['username' => 'derp', 'created' => null]);
  144. $this->assertSame($entity, $table->save($entity));
  145. $this->assertNull($entity->created);
  146. }
  147. /**
  148. * Test for https://github.com/cakephp/cakephp/issues/3626
  149. *
  150. * Checks that join data is actually created and not tried to be updated every time
  151. * @return void
  152. */
  153. public function testCreateJointData()
  154. {
  155. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  156. $articles = TableRegistry::get('Articles');
  157. $articles->belongsToMany('Highlights', [
  158. 'className' => 'TestApp\Model\Table\TagsTable',
  159. 'foreignKey' => 'article_id',
  160. 'targetForeignKey' => 'tag_id',
  161. 'through' => 'SpecialTags'
  162. ]);
  163. $entity = $articles->get(2);
  164. $data = [
  165. 'id' => 2,
  166. 'highlights' => [
  167. [
  168. 'name' => 'New Special Tag',
  169. '_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00']
  170. ]
  171. ]
  172. ];
  173. $entity = $articles->patchEntity($entity, $data, ['Highlights._joinData']);
  174. $articles->save($entity);
  175. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  176. $this->assertEquals(4, $entity->highlights[0]->_joinData->tag_id);
  177. $this->assertEquals('2014-06-01', $entity->highlights[0]->_joinData->highlighted_time->format('Y-m-d'));
  178. }
  179. /**
  180. * Tests that the junction table instance taken from both sides of a belongsToMany
  181. * relationship is actually the same object.
  182. *
  183. * @return void
  184. */
  185. public function testReciprocalBelongsToMany()
  186. {
  187. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  188. $articles = TableRegistry::get('Articles');
  189. $tags = TableRegistry::get('Tags');
  190. $articles->belongsToMany('Tags');
  191. $tags->belongsToMany('Articles');
  192. $left = $articles->Tags->junction();
  193. $right = $tags->Articles->junction();
  194. $this->assertSame($left, $right);
  195. }
  196. /**
  197. * Test for https://github.com/cakephp/cakephp/issues/4253
  198. *
  199. * Makes sure that the belongsToMany association is not overwritten with conflicting information
  200. * by any of the sides when the junction() function is invoked
  201. *
  202. * @return void
  203. */
  204. public function testReciprocalBelongsToManyNoOverwrite()
  205. {
  206. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  207. $articles = TableRegistry::get('Articles');
  208. $tags = TableRegistry::get('Tags');
  209. $articles->belongsToMany('Tags');
  210. $tags->belongsToMany('Articles');
  211. $sub = $articles->Tags->find()->select(['Tags.id'])->matching('Articles', function ($q) {
  212. return $q->where(['Articles.id' => 1]);
  213. });
  214. $query = $articles->Tags->find()->where(['Tags.id NOT IN' => $sub]);
  215. $this->assertEquals(1, $query->count());
  216. }
  217. /**
  218. * Returns an array with the saving strategies for a belongsTo association
  219. *
  220. * @return array
  221. */
  222. public function strategyProvider()
  223. {
  224. return [['append', 'replace']];
  225. }
  226. /**
  227. * Test for https://github.com/cakephp/cakephp/issues/3677 and
  228. * https://github.com/cakephp/cakephp/issues/3714
  229. *
  230. * Checks that only relevant associations are passed when saving _joinData
  231. * Tests that _joinData can also save deeper associations
  232. *
  233. * @dataProvider strategyProvider
  234. * @param string $strategy
  235. * @return void
  236. */
  237. public function testBelongsToManyDeepSave($strategy)
  238. {
  239. $this->loadFixtures('Articles', 'Tags', 'SpecialTags', 'Authors');
  240. $articles = TableRegistry::get('Articles');
  241. $articles->belongsToMany('Highlights', [
  242. 'className' => 'TestApp\Model\Table\TagsTable',
  243. 'foreignKey' => 'article_id',
  244. 'targetForeignKey' => 'tag_id',
  245. 'through' => 'SpecialTags',
  246. 'saveStrategy' => $strategy
  247. ]);
  248. $articles->Highlights->junction()->belongsTo('Authors');
  249. $articles->Highlights->hasOne('Authors', [
  250. 'foreignKey' => 'id'
  251. ]);
  252. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  253. $data = [
  254. 'highlights' => [
  255. [
  256. 'name' => 'New Special Tag',
  257. '_joinData' => [
  258. 'highlighted' => true,
  259. 'highlighted_time' => '2014-06-01 10:10:00',
  260. 'author' => [
  261. 'name' => 'mariano'
  262. ]
  263. ],
  264. 'author' => ['name' => 'mark']
  265. ]
  266. ]
  267. ];
  268. $options = [
  269. 'associated' => [
  270. 'Highlights._joinData.Authors', 'Highlights.Authors'
  271. ]
  272. ];
  273. $entity = $articles->patchEntity($entity, $data, $options);
  274. $articles->save($entity, $options);
  275. $entity = $articles->get(2, [
  276. 'contain' => [
  277. 'SpecialTags' => ['sort' => ['SpecialTags.id' => 'ASC']],
  278. 'SpecialTags.Authors',
  279. 'Highlights.Authors'
  280. ]
  281. ]);
  282. $this->assertEquals('mark', end($entity->highlights)->author->name);
  283. $lastTag = end($entity->special_tags);
  284. $this->assertTrue($lastTag->highlighted);
  285. $this->assertEquals('2014-06-01 10:10:00', $lastTag->highlighted_time->format('Y-m-d H:i:s'));
  286. $this->assertEquals('mariano', $lastTag->author->name);
  287. }
  288. /**
  289. * Tests that no exceptions are generated becuase of ambiguous column names in queries
  290. * during a save operation
  291. *
  292. * @see https://github.com/cakephp/cakephp/issues/3803
  293. * @return void
  294. */
  295. public function testSaveWithCallbacks()
  296. {
  297. $this->loadFixtures('Articles', 'Authors');
  298. $articles = TableRegistry::get('Articles');
  299. $articles->belongsTo('Authors');
  300. $articles->eventManager()->attach(function ($event, $query) {
  301. return $query->contain('Authors');
  302. }, 'Model.beforeFind');
  303. $article = $articles->newEntity();
  304. $article->title = 'Foo';
  305. $article->body = 'Bar';
  306. $this->assertSame($article, $articles->save($article));
  307. }
  308. /**
  309. * Test that save() works with entities containing expressions
  310. * as properties.
  311. *
  312. * @return void
  313. */
  314. public function testSaveWithExpressionProperty()
  315. {
  316. $this->loadFixtures('Articles');
  317. $articles = TableRegistry::get('Articles');
  318. $article = $articles->newEntity();
  319. $article->title = new \Cake\Database\Expression\QueryExpression("SELECT 'jose'");
  320. $this->assertSame($article, $articles->save($article));
  321. }
  322. /**
  323. * Tests that whe saving deep associations for a belongsToMany property,
  324. * data is not removed becuase of excesive associations filtering.
  325. *
  326. * @see https://github.com/cakephp/cakephp/issues/4009
  327. * @return void
  328. */
  329. public function testBelongsToManyDeepSave2()
  330. {
  331. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  332. $articles = TableRegistry::get('Articles');
  333. $articles->belongsToMany('Highlights', [
  334. 'className' => 'TestApp\Model\Table\TagsTable',
  335. 'foreignKey' => 'article_id',
  336. 'targetForeignKey' => 'tag_id',
  337. 'through' => 'SpecialTags',
  338. ]);
  339. $articles->Highlights->hasMany('TopArticles', [
  340. 'className' => 'TestApp\Model\Table\ArticlesTable',
  341. 'foreignKey' => 'author_id',
  342. ]);
  343. $entity = $articles->get(2, ['contain' => ['Highlights']]);
  344. $data = [
  345. 'highlights' => [
  346. [
  347. 'name' => 'New Special Tag',
  348. '_joinData' => [
  349. 'highlighted' => true,
  350. 'highlighted_time' => '2014-06-01 10:10:00',
  351. ],
  352. 'top_articles' => [
  353. ['title' => 'First top article'],
  354. ['title' => 'Second top article'],
  355. ]
  356. ]
  357. ]
  358. ];
  359. $options = [
  360. 'associated' => [
  361. 'Highlights._joinData', 'Highlights.TopArticles'
  362. ]
  363. ];
  364. $entity = $articles->patchEntity($entity, $data, $options);
  365. $articles->save($entity, $options);
  366. $entity = $articles->get(2, [
  367. 'contain' => [
  368. 'Highlights.TopArticles'
  369. ]
  370. ]);
  371. $highlights = $entity->highlights[0];
  372. $this->assertEquals('First top article', $highlights->top_articles[0]->title);
  373. $this->assertEquals('Second top article', $highlights->top_articles[1]->title);
  374. $this->assertEquals(
  375. new Time('2014-06-01 10:10:00'),
  376. $highlights->_joinData->highlighted_time
  377. );
  378. }
  379. /**
  380. * An integration test that spot checks that associations use the
  381. * correct alias names to generate queries.
  382. *
  383. * @return void
  384. */
  385. public function testPluginAssociationQueryGeneration()
  386. {
  387. $this->loadFixtures('Articles', 'Comments', 'Authors');
  388. Plugin::load('TestPlugin');
  389. $articles = TableRegistry::get('Articles');
  390. $articles->hasMany('TestPlugin.Comments');
  391. $articles->belongsTo('TestPlugin.Authors');
  392. $result = $articles->find()
  393. ->where(['Articles.id' => 2])
  394. ->contain(['Comments', 'Authors'])
  395. ->first();
  396. $this->assertNotEmpty(
  397. $result->comments[0]->id,
  398. 'No SQL error and comment exists.'
  399. );
  400. $this->assertNotEmpty(
  401. $result->author->id,
  402. 'No SQL error and author exists.'
  403. );
  404. }
  405. /**
  406. * Tests that loading associations having the same alias in the
  407. * joinable associations chain is not sensitive to the order in which
  408. * the associations are selected.
  409. *
  410. * @see https://github.com/cakephp/cakephp/issues/4454
  411. * @return void
  412. */
  413. public function testAssociationChainOrder()
  414. {
  415. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags', 'Authors');
  416. $articles = TableRegistry::get('Articles');
  417. $articles->belongsTo('Authors');
  418. $articles->hasOne('ArticlesTags');
  419. $articlesTags = TableRegistry::get('ArticlesTags');
  420. $articlesTags->belongsTo('Authors', [
  421. 'foreignKey' => 'tag_id'
  422. ]);
  423. $resultA = $articles->find()
  424. ->contain(['ArticlesTags.Authors', 'Authors'])
  425. ->first();
  426. $resultB = $articles->find()
  427. ->contain(['Authors', 'ArticlesTags.Authors'])
  428. ->first();
  429. $this->assertEquals($resultA, $resultB);
  430. $this->assertNotEmpty($resultA->author);
  431. $this->assertNotEmpty($resultA->articles_tag->author);
  432. }
  433. /**
  434. * Test that offset/limit are elided from subquery loads.
  435. *
  436. * @return void
  437. */
  438. public function testAssociationSubQueryNoOffset()
  439. {
  440. $this->loadFixtures('Articles', 'Translates');
  441. $table = TableRegistry::get('Articles');
  442. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  443. $table->locale('eng');
  444. $query = $table->find('translations')
  445. ->order(['Articles.id' => 'ASC'])
  446. ->limit(10)
  447. ->offset(1);
  448. $result = $query->toArray();
  449. $this->assertCount(2, $result);
  450. }
  451. /**
  452. * Tests that using the subquery strategy in a deep association returns the right results
  453. *
  454. * @see https://github.com/cakephp/cakephp/issues/4484
  455. * @return void
  456. */
  457. public function testDeepBelongsToManySubqueryStrategy()
  458. {
  459. $this->loadFixtures('Authors', 'Tags', 'Articles', 'ArticlesTags');
  460. $table = TableRegistry::get('Authors');
  461. $table->hasMany('Articles');
  462. $table->Articles->belongsToMany('Tags', [
  463. 'strategy' => 'subquery'
  464. ]);
  465. $result = $table->find()->contain(['Articles.Tags'])->toArray();
  466. $this->assertEquals(
  467. ['tag1', 'tag3'],
  468. collection($result[2]->articles[0]->tags)->extract('name')->toArray()
  469. );
  470. }
  471. /**
  472. * Tests that using the subquery strategy in a deep association returns the right results
  473. *
  474. * @see https://github.com/cakephp/cakephp/issues/5769
  475. * @return void
  476. */
  477. public function testDeepBelongsToManySubqueryStrategy2()
  478. {
  479. $this->loadFixtures('Articles', 'Authors', 'Tags', 'Authors', 'AuthorsTags');
  480. $table = TableRegistry::get('Authors');
  481. $table->hasMany('Articles');
  482. $table->Articles->belongsToMany('Tags', [
  483. 'strategy' => 'subquery'
  484. ]);
  485. $table->belongsToMany('Tags', [
  486. 'strategy' => 'subquery',
  487. ]);
  488. $table->Articles->belongsTo('Authors');
  489. $result = $table->Articles->find()
  490. ->where(['Authors.id >' => 1])
  491. ->contain(['Authors.Tags'])
  492. ->toArray();
  493. $this->assertEquals(
  494. ['tag1', 'tag2'],
  495. collection($result[0]->author->tags)->extract('name')->toArray()
  496. );
  497. $this->assertEquals(3, $result[0]->author->id);
  498. }
  499. /**
  500. * Tests that finding on a table with a primary key other than `id` will work
  501. * seamlessly with either select or subquery.
  502. *
  503. * @see https://github.com/cakephp/cakephp/issues/6781
  504. * @return void
  505. */
  506. public function testDeepHasManyEitherStrategy()
  507. {
  508. $this->loadFixtures('Tags', 'FeaturedTags', 'TagsTranslations');
  509. $tags = TableRegistry::get('Tags');
  510. $featuredTags = TableRegistry::get('FeaturedTags');
  511. $featuredTags->belongsTo('Tags');
  512. $tags->hasMany('TagsTranslations', [
  513. 'foreignKey' => 'id',
  514. 'strategy' => 'select'
  515. ]);
  516. $findViaSelect = $featuredTags
  517. ->find()
  518. ->where(['FeaturedTags.tag_id' => 2])
  519. ->contain('Tags.TagsTranslations');
  520. $tags->hasMany('TagsTranslations', [
  521. 'foreignKey' => 'id',
  522. 'strategy' => 'subquery'
  523. ]);
  524. $findViaSubquery = $featuredTags
  525. ->find()
  526. ->where(['FeaturedTags.tag_id' => 2])
  527. ->contain('Tags.TagsTranslations');
  528. $expected = [2 => 'tag 2 translated into en_us'];
  529. $this->assertEquals($expected, $findViaSelect->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  530. $this->assertEquals($expected, $findViaSubquery->combine('tag_id', 'tag.tags_translations.0.name')->toArray());
  531. }
  532. /**
  533. * Tests that getting the count of a query having containments return
  534. * the correct results
  535. *
  536. * @see https://github.com/cakephp/cakephp/issues/4511
  537. * @return void
  538. */
  539. public function testCountWithContain()
  540. {
  541. $this->loadFixtures('Articles', 'Authors');
  542. $table = TableRegistry::get('Articles');
  543. $table->belongsTo('Authors', ['joinType' => 'inner']);
  544. $count = $table
  545. ->find()
  546. ->contain(['Authors' => function ($q) {
  547. return $q->where(['Authors.id' => 1]);
  548. }])
  549. ->count();
  550. $this->assertEquals(2, $count);
  551. }
  552. /**
  553. * Tests that getting the count of a query with bind is correct
  554. *
  555. * @see https://github.com/cakephp/cakephp/issues/8466
  556. * @return void
  557. */
  558. public function testCountWithBind()
  559. {
  560. $this->loadFixtures('Articles');
  561. $table = TableRegistry::get('Articles');
  562. $query = $table
  563. ->find()
  564. ->select(['title', 'id'])
  565. ->where("title LIKE :val")
  566. ->group(['id', 'title'])
  567. ->bind(':val', '%Second%');
  568. $count = $query->count();
  569. $this->assertEquals(1, $count);
  570. }
  571. /**
  572. * Tests that bind in subqueries works.
  573. *
  574. * @return void
  575. */
  576. public function testSubqueryBind()
  577. {
  578. $this->loadFixtures('Articles');
  579. $table = TableRegistry::get('Articles');
  580. $sub = $table->find()
  581. ->select(['id'])
  582. ->where("title LIKE :val")
  583. ->bind(':val', 'Second %');
  584. $query = $table
  585. ->find()
  586. ->select(['title'])
  587. ->where(["id NOT IN" => $sub]);
  588. $result = $query->toArray();
  589. $this->assertCount(2, $result);
  590. $this->assertEquals('First Article', $result[0]->title);
  591. $this->assertEquals('Third Article', $result[1]->title);
  592. }
  593. /**
  594. * Test that deep containments don't generate empty entities for
  595. * intermediary relations.
  596. *
  597. * @return void
  598. */
  599. public function testContainNoEmptyAssociatedObjects()
  600. {
  601. $this->loadFixtures('Comments', 'Users', 'Articles');
  602. $comments = TableRegistry::get('Comments');
  603. $comments->belongsTo('Users');
  604. $users = TableRegistry::get('Users');
  605. $users->hasMany('Articles', [
  606. 'foreignKey' => 'author_id'
  607. ]);
  608. $comments->updateAll(['user_id' => 99], ['id' => 1]);
  609. $result = $comments->find()
  610. ->contain(['Users'])
  611. ->where(['Comments.id' => 1])
  612. ->first();
  613. $this->assertNull($result->user, 'No record should be null.');
  614. $result = $comments->find()
  615. ->contain(['Users', 'Users.Articles'])
  616. ->where(['Comments.id' => 1])
  617. ->first();
  618. $this->assertNull($result->user, 'No record should be null.');
  619. }
  620. /**
  621. * Tests that using a comparison expression inside an OR condition works
  622. *
  623. * @see https://github.com/cakephp/cakephp/issues/5081
  624. * @return void
  625. */
  626. public function testOrConditionsWithExpression()
  627. {
  628. $this->loadFixtures('Articles');
  629. $table = TableRegistry::get('Articles');
  630. $query = $table->find();
  631. $query->where([
  632. 'OR' => [
  633. new \Cake\Database\Expression\Comparison('id', 1, 'integer', '>'),
  634. new \Cake\Database\Expression\Comparison('id', 3, 'integer', '<')
  635. ]
  636. ]);
  637. $results = $query->toArray();
  638. $this->assertCount(3, $results);
  639. }
  640. /**
  641. * Tests that calling count on a query having a union works correctly
  642. *
  643. * @see https://github.com/cakephp/cakephp/issues/5107
  644. * @return void
  645. */
  646. public function testCountWithUnionQuery()
  647. {
  648. $this->loadFixtures('Articles');
  649. $table = TableRegistry::get('Articles');
  650. $query = $table->find()->where(['id' => 1]);
  651. $query2 = $table->find()->where(['id' => 2]);
  652. $query->union($query2);
  653. $this->assertEquals(2, $query->count());
  654. }
  655. /**
  656. * Integration test when selecting no fields on the primary table.
  657. *
  658. * @return void
  659. */
  660. public function testSelectNoFieldsOnPrimaryAlias()
  661. {
  662. $this->loadFixtures('Articles', 'Users');
  663. $table = TableRegistry::get('Articles');
  664. $table->belongsTo('Users');
  665. $query = $table->find()
  666. ->select(['Users__id' => 'id']);
  667. $results = $query->toArray();
  668. $this->assertCount(3, $results);
  669. }
  670. /**
  671. * Tests that calling first on the query results will not remove all other results
  672. * from the set.
  673. *
  674. * @return void
  675. */
  676. public function testFirstOnResultSet()
  677. {
  678. $this->loadFixtures('Articles');
  679. $results = TableRegistry::get('Articles')->find()->all();
  680. $this->assertEquals(3, $results->count());
  681. $this->assertNotNull($results->first());
  682. $this->assertCount(3, $results->toArray());
  683. }
  684. /**
  685. * Checks that matching and contain can be called for the same belongsTo association
  686. *
  687. * @see https://github.com/cakephp/cakephp/issues/5463
  688. * @return void
  689. */
  690. public function testFindMatchingAndContain()
  691. {
  692. $this->loadFixtures('Articles', 'Authors');
  693. $table = TableRegistry::get('Articles');
  694. $table->belongsTo('Authors');
  695. $article = $table->find()
  696. ->contain('Authors')
  697. ->matching('Authors', function ($q) {
  698. return $q->where(['Authors.id' => 1]);
  699. })
  700. ->first();
  701. $this->assertNotNull($article->author);
  702. $this->assertEquals($article->author, $article->_matchingData['Authors']);
  703. }
  704. /**
  705. * Checks that matching and contain can be called for the same belongsTo association
  706. *
  707. * @see https://github.com/cakephp/cakephp/issues/5463
  708. * @return void
  709. */
  710. public function testFindMatchingAndContainWithSubquery()
  711. {
  712. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  713. $table = TableRegistry::get('authors');
  714. $table->hasMany('articles', ['strategy' => 'subquery']);
  715. $table->articles->belongsToMany('tags');
  716. $result = $table->find()
  717. ->matching('articles.tags', function ($q) {
  718. return $q->where(['tags.id' => 2]);
  719. })
  720. ->contain('articles');
  721. $this->assertCount(2, $result->first()->articles);
  722. }
  723. /**
  724. * Tests that matching does not overwrite associations in contain
  725. *
  726. * @see https://github.com/cakephp/cakephp/issues/5584
  727. * @return void
  728. */
  729. public function testFindMatchingOverwrite()
  730. {
  731. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags');
  732. $comments = TableRegistry::get('Comments');
  733. $comments->belongsTo('Articles');
  734. $articles = TableRegistry::get('Articles');
  735. $articles->belongsToMany('Tags');
  736. $result = $comments
  737. ->find()
  738. ->matching('Articles.Tags', function ($q) {
  739. return $q->where(['Tags.id' => 2]);
  740. })
  741. ->contain('Articles')
  742. ->first();
  743. $this->assertEquals(1, $result->id);
  744. $this->assertEquals(1, $result->_matchingData['Articles']->id);
  745. $this->assertEquals(2, $result->_matchingData['Tags']->id);
  746. $this->assertNotNull($result->article);
  747. $this->assertEquals($result->article, $result->_matchingData['Articles']);
  748. }
  749. /**
  750. * Tests that matching does not overwrite associations in contain
  751. *
  752. * @see https://github.com/cakephp/cakephp/issues/5584
  753. * @return void
  754. */
  755. public function testFindMatchingOverwrite2()
  756. {
  757. $this->loadFixtures('Articles', 'Comments', 'Tags', 'ArticlesTags', 'Authors');
  758. $comments = TableRegistry::get('Comments');
  759. $comments->belongsTo('Articles');
  760. $articles = TableRegistry::get('Articles');
  761. $articles->belongsTo('Authors');
  762. $articles->belongsToMany('Tags');
  763. $result = $comments
  764. ->find()
  765. ->matching('Articles.Tags', function ($q) {
  766. return $q->where(['Tags.id' => 2]);
  767. })
  768. ->contain('Articles.Authors')
  769. ->first();
  770. $this->assertNotNull($result->article->author);
  771. }
  772. /**
  773. * Tests that trying to contain an inexistent association
  774. * throws an exception and not a fatal error.
  775. *
  776. * @expectedException InvalidArgumentException
  777. * @return void
  778. */
  779. public function testQueryNotFatalError()
  780. {
  781. $this->loadFixtures('Comments');
  782. $comments = TableRegistry::get('Comments');
  783. $comments->find()->contain('Deprs')->all();
  784. }
  785. /**
  786. * Tests that using matching and contain on belongsTo associations
  787. * works correctly.
  788. *
  789. * @see https://github.com/cakephp/cakephp/issues/5721
  790. * @return void
  791. */
  792. public function testFindMatchingWithContain()
  793. {
  794. $this->loadFixtures('Articles', 'Comments', 'Users');
  795. $comments = TableRegistry::get('Comments');
  796. $comments->belongsTo('Articles');
  797. $comments->belongsTo('Users');
  798. $result = $comments->find()
  799. ->contain(['Articles', 'Users'])
  800. ->matching('Articles', function ($q) {
  801. return $q->where(['Articles.id >=' => 1]);
  802. })
  803. ->matching('Users', function ($q) {
  804. return $q->where(['Users.id >=' => 1]);
  805. })
  806. ->order(['Comments.id' => 'ASC'])
  807. ->first();
  808. $this->assertInstanceOf('Cake\ORM\Entity', $result->article);
  809. $this->assertInstanceOf('Cake\ORM\Entity', $result->user);
  810. $this->assertEquals(2, $result->user->id);
  811. $this->assertEquals(1, $result->article->id);
  812. }
  813. /**
  814. * Tests that HasMany associations don't use duplicate PK values.
  815. *
  816. * @return void
  817. */
  818. public function testHasManyEagerLoadingUniqueKey()
  819. {
  820. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  821. $table = TableRegistry::get('ArticlesTags');
  822. $table->belongsTo('Articles', [
  823. 'strategy' => 'select'
  824. ]);
  825. $result = $table->find()
  826. ->contain(['Articles' => function ($q) {
  827. $result = $q->sql();
  828. $this->assertNotContains(':c2', $result, 'Only 2 bindings as there are only 2 rows.');
  829. $this->assertNotContains(':c3', $result, 'Only 2 bindings as there are only 2 rows.');
  830. return $q;
  831. }])
  832. ->toArray();
  833. $this->assertNotEmpty($result[0]->article);
  834. }
  835. /**
  836. * Tests that using contain but selecting no fields from the association
  837. * does not trigger any errors and fetches the right results.
  838. *
  839. * @see https://github.com/cakephp/cakephp/issues/6214
  840. * @return void
  841. */
  842. public function testContainWithNoFields()
  843. {
  844. $this->loadFixtures('Comments', 'Users');
  845. $table = TableRegistry::get('Comments');
  846. $table->belongsTo('Users');
  847. $results = $table->find()
  848. ->select(['Comments.id', 'Comments.user_id'])
  849. ->contain(['Users'])
  850. ->where(['Users.id' => 1])
  851. ->combine('id', 'user_id');
  852. $this->assertEquals([3 => 1, 4 => 1, 5 => 1], $results->toArray());
  853. }
  854. /**
  855. * Tests that using matching and selecting no fields for that association
  856. * will no trigger any errors and fetch the right results
  857. *
  858. * @see https://github.com/cakephp/cakephp/issues/6223
  859. * @return void
  860. */
  861. public function testMatchingWithNoFields()
  862. {
  863. $this->loadFixtures('Comments', 'Users');
  864. $table = TableRegistry::get('Users');
  865. $table->hasMany('Comments');
  866. $results = $table->find()
  867. ->select(['Users.id'])
  868. ->matching('Comments', function ($q) {
  869. return $q->where(['Comments.id' => 1]);
  870. })
  871. ->extract('id')
  872. ->toList();
  873. $this->assertEquals([2], $results);
  874. }
  875. /**
  876. * Test that empty conditions in a matching clause don't cause errors.
  877. *
  878. * @return void
  879. */
  880. public function testMatchingEmptyQuery()
  881. {
  882. $this->loadFixtures('Articles', 'Tags', 'ArticlesTags');
  883. $table = TableRegistry::get('Articles');
  884. $table->belongsToMany('Tags');
  885. $rows = $table->find()
  886. ->matching('Tags', function ($q) {
  887. return $q->where([]);
  888. })
  889. ->all();
  890. $this->assertNotEmpty($rows);
  891. $rows = $table->find()
  892. ->matching('Tags', function ($q) {
  893. return $q->where(null);
  894. })
  895. ->all();
  896. $this->assertNotEmpty($rows);
  897. }
  898. /**
  899. * Tests that using a subquery as part of an expression will not make invalid SQL
  900. *
  901. * @return void
  902. */
  903. public function testSubqueryInSelectExpression()
  904. {
  905. $this->loadFixtures('Comments');
  906. $table = TableRegistry::get('Comments');
  907. $ratio = $table->find()
  908. ->select(function ($query) use ($table) {
  909. $allCommentsCount = $table->find()->select($query->func()->count('*'));
  910. $countToFloat = $query->newExpr([$query->func()->count('*'), '1.0'])->type('*');
  911. return [
  912. 'ratio' => $query
  913. ->newExpr($countToFloat)
  914. ->add($allCommentsCount)
  915. ->type('/')
  916. ];
  917. })
  918. ->where(['user_id' => 1])
  919. ->first()
  920. ->ratio;
  921. $this->assertEquals(0.5, $ratio);
  922. }
  923. /**
  924. * Tests calling last on an empty table
  925. *
  926. * @see https://github.com/cakephp/cakephp/issues/6683
  927. * @return void
  928. */
  929. public function testFindLastOnEmptyTable()
  930. {
  931. $this->loadFixtures('Comments');
  932. $table = TableRegistry::get('Comments');
  933. $table->deleteAll(['1 = 1']);
  934. $this->assertEquals(0, $table->find()->count());
  935. $this->assertNull($table->find()->last());
  936. }
  937. /**
  938. * Tests calling contain in a nested closure
  939. *
  940. * @see https://github.com/cakephp/cakephp/issues/7591
  941. * @return void
  942. */
  943. public function testContainInNestedClosure()
  944. {
  945. $this->loadFixtures('Comments', 'Articles', 'Authors', 'Tags', 'AuthorsTags');
  946. $table = TableRegistry::get('Comments');
  947. $table->belongsTo('Articles');
  948. $table->Articles->belongsTo('Authors');
  949. $table->Articles->Authors->belongsToMany('Tags');
  950. $query = $table->find()->where(['Comments.id' => 5])->contain(['Articles' => function ($q) {
  951. return $q->contain(['Authors' => function ($q) {
  952. return $q->contain('Tags');
  953. }]);
  954. }]);
  955. $this->assertCount(2, $query->first()->article->author->tags);
  956. }
  957. /**
  958. * Test that the typemaps used in function expressions
  959. * create the correct results.
  960. *
  961. * @return void
  962. */
  963. public function testTypemapInFunctions()
  964. {
  965. $this->loadFixtures('Comments');
  966. $table = TableRegistry::get('Comments');
  967. $table->updateAll(['published' => null], ['1 = 1']);
  968. $query = $table->find();
  969. $query->select([
  970. 'id',
  971. 'coalesced' => $query->func()->coalesce(
  972. ['published' => 'identifier', -1],
  973. ['integer']
  974. )
  975. ]);
  976. $result = $query->all()->first();
  977. $this->assertSame(
  978. -1,
  979. $result['coalesced'],
  980. 'Output values for functions should be casted'
  981. );
  982. }
  983. /**
  984. * Test that the typemaps used in function expressions
  985. * create the correct results.
  986. *
  987. * @return void
  988. */
  989. public function testTypemapInFunctions2()
  990. {
  991. $this->loadFixtures('Comments');
  992. $table = TableRegistry::get('Comments');
  993. $query = $table->find();
  994. $query->select([
  995. 'max' => $query->func()->max('created', ['datetime'])
  996. ]);
  997. $result = $query->all()->first();
  998. $this->assertEquals(new Time('2007-03-18 10:55:23'), $result['max']);
  999. }
  1000. /**
  1001. * Test that contain queries map types correctly.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testBooleanConditionsInContain()
  1006. {
  1007. $this->loadFixtures('Articles', 'Tags', 'SpecialTags');
  1008. $table = TableRegistry::get('Articles');
  1009. $table->belongsToMany('Tags', [
  1010. 'foreignKey' => 'article_id',
  1011. 'associationForeignKey' => 'tag_id',
  1012. 'through' => 'SpecialTags'
  1013. ]);
  1014. $query = $table->find()
  1015. ->contain(['Tags' => function ($q) {
  1016. return $q->where(['SpecialTags.highlighted_time >' => new Time('2014-06-01 00:00:00')]);
  1017. }])
  1018. ->where(['Articles.id' => 2]);
  1019. $result = $query->first();
  1020. $this->assertEquals(2, $result->id);
  1021. $this->assertNotEmpty($result->tags, 'Missing tags');
  1022. $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
  1023. }
  1024. /**
  1025. * Test that contain queries map types correctly.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testComplexTypesInJoinedWhere()
  1030. {
  1031. $this->loadFixtures('Comments', 'Users');
  1032. $table = TableRegistry::get('Users');
  1033. $table->hasOne('Comments', [
  1034. 'foreignKey' => 'user_id',
  1035. ]);
  1036. $query = $table->find()
  1037. ->contain('Comments')
  1038. ->where([
  1039. 'Comments.updated >' => new \DateTime('2007-03-18 10:55:00')
  1040. ]);
  1041. $result = $query->first();
  1042. $this->assertNotEmpty($result);
  1043. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->updated);
  1044. }
  1045. /**
  1046. * Test that nested contain queries map types correctly.
  1047. *
  1048. * @return void
  1049. */
  1050. public function testComplexNestedTypesInJoinedWhere()
  1051. {
  1052. $this->loadFixtures('Comments', 'Users', 'Articles');
  1053. $table = TableRegistry::get('Users');
  1054. $table->hasOne('Comments', [
  1055. 'foreignKey' => 'user_id',
  1056. ]);
  1057. $table->Comments->belongsTo('Articles');
  1058. $table->Comments->Articles->belongsTo('Authors', [
  1059. 'className' => 'Users',
  1060. 'foreignKey' => 'author_id'
  1061. ]);
  1062. $query = $table->find()
  1063. ->contain('Comments.Articles.Authors')
  1064. ->where([
  1065. 'Authors.created >' => new \DateTime('2007-03-17 01:16:00')
  1066. ]);
  1067. $result = $query->first();
  1068. $this->assertNotEmpty($result);
  1069. $this->assertInstanceOf('Cake\I18n\Time', $result->comment->article->author->updated);
  1070. }
  1071. /**
  1072. * Tests that it is possible to use matching with dot notation
  1073. * even when part of the part of the path in the dot notation is
  1074. * shared for two different calls
  1075. *
  1076. * @return void
  1077. */
  1078. public function testDotNotationNotOverride()
  1079. {
  1080. $this->loadFixtures('Comments', 'Articles', 'Tags', 'Authors', 'SpecialTags');
  1081. $table = TableRegistry::get('Comments');
  1082. $articles = $table->belongsTo('Articles');
  1083. $specialTags = $articles->hasMany('SpecialTags');
  1084. $specialTags->belongsTo('Authors');
  1085. $specialTags->belongsTo('Tags');
  1086. $results = $table
  1087. ->find()
  1088. ->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
  1089. ->matching('Articles.SpecialTags.Tags')
  1090. ->matching('Articles.SpecialTags.Authors', function ($q) {
  1091. return $q->where(['Authors.id' => 2]);
  1092. })
  1093. ->distinct()
  1094. ->hydrate(false)
  1095. ->toArray();
  1096. $this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
  1097. }
  1098. /**
  1099. * Test expression based ordering with unions.
  1100. *
  1101. * @return void
  1102. */
  1103. public function testComplexOrderWithUnion()
  1104. {
  1105. $this->loadFixtures('Comments');
  1106. $table = TableRegistry::get('Comments');
  1107. $query = $table->find();
  1108. $inner = $table->find()
  1109. ->select(['content' => 'comment'])
  1110. ->where(['id >' => 3]);
  1111. $inner2 = $table->find()
  1112. ->select(['content' => 'comment'])
  1113. ->where(['id <' => 3]);
  1114. $order = $query->func()->concat(['content' => 'literal', 'test']);
  1115. $query->select(['inside.content'])
  1116. ->from(['inside' => $inner->unionAll($inner2)])
  1117. ->orderAsc($order);
  1118. $results = $query->toArray();
  1119. $this->assertCount(5, $results);
  1120. }
  1121. /**
  1122. * Test that associations that are loaded with subqueries
  1123. * do not cause errors when the subquery has a limit & order clause.
  1124. *
  1125. * @return void
  1126. */
  1127. public function testEagerLoadOrderAndSubquery()
  1128. {
  1129. $this->loadFixtures('Articles', 'Comments');
  1130. $table = TableRegistry::get('Articles');
  1131. $table->hasMany('Comments', [
  1132. 'strategy' => 'subquery'
  1133. ]);
  1134. $query = $table->find()
  1135. ->select(['score' => 100])
  1136. ->autoFields(true)
  1137. ->contain(['Comments'])
  1138. ->limit(5)
  1139. ->order(['score' => 'desc']);
  1140. $result = $query->all();
  1141. $this->assertCount(3, $result);
  1142. }
  1143. /**
  1144. * Tests that decorating the results does not result in a memory leak
  1145. *
  1146. * @return void
  1147. */
  1148. public function testFormatResultsMemoryLeak()
  1149. {
  1150. $this->loadFixtures('Articles', 'Authors', 'Tags', 'ArticlesTags');
  1151. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1152. $table = TableRegistry::get('Articles');
  1153. $table->belongsTo('Authors');
  1154. $table->belongsToMany('Tags');
  1155. gc_collect_cycles();
  1156. $memory = memory_get_usage() / 1024 / 1024;
  1157. foreach (range(1, 3) as $time) {
  1158. $table->find()
  1159. ->contain(['Authors', 'Tags'])
  1160. ->formatResults(function ($results) {
  1161. return $results;
  1162. })
  1163. ->all();
  1164. }
  1165. gc_collect_cycles();
  1166. $endMemory = memory_get_usage() / 1024 / 1024;
  1167. $this->assertWithinRange($endMemory, $memory, 1.25, 'Memory leak in ResultSet');
  1168. }
  1169. /**
  1170. * Tests that having bound placeholders in the order clause does not result
  1171. * in an error when trying to count a query.
  1172. *
  1173. * @return void
  1174. */
  1175. public function testCountWithComplexOrderBy()
  1176. {
  1177. $this->loadFixtures('Articles');
  1178. $table = TableRegistry::get('Articles');
  1179. $query = $table->find();
  1180. $query->orderDesc($query->newExpr()->addCase(
  1181. [$query->newExpr()->add(['id' => 3])],
  1182. [1, 0]
  1183. ));
  1184. $query->order(['title' => 'desc']);
  1185. // Executing the normal query before getting the count
  1186. $query->all();
  1187. $this->assertEquals(3, $query->count());
  1188. $table = TableRegistry::get('Articles');
  1189. $query = $table->find();
  1190. $query->orderDesc($query->newExpr()->addCase(
  1191. [$query->newExpr()->add(['id' => 3])],
  1192. [1, 0]
  1193. ));
  1194. $query->orderDesc($query->newExpr()->add(['id' => 3]));
  1195. // Not executing the query first, just getting the count
  1196. $this->assertEquals(3, $query->count());
  1197. }
  1198. /**
  1199. * Tests that the now() function expression can be used in the
  1200. * where clause of a query
  1201. *
  1202. * @see https://github.com/cakephp/cakephp/issues/7943
  1203. * @return void
  1204. */
  1205. public function testFunctionInWhereClause()
  1206. {
  1207. $this->loadFixtures('Comments');
  1208. $table = TableRegistry::get('Comments');
  1209. $table->updateAll(['updated' => Time::tomorrow()], ['id' => 6]);
  1210. $query = $table->find();
  1211. $result = $query->where(['updated >' => $query->func()->now('datetime')])->first();
  1212. $this->assertSame(6, $result->id);
  1213. }
  1214. }