ModelAwareTraitTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  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\Datasource;
  16. use Cake\Datasource\FactoryLocator;
  17. use Cake\Datasource\LocatorInterface;
  18. use Cake\Datasource\RepositoryInterface;
  19. use Cake\TestSuite\TestCase;
  20. use InvalidArgumentException;
  21. use TestApp\Model\Table\PaginatorPostsTable;
  22. use TestApp\Stub\Stub;
  23. /**
  24. * ModelAwareTrait test case
  25. */
  26. class ModelAwareTraitTest extends TestCase
  27. {
  28. /**
  29. * Test set modelClass
  30. *
  31. * @return void
  32. */
  33. public function testSetModelClass()
  34. {
  35. $stub = new Stub();
  36. $this->assertNull($stub->getModelClass());
  37. $stub->setProps('StubArticles');
  38. $this->assertEquals('StubArticles', $stub->getModelClass());
  39. }
  40. /**
  41. * test loadModel()
  42. *
  43. * @return void
  44. */
  45. public function testLoadModel()
  46. {
  47. $stub = new Stub();
  48. $stub->setProps('Articles');
  49. $stub->setModelType('Table');
  50. $result = $stub->loadModel();
  51. $this->assertInstanceOf('Cake\ORM\Table', $result);
  52. $this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);
  53. $result = $stub->loadModel('Comments');
  54. $this->assertInstanceOf('Cake\ORM\Table', $result);
  55. $this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
  56. $result = $stub->loadModel(PaginatorPostsTable::class);
  57. $this->assertInstanceOf(PaginatorPostsTable::class, $result);
  58. $this->assertInstanceOf(PaginatorPostsTable::class, $stub->PaginatorPosts);
  59. $this->assertSame('PaginatorPosts', $result->getAlias());
  60. }
  61. /**
  62. * Test that calling loadModel() without $modelClass argument when default
  63. * $modelClass property is empty generates exception.
  64. *
  65. * @return void
  66. */
  67. public function testLoadModelException()
  68. {
  69. $this->expectException(\UnexpectedValueException::class);
  70. $this->expectExceptionMessage('Default modelClass is empty');
  71. $stub = new Stub();
  72. $stub->setProps('');
  73. $stub->setModelType('Table');
  74. $stub->loadModel();
  75. }
  76. /**
  77. * test loadModel() with plugin prefixed models
  78. *
  79. * Load model should not be called with Foo.Model Bar.Model Model
  80. * But if it is, the first call wins.
  81. *
  82. * @return void
  83. */
  84. public function testLoadModelPlugin()
  85. {
  86. $stub = new Stub();
  87. $stub->setProps('Articles');
  88. $stub->setModelType('Table');
  89. $result = $stub->loadModel('TestPlugin.Comments');
  90. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  91. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  92. $result = $stub->loadModel('Comments');
  93. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  94. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  95. }
  96. /**
  97. * test alternate model factories.
  98. *
  99. * @return void
  100. */
  101. public function testModelFactory()
  102. {
  103. $stub = new Stub();
  104. $stub->setProps('Articles');
  105. $stub->modelFactory('Table', function ($name) {
  106. $mock = $this->getMockBuilder(RepositoryInterface::class)->getMock();
  107. $mock->name = $name;
  108. return $mock;
  109. });
  110. $result = $stub->loadModel('Magic', 'Table');
  111. $this->assertInstanceOf(RepositoryInterface::class, $result);
  112. $this->assertInstanceOf(RepositoryInterface::class, $stub->Magic);
  113. $this->assertSame('Magic', $stub->Magic->name);
  114. $locator = $this->getMockBuilder(LocatorInterface::class)->getMock();
  115. $mock2 = $this->getMockBuilder(RepositoryInterface::class)->getMock();
  116. $mock2->alias = 'Foo';
  117. $locator->expects($this->any())
  118. ->method('get')
  119. ->willReturn($mock2);
  120. $stub->modelFactory('MyType', $locator);
  121. $result = $stub->loadModel('Foo', 'MyType');
  122. $this->assertInstanceOf(RepositoryInterface::class, $result);
  123. $this->assertSame('Foo', $stub->Foo->alias);
  124. }
  125. public function testModelFactoryException()
  126. {
  127. $this->expectException(InvalidArgumentException::class);
  128. $this->expectExceptionMessage(
  129. '`$factory` must be an instance of Cake\Datasource\LocatorInterface or a callable.'
  130. . ' Got type `string` instead.'
  131. );
  132. $stub = new Stub();
  133. $stub->modelFactory('MyType', 'fail');
  134. }
  135. /**
  136. * test getModelType() and setModelType()
  137. *
  138. * @return void
  139. */
  140. public function testGetSetModelType()
  141. {
  142. $stub = new Stub();
  143. $stub->setProps('Articles');
  144. FactoryLocator::add('Test', function ($name) {
  145. $mock = new \stdClass();
  146. $mock->name = $name;
  147. return $mock;
  148. });
  149. $stub->setModelType('Test');
  150. $this->assertSame('Test', $stub->getModelType());
  151. }
  152. /**
  153. * test MissingModelException being thrown
  154. *
  155. * @return void
  156. */
  157. public function testMissingModelException()
  158. {
  159. $this->expectException(\Cake\Datasource\Exception\MissingModelException::class);
  160. $this->expectExceptionMessage('Model class "Magic" of type "Test" could not be found.');
  161. $stub = new Stub();
  162. FactoryLocator::add('Test', function ($name) {
  163. return false;
  164. });
  165. $stub->loadModel('Magic', 'Test');
  166. }
  167. public function tearDown(): void
  168. {
  169. FactoryLocator::drop('Test');
  170. parent::tearDown();
  171. }
  172. }