AssociationProxyTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Tests that it is possible to get associations as a property
  33. *
  34. * @return void
  35. */
  36. public function testAssociationAsProperty()
  37. {
  38. $articles = $this->getTableLocator()->get('articles');
  39. $articles->hasMany('comments');
  40. $articles->belongsTo('authors');
  41. $this->assertTrue(isset($articles->authors));
  42. $this->assertTrue(isset($articles->comments));
  43. $this->assertFalse(isset($articles->posts));
  44. $this->assertSame($articles->getAssociation('authors'), $articles->authors);
  45. $this->assertSame($articles->getAssociation('comments'), $articles->comments);
  46. }
  47. /**
  48. * Tests that getting a bad property throws exception
  49. *
  50. * @return void
  51. */
  52. public function testGetBadAssociation()
  53. {
  54. $this->expectException(\RuntimeException::class);
  55. $this->expectExceptionMessage('You have not defined');
  56. $articles = $this->getTableLocator()->get('articles');
  57. $articles->posts;
  58. }
  59. /**
  60. * Test that find() with empty conditions generates valid SQL
  61. *
  62. * @return void
  63. */
  64. public function testFindEmptyConditions()
  65. {
  66. $table = $this->getTableLocator()->get('Users');
  67. $table->hasMany('Articles', [
  68. 'foreignKey' => 'author_id',
  69. 'conditions' => '',
  70. ]);
  71. $query = $table->Articles->find('list', ['limit' => 2]);
  72. $this->assertCount(2, $query->all());
  73. }
  74. /**
  75. * Tests that the proxied updateAll will preserve conditions set for the association
  76. *
  77. * @return void
  78. */
  79. public function testUpdateAllFromAssociation()
  80. {
  81. $articles = $this->getTableLocator()->get('articles');
  82. $comments = $this->getTableLocator()->get('comments');
  83. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  84. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  85. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  86. $this->assertEquals(3, $changed);
  87. }
  88. /**
  89. * Tests that the proxied deleteAll preserves conditions set for the association
  90. *
  91. * @return void
  92. */
  93. public function testDeleteAllFromAssociation()
  94. {
  95. $articles = $this->getTableLocator()->get('articles');
  96. $comments = $this->getTableLocator()->get('comments');
  97. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  98. $articles->comments->deleteAll(['article_id' => 1]);
  99. $remaining = $comments->find()->where(['article_id' => 1])->count();
  100. $this->assertEquals(1, $remaining);
  101. }
  102. /**
  103. * Tests that it is possible to get associations as a property
  104. *
  105. * @return void
  106. */
  107. public function testAssociationAsPropertyProxy()
  108. {
  109. $articles = $this->getTableLocator()->get('articles');
  110. $authors = $this->getTableLocator()->get('authors');
  111. $articles->belongsTo('authors');
  112. $authors->hasMany('comments');
  113. $this->assertTrue(isset($articles->authors->comments));
  114. $this->assertSame($authors->getAssociation('comments'), $articles->authors->comments);
  115. }
  116. /**
  117. * Tests that methods are proxied from the Association to the target table
  118. *
  119. * @return void
  120. */
  121. public function testAssociationMethodProxy()
  122. {
  123. $articles = $this->getTableLocator()->get('articles');
  124. $mock = $this->getMockBuilder('Cake\ORM\Table')
  125. ->setMethods(['crazy'])
  126. ->getMock();
  127. $articles->belongsTo('authors', [
  128. 'targetTable' => $mock,
  129. ]);
  130. $mock->expects($this->once())->method('crazy')
  131. ->with('a', 'b')
  132. ->will($this->returnValue('thing'));
  133. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  134. }
  135. }