AssociationProxyTest.php 4.8 KB

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