PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/TestCase/ORM/QueryTest.php

https://github.com/binondord/cakephp
PHP | 2663 lines | 1937 code | 221 blank | 505 comment | 2 complexity | c687e8d4e2301b67d4b7e1ca37d1e675 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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\Database\Expression\IdentifierExpression;
  17. use Cake\Database\Expression\OrderByExpression;
  18. use Cake\Database\Expression\QueryExpression;
  19. use Cake\Database\TypeMap;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\ORM\Query;
  22. use Cake\ORM\ResultSet;
  23. use Cake\ORM\Table;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * Tests Query class
  28. */
  29. class QueryTest extends TestCase
  30. {
  31. /**
  32. * Fixture to be used
  33. *
  34. * @var array
  35. */
  36. public $fixtures = ['core.articles', 'core.authors', 'core.tags',
  37. 'core.articles_tags', 'core.posts'];
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. $this->connection = ConnectionManager::get('test');
  47. $schema = [
  48. 'id' => ['type' => 'integer'],
  49. '_constraints' => [
  50. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  51. ]
  52. ];
  53. $schema1 = [
  54. 'id' => ['type' => 'integer'],
  55. 'name' => ['type' => 'string'],
  56. 'phone' => ['type' => 'string'],
  57. '_constraints' => [
  58. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  59. ]
  60. ];
  61. $schema2 = [
  62. 'id' => ['type' => 'integer'],
  63. 'total' => ['type' => 'string'],
  64. 'placed' => ['type' => 'datetime'],
  65. '_constraints' => [
  66. 'primary' => ['type' => 'primary', 'columns' => ['id']]
  67. ]
  68. ];
  69. $this->table = $table = TableRegistry::get('foo', ['schema' => $schema]);
  70. $clients = TableRegistry::get('clients', ['schema' => $schema1]);
  71. $orders = TableRegistry::get('orders', ['schema' => $schema2]);
  72. $companies = TableRegistry::get('companies', ['schema' => $schema, 'table' => 'organizations']);
  73. $orderTypes = TableRegistry::get('orderTypes', ['schema' => $schema]);
  74. $stuff = TableRegistry::get('stuff', ['schema' => $schema, 'table' => 'things']);
  75. $stuffTypes = TableRegistry::get('stuffTypes', ['schema' => $schema]);
  76. $categories = TableRegistry::get('categories', ['schema' => $schema]);
  77. $table->belongsTo('clients');
  78. $clients->hasOne('orders');
  79. $clients->belongsTo('companies');
  80. $orders->belongsTo('orderTypes');
  81. $orders->hasOne('stuff');
  82. $stuff->belongsTo('stuffTypes');
  83. $companies->belongsTo('categories');
  84. $this->fooTypeMap = new TypeMap(['foo.id' => 'integer', 'id' => 'integer']);
  85. }
  86. /**
  87. * tearDown method
  88. *
  89. * @return void
  90. */
  91. public function tearDown()
  92. {
  93. parent::tearDown();
  94. TableRegistry::clear();
  95. }
  96. /**
  97. * Data provider for the two types of strategies HasMany implements
  98. *
  99. * @return void
  100. */
  101. public function strategiesProviderHasMany()
  102. {
  103. return [['subquery'], ['select']];
  104. }
  105. /**
  106. * Data provider for the two types of strategies BelongsTo implements
  107. *
  108. * @return void
  109. */
  110. public function strategiesProviderBelongsTo()
  111. {
  112. return [['join'], ['select']];
  113. }
  114. /**
  115. * Data provider for the two types of strategies BelongsToMany implements
  116. *
  117. * @return void
  118. */
  119. public function strategiesProviderBelongsToMany()
  120. {
  121. return [['subquery'], ['select']];
  122. }
  123. /**
  124. * Tests that results are grouped correctly when using contain()
  125. * and results are not hydrated
  126. *
  127. * @dataProvider strategiesProviderBelongsTo
  128. * @return void
  129. */
  130. public function testContainResultFetchingOneLevel($strategy)
  131. {
  132. $table = TableRegistry::get('articles', ['table' => 'articles']);
  133. $table->belongsTo('authors', ['strategy' => $strategy]);
  134. $query = new Query($this->connection, $table);
  135. $results = $query->select()
  136. ->contain('authors')
  137. ->hydrate(false)
  138. ->order(['articles.id' => 'asc'])
  139. ->toArray();
  140. $expected = [
  141. [
  142. 'id' => 1,
  143. 'title' => 'First Article',
  144. 'body' => 'First Article Body',
  145. 'author_id' => 1,
  146. 'published' => 'Y',
  147. 'author' => [
  148. 'id' => 1,
  149. 'name' => 'mariano'
  150. ]
  151. ],
  152. [
  153. 'id' => 2,
  154. 'title' => 'Second Article',
  155. 'body' => 'Second Article Body',
  156. 'author_id' => 3,
  157. 'published' => 'Y',
  158. 'author' => [
  159. 'id' => 3,
  160. 'name' => 'larry'
  161. ]
  162. ],
  163. [
  164. 'id' => 3,
  165. 'title' => 'Third Article',
  166. 'body' => 'Third Article Body',
  167. 'author_id' => 1,
  168. 'published' => 'Y',
  169. 'author' => [
  170. 'id' => 1,
  171. 'name' => 'mariano'
  172. ]
  173. ],
  174. ];
  175. $this->assertEquals($expected, $results);
  176. }
  177. /**
  178. * Tests that HasMany associations are correctly eager loaded and results
  179. * correctly nested when no hydration is used
  180. * Also that the query object passes the correct parent model keys to the
  181. * association objects in order to perform eager loading with select strategy
  182. *
  183. * @dataProvider strategiesProviderHasMany
  184. * @return void
  185. */
  186. public function testHasManyEagerLoadingNoHydration($strategy)
  187. {
  188. $table = TableRegistry::get('authors');
  189. TableRegistry::get('articles');
  190. $table->hasMany('articles', [
  191. 'propertyName' => 'articles',
  192. 'strategy' => $strategy,
  193. 'sort' => ['articles.id' => 'asc']
  194. ]);
  195. $query = new Query($this->connection, $table);
  196. $results = $query->select()
  197. ->contain('articles')
  198. ->hydrate(false)
  199. ->toArray();
  200. $expected = [
  201. [
  202. 'id' => 1,
  203. 'name' => 'mariano',
  204. 'articles' => [
  205. [
  206. 'id' => 1,
  207. 'title' => 'First Article',
  208. 'body' => 'First Article Body',
  209. 'author_id' => 1,
  210. 'published' => 'Y',
  211. ],
  212. [
  213. 'id' => 3,
  214. 'title' => 'Third Article',
  215. 'body' => 'Third Article Body',
  216. 'author_id' => 1,
  217. 'published' => 'Y',
  218. ],
  219. ]
  220. ],
  221. [
  222. 'id' => 2,
  223. 'name' => 'nate',
  224. 'articles' => [],
  225. ],
  226. [
  227. 'id' => 3,
  228. 'name' => 'larry',
  229. 'articles' => [
  230. [
  231. 'id' => 2,
  232. 'title' => 'Second Article',
  233. 'body' => 'Second Article Body',
  234. 'author_id' => 3,
  235. 'published' => 'Y'
  236. ]
  237. ]
  238. ],
  239. [
  240. 'id' => 4,
  241. 'name' => 'garrett',
  242. 'articles' => [],
  243. ]
  244. ];
  245. $this->assertEquals($expected, $results);
  246. $results = $query->repository($table)
  247. ->select()
  248. ->contain(['articles' => ['conditions' => ['articles.id' => 2]]])
  249. ->hydrate(false)
  250. ->toArray();
  251. $expected[0]['articles'] = [];
  252. $this->assertEquals($expected, $results);
  253. $this->assertEquals($table->association('articles')->strategy(), $strategy);
  254. }
  255. /**
  256. * Tests that it is possible to count results containing hasMany associations
  257. * both hydrating and not hydrating the results.
  258. *
  259. * @dataProvider strategiesProviderHasMany
  260. * @return void
  261. */
  262. public function testHasManyEagerLoadingCount($strategy)
  263. {
  264. $table = TableRegistry::get('authors');
  265. TableRegistry::get('articles');
  266. $table->hasMany('articles', [
  267. 'property' => 'articles',
  268. 'strategy' => $strategy,
  269. 'sort' => ['articles.id' => 'asc']
  270. ]);
  271. $query = new Query($this->connection, $table);
  272. $query = $query->select()
  273. ->contain('articles');
  274. $expected = 4;
  275. $results = $query->hydrate(false)
  276. ->count();
  277. $this->assertEquals($expected, $results);
  278. $results = $query->hydrate(true)
  279. ->count();
  280. $this->assertEquals($expected, $results);
  281. }
  282. /**
  283. * Tests that it is possible to set fields & order in a hasMany result set
  284. *
  285. * @dataProvider strategiesProviderHasMany
  286. * @return void
  287. */
  288. public function testHasManyEagerLoadingFieldsAndOrderNoHydration($strategy)
  289. {
  290. $table = TableRegistry::get('authors');
  291. TableRegistry::get('articles');
  292. $table->hasMany('articles', ['propertyName' => 'articles'] + compact('strategy'));
  293. $query = new Query($this->connection, $table);
  294. $results = $query->select()
  295. ->contain([
  296. 'articles' => [
  297. 'fields' => ['title', 'author_id'],
  298. 'sort' => ['articles.id' => 'DESC']
  299. ]
  300. ])
  301. ->hydrate(false)
  302. ->toArray();
  303. $expected = [
  304. [
  305. 'id' => 1,
  306. 'name' => 'mariano',
  307. 'articles' => [
  308. ['title' => 'Third Article', 'author_id' => 1],
  309. ['title' => 'First Article', 'author_id' => 1],
  310. ]
  311. ],
  312. [
  313. 'id' => 2,
  314. 'name' => 'nate',
  315. 'articles' => [],
  316. ],
  317. [
  318. 'id' => 3,
  319. 'name' => 'larry',
  320. 'articles' => [
  321. ['title' => 'Second Article', 'author_id' => 3],
  322. ]
  323. ],
  324. [
  325. 'id' => 4,
  326. 'name' => 'garrett',
  327. 'articles' => [],
  328. ],
  329. ];
  330. $this->assertEquals($expected, $results);
  331. }
  332. /**
  333. * Tests that deep associations can be eagerly loaded
  334. *
  335. * @dataProvider strategiesProviderHasMany
  336. * @return void
  337. */
  338. public function testHasManyEagerLoadingDeep($strategy)
  339. {
  340. $table = TableRegistry::get('authors');
  341. $article = TableRegistry::get('articles');
  342. $table->hasMany('articles', [
  343. 'propertyName' => 'articles',
  344. 'strategy' => $strategy,
  345. 'sort' => ['articles.id' => 'asc']
  346. ]);
  347. $article->belongsTo('authors');
  348. $query = new Query($this->connection, $table);
  349. $results = $query->select()
  350. ->contain(['articles' => ['authors']])
  351. ->hydrate(false)
  352. ->toArray();
  353. $expected = [
  354. [
  355. 'id' => 1,
  356. 'name' => 'mariano',
  357. 'articles' => [
  358. [
  359. 'id' => 1,
  360. 'title' => 'First Article',
  361. 'author_id' => 1,
  362. 'body' => 'First Article Body',
  363. 'published' => 'Y',
  364. 'author' => ['id' => 1, 'name' => 'mariano']
  365. ],
  366. [
  367. 'id' => 3,
  368. 'title' => 'Third Article',
  369. 'author_id' => 1,
  370. 'body' => 'Third Article Body',
  371. 'published' => 'Y',
  372. 'author' => ['id' => 1, 'name' => 'mariano']
  373. ],
  374. ]
  375. ],
  376. [
  377. 'id' => 2,
  378. 'name' => 'nate',
  379. 'articles' => [],
  380. ],
  381. [
  382. 'id' => 3,
  383. 'name' => 'larry',
  384. 'articles' => [
  385. [
  386. 'id' => 2,
  387. 'title' => 'Second Article',
  388. 'author_id' => 3,
  389. 'body' => 'Second Article Body',
  390. 'published' => 'Y',
  391. 'author' => ['id' => 3, 'name' => 'larry']
  392. ],
  393. ]
  394. ],
  395. [
  396. 'id' => 4,
  397. 'name' => 'garrett',
  398. 'articles' => [],
  399. ]
  400. ];
  401. $this->assertEquals($expected, $results);
  402. }
  403. /**
  404. * Tests that hasMany associations can be loaded even when related to a secondary
  405. * model in the query
  406. *
  407. * @dataProvider strategiesProviderHasMany
  408. * @return void
  409. */
  410. public function testHasManyEagerLoadingFromSecondaryTable($strategy)
  411. {
  412. $author = TableRegistry::get('authors');
  413. $article = TableRegistry::get('articles');
  414. $post = TableRegistry::get('posts');
  415. $author->hasMany('posts', [
  416. 'sort' => ['posts.id' => 'ASC'],
  417. 'strategy' => $strategy
  418. ]);
  419. $article->belongsTo('authors');
  420. $query = new Query($this->connection, $article);
  421. $results = $query->select()
  422. ->contain(['authors' => ['posts']])
  423. ->order(['articles.id' => 'ASC'])
  424. ->hydrate(false)
  425. ->toArray();
  426. $expected = [
  427. [
  428. 'id' => 1,
  429. 'title' => 'First Article',
  430. 'body' => 'First Article Body',
  431. 'author_id' => 1,
  432. 'published' => 'Y',
  433. 'author' => [
  434. 'id' => 1,
  435. 'name' => 'mariano',
  436. 'posts' => [
  437. [
  438. 'id' => '1',
  439. 'title' => 'First Post',
  440. 'body' => 'First Post Body',
  441. 'author_id' => 1,
  442. 'published' => 'Y',
  443. ],
  444. [
  445. 'id' => '3',
  446. 'title' => 'Third Post',
  447. 'body' => 'Third Post Body',
  448. 'author_id' => 1,
  449. 'published' => 'Y',
  450. ],
  451. ]
  452. ]
  453. ],
  454. [
  455. 'id' => 2,
  456. 'title' => 'Second Article',
  457. 'body' => 'Second Article Body',
  458. 'author_id' => 3,
  459. 'published' => 'Y',
  460. 'author' => [
  461. 'id' => 3,
  462. 'name' => 'larry',
  463. 'posts' => [
  464. [
  465. 'id' => 2,
  466. 'title' => 'Second Post',
  467. 'body' => 'Second Post Body',
  468. 'author_id' => 3,
  469. 'published' => 'Y',
  470. ]
  471. ]
  472. ]
  473. ],
  474. [
  475. 'id' => 3,
  476. 'title' => 'Third Article',
  477. 'body' => 'Third Article Body',
  478. 'author_id' => 1,
  479. 'published' => 'Y',
  480. 'author' => [
  481. 'id' => 1,
  482. 'name' => 'mariano',
  483. 'posts' => [
  484. [
  485. 'id' => '1',
  486. 'title' => 'First Post',
  487. 'body' => 'First Post Body',
  488. 'author_id' => 1,
  489. 'published' => 'Y',
  490. ],
  491. [
  492. 'id' => '3',
  493. 'title' => 'Third Post',
  494. 'body' => 'Third Post Body',
  495. 'author_id' => 1,
  496. 'published' => 'Y',
  497. ],
  498. ]
  499. ]
  500. ],
  501. ];
  502. $this->assertEquals($expected, $results);
  503. }
  504. /**
  505. * Tests that BelongsToMany associations are correctly eager loaded.
  506. * Also that the query object passes the correct parent model keys to the
  507. * association objects in order to perform eager loading with select strategy
  508. *
  509. * @dataProvider strategiesProviderBelongsToMany
  510. * @return void
  511. */
  512. public function testBelongsToManyEagerLoadingNoHydration($strategy)
  513. {
  514. $table = TableRegistry::get('Articles');
  515. TableRegistry::get('Tags');
  516. TableRegistry::get('ArticlesTags', [
  517. 'table' => 'articles_tags'
  518. ]);
  519. $table->belongsToMany('Tags', ['strategy' => $strategy]);
  520. $query = new Query($this->connection, $table);
  521. $results = $query->select()->contain('Tags')->hydrate(false)->toArray();
  522. $expected = [
  523. [
  524. 'id' => 1,
  525. 'author_id' => 1,
  526. 'title' => 'First Article',
  527. 'body' => 'First Article Body',
  528. 'published' => 'Y',
  529. 'tags' => [
  530. [
  531. 'id' => 1,
  532. 'name' => 'tag1',
  533. '_joinData' => ['article_id' => 1, 'tag_id' => 1]
  534. ],
  535. [
  536. 'id' => 2,
  537. 'name' => 'tag2',
  538. '_joinData' => ['article_id' => 1, 'tag_id' => 2]
  539. ]
  540. ]
  541. ],
  542. [
  543. 'id' => 2,
  544. 'title' => 'Second Article',
  545. 'body' => 'Second Article Body',
  546. 'author_id' => 3,
  547. 'published' => 'Y',
  548. 'tags' => [
  549. [
  550. 'id' => 1,
  551. 'name' => 'tag1',
  552. '_joinData' => ['article_id' => 2, 'tag_id' => 1]
  553. ],
  554. [
  555. 'id' => 3,
  556. 'name' => 'tag3',
  557. '_joinData' => ['article_id' => 2, 'tag_id' => 3]
  558. ]
  559. ]
  560. ],
  561. [
  562. 'id' => 3,
  563. 'title' => 'Third Article',
  564. 'body' => 'Third Article Body',
  565. 'author_id' => 1,
  566. 'published' => 'Y',
  567. 'tags' => [],
  568. ],
  569. ];
  570. $this->assertEquals($expected, $results);
  571. $results = $query->select()
  572. ->contain(['Tags' => ['conditions' => ['Tags.id' => 3]]])
  573. ->hydrate(false)
  574. ->toArray();
  575. $expected = [
  576. [
  577. 'id' => 1,
  578. 'author_id' => 1,
  579. 'title' => 'First Article',
  580. 'body' => 'First Article Body',
  581. 'published' => 'Y',
  582. 'tags' => [],
  583. ],
  584. [
  585. 'id' => 2,
  586. 'title' => 'Second Article',
  587. 'body' => 'Second Article Body',
  588. 'author_id' => 3,
  589. 'published' => 'Y',
  590. 'tags' => [
  591. [
  592. 'id' => 3,
  593. 'name' => 'tag3',
  594. '_joinData' => ['article_id' => 2, 'tag_id' => 3]
  595. ]
  596. ]
  597. ],
  598. [
  599. 'id' => 3,
  600. 'title' => 'Third Article',
  601. 'body' => 'Third Article Body',
  602. 'author_id' => 1,
  603. 'published' => 'Y',
  604. 'tags' => [],
  605. ],
  606. ];
  607. $this->assertEquals($expected, $results);
  608. $this->assertEquals($table->association('Tags')->strategy(), $strategy);
  609. }
  610. /**
  611. * Tests that tables results can be filtered by the result of a HasMany
  612. *
  613. * @return void
  614. */
  615. public function testFilteringByHasManyNoHydration()
  616. {
  617. $query = new Query($this->connection, $this->table);
  618. $table = TableRegistry::get('authors');
  619. TableRegistry::get('articles');
  620. $table->hasMany('articles');
  621. $results = $query->repository($table)
  622. ->select()
  623. ->hydrate(false)
  624. ->matching('articles', function ($q) {
  625. return $q->where(['articles.id' => 2]);
  626. })
  627. ->toArray();
  628. $expected = [
  629. [
  630. 'id' => 3,
  631. 'name' => 'larry',
  632. '_matchingData' => [
  633. 'articles' => [
  634. 'id' => 2,
  635. 'title' => 'Second Article',
  636. 'body' => 'Second Article Body',
  637. 'author_id' => 3,
  638. 'published' => 'Y',
  639. ]
  640. ]
  641. ]
  642. ];
  643. $this->assertEquals($expected, $results);
  644. }
  645. /**
  646. * Tests that BelongsToMany associations are correctly eager loaded.
  647. * Also that the query object passes the correct parent model keys to the
  648. * association objects in order to perform eager loading with select strategy
  649. *
  650. * @return void
  651. */
  652. public function testFilteringByBelongsToManyNoHydration()
  653. {
  654. $query = new Query($this->connection, $this->table);
  655. $table = TableRegistry::get('Articles');
  656. TableRegistry::get('Tags');
  657. TableRegistry::get('ArticlesTags', [
  658. 'table' => 'articles_tags'
  659. ]);
  660. $table->belongsToMany('Tags');
  661. $results = $query->repository($table)->select()
  662. ->matching('Tags', function ($q) {
  663. return $q->where(['Tags.id' => 3]);
  664. })
  665. ->hydrate(false)
  666. ->toArray();
  667. $expected = [
  668. [
  669. 'id' => 2,
  670. 'author_id' => 3,
  671. 'title' => 'Second Article',
  672. 'body' => 'Second Article Body',
  673. 'published' => 'Y',
  674. '_matchingData' => [
  675. 'Tags' => [
  676. 'id' => 3,
  677. 'name' => 'tag3',
  678. ],
  679. 'ArticlesTags' => ['article_id' => 2, 'tag_id' => 3]
  680. ]
  681. ]
  682. ];
  683. $this->assertEquals($expected, $results);
  684. $query = new Query($this->connection, $table);
  685. $results = $query->select()
  686. ->matching('Tags', function ($q) {
  687. return $q->where(['Tags.name' => 'tag2']);
  688. })
  689. ->hydrate(false)
  690. ->toArray();
  691. $expected = [
  692. [
  693. 'id' => 1,
  694. 'title' => 'First Article',
  695. 'body' => 'First Article Body',
  696. 'author_id' => 1,
  697. 'published' => 'Y',
  698. '_matchingData' => [
  699. 'Tags' => [
  700. 'id' => 2,
  701. 'name' => 'tag2',
  702. ],
  703. 'ArticlesTags' => ['article_id' => 1, 'tag_id' => 2]
  704. ]
  705. ]
  706. ];
  707. $this->assertEquals($expected, $results);
  708. }
  709. /**
  710. * Tests that it is possible to filter by deep associations
  711. *
  712. * @return void
  713. */
  714. public function testMatchingDotNotation()
  715. {
  716. $query = new Query($this->connection, $this->table);
  717. $table = TableRegistry::get('authors');
  718. TableRegistry::get('articles');
  719. $table->hasMany('articles');
  720. TableRegistry::get('articles')->belongsToMany('tags');
  721. $results = $query->repository($table)
  722. ->select()
  723. ->hydrate(false)
  724. ->matching('articles.tags', function ($q) {
  725. return $q->where(['tags.id' => 2]);
  726. })
  727. ->toArray();
  728. $expected = [
  729. [
  730. 'id' => 1,
  731. 'name' => 'mariano',
  732. '_matchingData' => [
  733. 'tags' => [
  734. 'id' => 2,
  735. 'name' => 'tag2',
  736. ],
  737. 'articles' => [
  738. 'id' => 1,
  739. 'author_id' => 1,
  740. 'title' => 'First Article',
  741. 'body' => 'First Article Body',
  742. 'published' => 'Y'
  743. ],
  744. 'ArticlesTags' => [
  745. 'article_id' => 1,
  746. 'tag_id' => 2
  747. ]
  748. ]
  749. ]
  750. ];
  751. $this->assertEquals($expected, $results);
  752. }
  753. /**
  754. * Test setResult()
  755. *
  756. * @return void
  757. */
  758. public function testSetResult()
  759. {
  760. $query = new Query($this->connection, $this->table);
  761. $stmt = $this->getMock('Cake\Database\StatementInterface');
  762. $results = new ResultSet($query, $stmt);
  763. $query->setResult($results);
  764. $this->assertSame($results, $query->all());
  765. }
  766. /**
  767. * Tests that applying array options to a query will convert them
  768. * to equivalent function calls with the correspondent array values
  769. *
  770. * @return void
  771. */
  772. public function testApplyOptions()
  773. {
  774. $options = [
  775. 'fields' => ['field_a', 'field_b'],
  776. 'conditions' => ['field_a' => 1, 'field_b' => 'something'],
  777. 'limit' => 1,
  778. 'order' => ['a' => 'ASC'],
  779. 'offset' => 5,
  780. 'group' => ['field_a'],
  781. 'having' => ['field_a >' => 100],
  782. 'contain' => ['table_a' => ['table_b']],
  783. 'join' => ['table_a' => ['conditions' => ['a > b']]]
  784. ];
  785. $query = new Query($this->connection, $this->table);
  786. $query->applyOptions($options);
  787. $this->assertEquals(['field_a', 'field_b'], $query->clause('select'));
  788. $expected = new QueryExpression($options['conditions'], $this->fooTypeMap);
  789. $result = $query->clause('where');
  790. $this->assertEquals($expected, $result);
  791. $this->assertEquals(1, $query->clause('limit'));
  792. $expected = new QueryExpression(['a > b'], $this->fooTypeMap);
  793. $result = $query->clause('join');
  794. $this->assertEquals([
  795. 'table_a' => ['alias' => 'table_a', 'type' => 'INNER', 'conditions' => $expected]
  796. ], $result);
  797. $expected = new OrderByExpression(['a' => 'ASC']);
  798. $this->assertEquals($expected, $query->clause('order'));
  799. $this->assertEquals(5, $query->clause('offset'));
  800. $this->assertEquals(['field_a'], $query->clause('group'));
  801. $expected = new QueryExpression($options['having']);
  802. $expected->typeMap($this->fooTypeMap);
  803. $this->assertEquals($expected, $query->clause('having'));
  804. $expected = ['table_a' => ['table_b' => []]];
  805. $this->assertEquals($expected, $query->contain());
  806. }
  807. /**
  808. * Test that page is applied after limit.
  809. *
  810. * @return void
  811. */
  812. public function testApplyOptionsPageIsLast()
  813. {
  814. $query = new Query($this->connection, $this->table);
  815. $opts = [
  816. 'page' => 3,
  817. 'limit' => 5
  818. ];
  819. $query->applyOptions($opts);
  820. $this->assertEquals(5, $query->clause('limit'));
  821. $this->assertEquals(10, $query->clause('offset'));
  822. }
  823. /**
  824. * ApplyOptions should ignore null values.
  825. *
  826. * @return void
  827. */
  828. public function testApplyOptionsIgnoreNull()
  829. {
  830. $options = [
  831. 'fields' => null,
  832. ];
  833. $query = new Query($this->connection, $this->table);
  834. $query->applyOptions($options);
  835. $this->assertEquals([], $query->clause('select'));
  836. }
  837. /**
  838. * Tests getOptions() method
  839. *
  840. * @return void
  841. */
  842. public function testGetOptions()
  843. {
  844. $options = ['doABarrelRoll' => true, 'fields' => ['id', 'name']];
  845. $query = new Query($this->connection, $this->table);
  846. $query->applyOptions($options);
  847. $expected = ['doABarrelRoll' => true];
  848. $this->assertEquals($expected, $query->getOptions());
  849. $expected = ['doABarrelRoll' => false, 'doAwesome' => true];
  850. $query->applyOptions($expected);
  851. $this->assertEquals($expected, $query->getOptions());
  852. }
  853. /**
  854. * Tests registering mappers with mapReduce()
  855. *
  856. * @return void
  857. */
  858. public function testMapReduceOnlyMapper()
  859. {
  860. $mapper1 = function () {
  861. };
  862. $mapper2 = function () {
  863. };
  864. $query = new Query($this->connection, $this->table);
  865. $this->assertSame($query, $query->mapReduce($mapper1));
  866. $this->assertEquals(
  867. [['mapper' => $mapper1, 'reducer' => null]],
  868. $query->mapReduce()
  869. );
  870. $this->assertEquals($query, $query->mapReduce($mapper2));
  871. $result = $query->mapReduce();
  872. $this->assertSame(
  873. [
  874. ['mapper' => $mapper1, 'reducer' => null],
  875. ['mapper' => $mapper2, 'reducer' => null]
  876. ],
  877. $result
  878. );
  879. }
  880. /**
  881. * Tests registering mappers and reducers with mapReduce()
  882. *
  883. * @return void
  884. */
  885. public function testMapReduceBothMethods()
  886. {
  887. $mapper1 = function () {
  888. };
  889. $mapper2 = function () {
  890. };
  891. $reducer1 = function () {
  892. };
  893. $reducer2 = function () {
  894. };
  895. $query = new Query($this->connection, $this->table);
  896. $this->assertSame($query, $query->mapReduce($mapper1, $reducer1));
  897. $this->assertEquals(
  898. [['mapper' => $mapper1, 'reducer' => $reducer1]],
  899. $query->mapReduce()
  900. );
  901. $this->assertSame($query, $query->mapReduce($mapper2, $reducer2));
  902. $this->assertEquals(
  903. [
  904. ['mapper' => $mapper1, 'reducer' => $reducer1],
  905. ['mapper' => $mapper2, 'reducer' => $reducer2]
  906. ],
  907. $query->mapReduce()
  908. );
  909. }
  910. /**
  911. * Tests that it is possible to overwrite previous map reducers
  912. *
  913. * @return void
  914. */
  915. public function testOverwriteMapReduce()
  916. {
  917. $mapper1 = function () {
  918. };
  919. $mapper2 = function () {
  920. };
  921. $reducer1 = function () {
  922. };
  923. $reducer2 = function () {
  924. };
  925. $query = new Query($this->connection, $this->table);
  926. $this->assertEquals($query, $query->mapReduce($mapper1, $reducer1));
  927. $this->assertEquals(
  928. [['mapper' => $mapper1, 'reducer' => $reducer1]],
  929. $query->mapReduce()
  930. );
  931. $this->assertEquals($query, $query->mapReduce($mapper2, $reducer2, true));
  932. $this->assertEquals(
  933. [['mapper' => $mapper2, 'reducer' => $reducer2]],
  934. $query->mapReduce()
  935. );
  936. }
  937. /**
  938. * Tests that multiple map reducers can be stacked
  939. *
  940. * @return void
  941. */
  942. public function testResultsAreWrappedInMapReduce()
  943. {
  944. $table = TableRegistry::get('articles', ['table' => 'articles']);
  945. $query = new Query($this->connection, $table);
  946. $query->select(['a' => 'id'])->limit(2)->order(['id' => 'ASC']);
  947. $query->mapReduce(function ($v, $k, $mr) {
  948. $mr->emit($v['a']);
  949. });
  950. $query->mapReduce(
  951. function ($v, $k, $mr) {
  952. $mr->emitIntermediate($v, $k);
  953. },
  954. function ($v, $k, $mr) {
  955. $mr->emit($v[0] + 1);
  956. }
  957. );
  958. $this->assertEquals([2, 3], iterator_to_array($query->all()));
  959. }
  960. /**
  961. * Tests first() method when the query has not been executed before
  962. *
  963. * @return void
  964. */
  965. public function testFirstDirtyQuery()
  966. {
  967. $table = TableRegistry::get('articles', ['table' => 'articles']);
  968. $query = new Query($this->connection, $table);
  969. $result = $query->select(['id'])->hydrate(false)->first();
  970. $this->assertEquals(['id' => 1], $result);
  971. $this->assertEquals(1, $query->clause('limit'));
  972. $result = $query->select(['id'])->first();
  973. $this->assertEquals(['id' => 1], $result);
  974. }
  975. /**
  976. * Tests that first can be called again on an already executed query
  977. *
  978. * @return void
  979. */
  980. public function testFirstCleanQuery()
  981. {
  982. $table = TableRegistry::get('articles', ['table' => 'articles']);
  983. $query = new Query($this->connection, $table);
  984. $query->select(['id'])->toArray();
  985. $first = $query->hydrate(false)->first();
  986. $this->assertEquals(['id' => 1], $first);
  987. $this->assertEquals(1, $query->clause('limit'));
  988. }
  989. /**
  990. * Tests that first() will not execute the same query twice
  991. *
  992. * @return void
  993. */
  994. public function testFirstSameResult()
  995. {
  996. $table = TableRegistry::get('articles', ['table' => 'articles']);
  997. $query = new Query($this->connection, $table);
  998. $query->select(['id'])->toArray();
  999. $first = $query->hydrate(false)->first();
  1000. $resultSet = $query->all();
  1001. $this->assertEquals(['id' => 1], $first);
  1002. $this->assertSame($resultSet, $query->all());
  1003. }
  1004. /**
  1005. * Tests that first can be called against a query with a mapReduce
  1006. *
  1007. * @return void
  1008. */
  1009. public function testFirstMapReduce()
  1010. {
  1011. $map = function ($row, $key, $mapReduce) {
  1012. $mapReduce->emitIntermediate($row['id'], 'id');
  1013. };
  1014. $reduce = function ($values, $key, $mapReduce) {
  1015. $mapReduce->emit(array_sum($values));
  1016. };
  1017. $table = TableRegistry::get('articles', ['table' => 'articles']);
  1018. $query = new Query($this->connection, $table);
  1019. $query->select(['id'])
  1020. ->hydrate(false)
  1021. ->mapReduce($map, $reduce);
  1022. $first = $query->first();
  1023. $this->assertEquals(1, $first);
  1024. }
  1025. /**
  1026. * Tests that first can be called on an unbuffered query
  1027. *
  1028. * @return void
  1029. */
  1030. public function testFirstUnbuffered()
  1031. {
  1032. $table = TableRegistry::get('Articles');
  1033. $query = new Query($this->connection, $table);
  1034. $query->select(['id']);
  1035. $first = $query->hydrate(false)
  1036. ->bufferResults(false)->first();
  1037. $this->assertEquals(['id' => 1], $first);
  1038. }
  1039. /**
  1040. * Testing hydrating a result set into Entity objects
  1041. *
  1042. * @return void
  1043. */
  1044. public function testHydrateSimple()
  1045. {
  1046. $table = TableRegistry::get('articles', ['table' => 'articles']);
  1047. $query = new Query($this->connection, $table);
  1048. $results = $query->select()->toArray();
  1049. $this->assertCount(3, $results);
  1050. foreach ($results as $r) {
  1051. $this->assertInstanceOf('Cake\ORM\Entity', $r);
  1052. }
  1053. $first = $results[0];
  1054. $this->assertEquals(1, $first->id);
  1055. $this->assertEquals(1, $first->author_id);
  1056. $this->assertEquals('First Article', $first->title);
  1057. $this->assertEquals('First Article Body', $first->body);
  1058. $this->assertEquals('Y', $first->published);
  1059. }
  1060. /**
  1061. * Tests that has many results are also hydrated correctly
  1062. *
  1063. * @return void
  1064. */
  1065. public function testHydrateHasMany()
  1066. {
  1067. $table = TableRegistry::get('authors');
  1068. TableRegistry::get('articles');
  1069. $table->hasMany('articles', [
  1070. 'propertyName' => 'articles',
  1071. 'sort' => ['articles.id' => 'asc']
  1072. ]);
  1073. $query = new Query($this->connection, $table);
  1074. $results = $query->select()
  1075. ->contain('articles')
  1076. ->toArray();
  1077. $first = $results[0];
  1078. foreach ($first->articles as $r) {
  1079. $this->assertInstanceOf('Cake\ORM\Entity', $r);
  1080. }
  1081. $this->assertCount(2, $first->articles);
  1082. $expected = [
  1083. 'id' => 1,
  1084. 'title' => 'First Article',
  1085. 'body' => 'First Article Body',
  1086. 'author_id' => 1,
  1087. 'published' => 'Y',
  1088. ];
  1089. $this->assertEquals($expected, $first->articles[0]->toArray());
  1090. $expected = [
  1091. 'id' => 3,
  1092. 'title' => 'Third Article',
  1093. 'author_id' => 1,
  1094. 'body' => 'Third Article Body',
  1095. 'published' => 'Y',
  1096. ];
  1097. $this->assertEquals($expected, $first->articles[1]->toArray());
  1098. }
  1099. /**
  1100. * Tests that belongsToMany associations are also correctly hydrated
  1101. *
  1102. * @return void
  1103. */
  1104. public function testHydrateBelongsToMany()
  1105. {
  1106. $table = TableRegistry::get('Articles');
  1107. TableRegistry::get('Tags');
  1108. TableRegistry::get('ArticlesTags', [
  1109. 'table' => 'articles_tags'
  1110. ]);
  1111. $table->belongsToMany('Tags');
  1112. $query = new Query($this->connection, $table);
  1113. $results = $query
  1114. ->select()
  1115. ->contain('Tags')
  1116. ->toArray();
  1117. $first = $results[0];
  1118. foreach ($first->tags as $r) {
  1119. $this->assertInstanceOf('Cake\ORM\Entity', $r);
  1120. }
  1121. $this->assertCount(2, $first->tags);
  1122. $expected = [
  1123. 'id' => 1,
  1124. 'name' => 'tag1',
  1125. '_joinData' => ['article_id' => 1, 'tag_id' => 1]
  1126. ];
  1127. $this->assertEquals($expected, $first->tags[0]->toArray());
  1128. $expected = [
  1129. 'id' => 2,
  1130. 'name' => 'tag2',
  1131. '_joinData' => ['article_id' => 1, 'tag_id' => 2]
  1132. ];
  1133. $this->assertEquals($expected, $first->tags[1]->toArray());
  1134. }
  1135. /**
  1136. * Tests that belongsToMany associations are also correctly hydrated
  1137. *
  1138. * @return void
  1139. */
  1140. public function testFormatResultsBelongsToMany()
  1141. {
  1142. $table = TableRegistry::get('Articles');
  1143. TableRegistry::get('Tags');
  1144. $articlesTags = TableRegistry::get('ArticlesTags', [
  1145. 'table' => 'articles_tags'
  1146. ]);
  1147. $table->belongsToMany('Tags');
  1148. $articlesTags
  1149. ->eventManager()
  1150. ->attach(function ($event, $query) {
  1151. $query->formatResults(function ($results) {
  1152. return $results;
  1153. });
  1154. }, 'Model.beforeFind');
  1155. $query = new Query($this->connection, $table);
  1156. $results = $query
  1157. ->select()
  1158. ->contain('Tags')
  1159. ->toArray();
  1160. $first = $results[0];
  1161. foreach ($first->tags as $r) {
  1162. $this->assertInstanceOf('Cake\ORM\Entity', $r);
  1163. }
  1164. $this->assertCount(2, $first->tags);
  1165. $expected = [
  1166. 'id' => 1,
  1167. 'name' => 'tag1',
  1168. '_joinData' => ['article_id' => 1, 'tag_id' => 1]
  1169. ];
  1170. $this->assertEquals($expected, $first->tags[0]->toArray());
  1171. $expected = [
  1172. 'id' => 2,
  1173. 'name' => 'tag2',
  1174. '_joinData' => ['article_id' => 1, 'tag_id' => 2]
  1175. ];
  1176. $this->assertEquals($expected, $first->tags[1]->toArray());
  1177. }
  1178. /**
  1179. * Tests that belongsTo relations are correctly hydrated
  1180. *
  1181. * @dataProvider strategiesProviderBelongsTo
  1182. * @return void
  1183. */
  1184. public function testHydrateBelongsTo($strategy)
  1185. {
  1186. $table = TableRegistry::get('articles');
  1187. TableRegistry::get('authors');
  1188. $table->belongsTo('authors', ['strategy' => $strategy]);
  1189. $query = new Query($this->connection, $table);
  1190. $results = $query->select()
  1191. ->contain('authors')
  1192. ->order(['articles.id' => 'asc'])
  1193. ->toArray();
  1194. $this->assertCount(3, $results);
  1195. $first = $results[0];
  1196. $this->assertInstanceOf('Cake\ORM\Entity', $first->author);
  1197. $expected = ['id' => 1, 'name' => 'mariano'];
  1198. $this->assertEquals($expected, $first->author->toArray());
  1199. }
  1200. /**
  1201. * Tests that deeply nested associations are also hydrated correctly
  1202. *
  1203. * @dataProvider strategiesProviderBelongsTo
  1204. * @return void
  1205. */
  1206. public function testHydrateDeep($strategy)
  1207. {
  1208. $table = TableRegistry::get('authors');
  1209. $article = TableRegistry::get('articles');
  1210. $table->hasMany('articles', [
  1211. 'propertyName' => 'articles',
  1212. 'sort' => ['articles.id' => 'asc']
  1213. ]);
  1214. $article->belongsTo('authors', ['strategy' => $strategy]);
  1215. $query = new Query($this->connection, $table);
  1216. $results = $query->select()
  1217. ->contain(['articles' => ['authors']])
  1218. ->toArray();
  1219. $this->assertCount(4, $results);
  1220. $first = $results[0];
  1221. $this->assertInstanceOf('Cake\ORM\Entity', $first->articles[0]->author);
  1222. $expected = ['id' => 1, 'name' => 'mariano'];
  1223. $this->assertEquals($expected, $first->articles[0]->author->toArray());
  1224. $this->assertTrue(isset($results[3]->articles));
  1225. }
  1226. /**
  1227. * Tests that it is possible to use a custom entity class
  1228. *
  1229. * @return void
  1230. */
  1231. public function testHydrateCustomObject()
  1232. {
  1233. $class = $this->getMockClass('\Cake\ORM\Entity', ['fakeMethod']);
  1234. $table = TableRegistry::get('articles', [
  1235. 'table' => 'articles',
  1236. 'entityClass' => '\\' . $class
  1237. ]);
  1238. $query = new Query($this->connection, $table);
  1239. $results = $query->select()->toArray();
  1240. $this->assertCount(3, $results);
  1241. foreach ($results as $r) {
  1242. $this->assertInstanceOf($class, $r);
  1243. }
  1244. $first = $results[0];
  1245. $this->assertEquals(1, $first->id);
  1246. $this->assertEquals(1, $first->author_id);
  1247. $this->assertEquals('First Article', $first->title);
  1248. $this->assertEquals('First Article Body', $first->body);
  1249. $this->assertEquals('Y', $first->published);
  1250. }
  1251. /**
  1252. * Tests that has many results are also hydrated correctly
  1253. * when specified a custom entity class
  1254. *
  1255. * @return void
  1256. */
  1257. public function testHydrateHasManyCustomEntity()
  1258. {
  1259. $authorEntity = $this->getMockClass('\Cake\ORM\Entity', ['foo']);
  1260. $articleEntity = $this->getMockClass('\Cake\ORM\Entity', ['foo']);
  1261. $table = TableRegistry::get('authors', [
  1262. 'entityClass' => '\\' . $authorEntity
  1263. ]);
  1264. TableRegistry::get('articles', [
  1265. 'entityClass' => '\\' . $articleEntity
  1266. ]);
  1267. $table->hasMany('articles', [
  1268. 'propertyName' => 'articles',
  1269. 'sort' => ['articles.id' => 'asc']
  1270. ]);
  1271. $query = new Query($this->connection, $table);
  1272. $results = $query->select()
  1273. ->contain('articles')
  1274. ->toArray();
  1275. $first = $results[0];
  1276. $this->assertInstanceOf($authorEntity, $first);
  1277. foreach ($first->articles as $r) {
  1278. $this->assertInstanceOf($articleEntity, $r);
  1279. }
  1280. $this->assertCount(2, $first->articles);
  1281. $expected = [
  1282. 'id' => 1,
  1283. 'title' => 'First Article',
  1284. 'body' => 'First Article Body',
  1285. 'author_id' => 1,
  1286. 'published' => 'Y',
  1287. ];
  1288. $this->assertEquals($expected, $first->articles[0]->toArray());
  1289. }
  1290. /**
  1291. * Tests that belongsTo relations are correctly hydrated into a custom entity class
  1292. *
  1293. * @return void
  1294. */
  1295. public function testHydrateBelongsToCustomEntity()
  1296. {
  1297. $authorEntity = $this->getMockClass('\Cake\ORM\Entity', ['foo']);
  1298. $table = TableRegistry::get('articles');
  1299. TableRegistry::get('authors', [
  1300. 'entityClass' => '\\' . $authorEntity
  1301. ]);
  1302. $table->belongsTo('authors');
  1303. $query = new Query($this->connection, $table);
  1304. $results = $query->select()
  1305. ->contain('authors')
  1306. ->order(['articles.id' => 'asc'])
  1307. ->toArray();
  1308. $first = $results[0];
  1309. $this->assertInstanceOf($authorEntity, $first->author);
  1310. }
  1311. /**
  1312. * Test getting counts from queries.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testCount()
  1317. {
  1318. $table = TableRegistry::get('articles');
  1319. $result = $table->find('all')->count();
  1320. $this->assertSame(3, $result);
  1321. $query = $table->find('all')
  1322. ->where(['id >' => 1])
  1323. ->limit(1);
  1324. $result = $query->count();
  1325. $this->assertSame(2, $result);
  1326. $result = $query->all();
  1327. $this->assertCount(1, $result);
  1328. $this->assertEquals(2, $result->first()->id);
  1329. }
  1330. /**
  1331. * Test getting counts from queries with contain.
  1332. *
  1333. * @return void
  1334. */
  1335. public function testCountWithContain()
  1336. {
  1337. $table = TableRegistry::get('Articles');
  1338. $table->belongsTo('Authors');
  1339. $result = $table->find('all')
  1340. ->contain([
  1341. 'Authors' => [
  1342. 'fields' => ['name']
  1343. ]
  1344. ])
  1345. ->count();
  1346. $this->assertSame(3, $result);
  1347. }
  1348. /**
  1349. * test count with a beforeFind.
  1350. *
  1351. * @return void
  1352. */
  1353. public function testCountBeforeFind()
  1354. {
  1355. $table = TableRegistry::get('Articles');
  1356. $table->hasMany('Comments');
  1357. $table->eventManager()
  1358. ->attach(function ($event, $query) {
  1359. $query
  1360. ->limit(1)
  1361. ->order(['Articles.title' => 'DESC']);
  1362. }, 'Model.beforeFind');
  1363. $query = $table->find();
  1364. $result = $query->count();
  1365. $this->assertSame(3, $result);
  1366. }
  1367. /**
  1368. * Test that count() returns correct results with group by.
  1369. *
  1370. * @return void
  1371. */
  1372. public function testCountWithGroup()
  1373. {
  1374. $table = TableRegistry::get('articles');
  1375. $query = $table->find('all');
  1376. $query->select(['author_id', 's' => $query->func()->sum('id')])
  1377. ->group(['author_id']);
  1378. $result = $query->count();
  1379. $this->assertEquals(2, $result);
  1380. }
  1381. /**
  1382. * Tests that it is possible to provide a callback for calculating the count
  1383. * of a query
  1384. *
  1385. * @return void
  1386. */
  1387. public function testCountWithCustomCounter()
  1388. {
  1389. $table = TableRegistry::get('articles');
  1390. $query = $table->find('all');
  1391. $query
  1392. ->select(['author_id', 's' => $query->func()->sum('id')])
  1393. ->where(['id >' => 2])
  1394. ->group(['author_id'])
  1395. ->counter(function ($q) use ($query) {
  1396. $this->assertNotSame($q, $query);
  1397. return $q->select([], true)->group([], true)->count();
  1398. });
  1399. $result = $query->count();
  1400. $this->assertEquals(1, $result);
  1401. }
  1402. /**
  1403. * Test update method.
  1404. *
  1405. * @return void
  1406. */
  1407. public function testUpdate()
  1408. {
  1409. $table = TableRegistry::get('articles');
  1410. $result = $table->query()
  1411. ->update()
  1412. ->set(['title' => 'First'])
  1413. ->execute();
  1414. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1415. $this->assertTrue($result->rowCount() > 0);
  1416. }
  1417. /**
  1418. * Test update method.
  1419. *
  1420. * @return void
  1421. */
  1422. public function testUpdateWithTableExpression()
  1423. {
  1424. $this->skipIf(!$this->connection->driver() instanceof \Cake\Database\Driver\Mysql);
  1425. $table = TableRegistry::get('articles');
  1426. $query = $table->query();
  1427. $result = $query->update($query->newExpr('articles, authors'))
  1428. ->set(['title' => 'First'])
  1429. ->where(['articles.author_id = authors.id'])
  1430. ->andWhere(['authors.name' => 'mariano'])
  1431. ->execute();
  1432. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1433. $this->assertTrue($result->rowCount() > 0);
  1434. }
  1435. /**
  1436. * Test insert method.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testInsert()
  1441. {
  1442. $table = TableRegistry::get('articles');
  1443. $result = $table->query()
  1444. ->insert(['title'])
  1445. ->values(['title' => 'First'])
  1446. ->values(['title' => 'Second'])
  1447. ->execute();
  1448. $result->closeCursor();
  1449. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1450. //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
  1451. if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
  1452. $this->assertEquals(2, $result->rowCount());
  1453. } else {
  1454. $this->assertEquals(-1, $result->rowCount());
  1455. }
  1456. }
  1457. /**
  1458. * Test delete method.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testDelete()
  1463. {
  1464. $table = TableRegistry::get('articles');
  1465. $result = $table->query()
  1466. ->delete()
  1467. ->where(['id >=' => 1])
  1468. ->execute();
  1469. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  1470. $this->assertTrue($result->rowCount() > 0);
  1471. }
  1472. /**
  1473. * Provides a list of…

Large files files are truncated, but you can click here to view the full file