ModelAwareTraitTest.php 5.0 KB

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