AssociationProxyTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Tests the features related to proxying methods from the Association
  19. * class to the Table class
  20. */
  21. class AssociationProxyTest extends TestCase
  22. {
  23. /**
  24. * Fixtures to be loaded
  25. *
  26. * @var array
  27. */
  28. public $fixtures = [
  29. 'core.Articles', 'core.Authors', 'core.Comments',
  30. ];
  31. /**
  32. * Teardown
  33. *
  34. * @return void
  35. */
  36. public function tearDown()
  37. {
  38. parent::tearDown();
  39. $this->getTableLocator()->clear();
  40. }
  41. /**
  42. * Tests that it is possible to get associations as a property
  43. *
  44. * @return void
  45. */
  46. public function testAssociationAsProperty()
  47. {
  48. $articles = $this->getTableLocator()->get('articles');
  49. $articles->hasMany('comments');
  50. $articles->belongsTo('authors');
  51. $this->assertTrue(isset($articles->authors));
  52. $this->assertTrue(isset($articles->comments));
  53. $this->assertFalse(isset($articles->posts));
  54. $this->assertSame($articles->getAssociation('authors'), $articles->authors);
  55. $this->assertSame($articles->getAssociation('comments'), $articles->comments);
  56. }
  57. /**
  58. * Tests that getting a bad property throws exception
  59. *
  60. * @return void
  61. */
  62. public function testGetBadAssociation()
  63. {
  64. $this->expectException(\RuntimeException::class);
  65. $this->expectExceptionMessage('You have not defined');
  66. $articles = $this->getTableLocator()->get('articles');
  67. $articles->posts;
  68. }
  69. /**
  70. * Test that find() with empty conditions generates valid SQL
  71. *
  72. * @return void
  73. */
  74. public function testFindEmptyConditions()
  75. {
  76. $table = $this->getTableLocator()->get('Users');
  77. $table->hasMany('Articles', [
  78. 'foreignKey' => 'author_id',
  79. 'conditions' => '',
  80. ]);
  81. $query = $table->Articles->find('list', ['limit' => 2]);
  82. $this->assertCount(2, $query->all());
  83. }
  84. /**
  85. * Tests that the proxied updateAll will preserve conditions set for the association
  86. *
  87. * @return void
  88. */
  89. public function testUpdateAllFromAssociation()
  90. {
  91. $articles = $this->getTableLocator()->get('articles');
  92. $comments = $this->getTableLocator()->get('comments');
  93. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  94. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  95. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  96. $this->assertEquals(3, $changed);
  97. }
  98. /**
  99. * Tests that the proxied deleteAll preserves conditions set for the association
  100. *
  101. * @return void
  102. */
  103. public function testDeleteAllFromAssociation()
  104. {
  105. $articles = $this->getTableLocator()->get('articles');
  106. $comments = $this->getTableLocator()->get('comments');
  107. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  108. $articles->comments->deleteAll(['article_id' => 1]);
  109. $remaining = $comments->find()->where(['article_id' => 1])->count();
  110. $this->assertEquals(1, $remaining);
  111. }
  112. /**
  113. * Tests that it is possible to get associations as a property
  114. *
  115. * @return void
  116. */
  117. public function testAssociationAsPropertyProxy()
  118. {
  119. $articles = $this->getTableLocator()->get('articles');
  120. $authors = $this->getTableLocator()->get('authors');
  121. $articles->belongsTo('authors');
  122. $authors->hasMany('comments');
  123. $this->assertTrue(isset($articles->authors->comments));
  124. $this->assertSame($authors->getAssociation('comments'), $articles->authors->comments);
  125. }
  126. /**
  127. * Tests that methods are proxied from the Association to the target table
  128. *
  129. * @return void
  130. */
  131. public function testAssociationMethodProxy()
  132. {
  133. $articles = $this->getTableLocator()->get('articles');
  134. $mock = $this->getMockBuilder('Cake\ORM\Table')
  135. ->setMethods(['crazy'])
  136. ->getMock();
  137. $articles->belongsTo('authors', [
  138. 'targetTable' => $mock,
  139. ]);
  140. $mock->expects($this->once())->method('crazy')
  141. ->with('a', 'b')
  142. ->will($this->returnValue('thing'));
  143. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  144. }
  145. }