ModelAwareTraitTest.php 5.9 KB

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