ModelAwareTraitTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Datasource;
  15. use Cake\Datasource\FactoryLocator;
  16. use Cake\Datasource\ModelAwareTrait;
  17. use Cake\TestSuite\TestCase;
  18. use TestApp\Model\Table\PaginatorPostsTable;
  19. /**
  20. * Testing stub.
  21. */
  22. class Stub
  23. {
  24. use ModelAwareTrait;
  25. public function setProps($name)
  26. {
  27. $this->_setModelClass($name);
  28. }
  29. }
  30. /**
  31. * ModelAwareTrait test case
  32. */
  33. class ModelAwareTraitTest extends TestCase
  34. {
  35. /**
  36. * Test set modelClass
  37. *
  38. * @return void
  39. */
  40. public function testSetModelClass()
  41. {
  42. $stub = new Stub();
  43. $this->assertNull($stub->modelClass);
  44. $stub->setProps('StubArticles');
  45. $this->assertEquals('StubArticles', $stub->modelClass);
  46. }
  47. /**
  48. * test loadModel()
  49. *
  50. * @return void
  51. */
  52. public function testLoadModel()
  53. {
  54. $stub = new Stub();
  55. $stub->setProps('Articles');
  56. $stub->setModelType('Table');
  57. $result = $stub->loadModel();
  58. $this->assertInstanceOf('Cake\ORM\Table', $result);
  59. $this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);
  60. $result = $stub->loadModel('Comments');
  61. $this->assertInstanceOf('Cake\ORM\Table', $result);
  62. $this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
  63. $result = $stub->loadModel(PaginatorPostsTable::class);
  64. $this->assertInstanceOf(PaginatorPostsTable::class, $result);
  65. $this->assertInstanceOf(PaginatorPostsTable::class, $stub->PaginatorPosts);
  66. $this->assertSame('PaginatorPosts', $result->getAlias());
  67. }
  68. /**
  69. * test loadModel() with plugin prefixed models
  70. *
  71. * Load model should not be called with Foo.Model Bar.Model Model
  72. * But if it is, the first call wins.
  73. *
  74. * @return void
  75. */
  76. public function testLoadModelPlugin()
  77. {
  78. $stub = new Stub();
  79. $stub->setProps('Articles');
  80. $stub->setModelType('Table');
  81. $result = $stub->loadModel('TestPlugin.Comments');
  82. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  83. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  84. $result = $stub->loadModel('Comments');
  85. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  86. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  87. }
  88. /**
  89. * test alternate model factories.
  90. *
  91. * @return void
  92. */
  93. public function testModelFactory()
  94. {
  95. $stub = new Stub();
  96. $stub->setProps('Articles');
  97. $stub->modelFactory('Table', function ($name) {
  98. $mock = new \StdClass();
  99. $mock->name = $name;
  100. return $mock;
  101. });
  102. $result = $stub->loadModel('Magic', 'Table');
  103. $this->assertInstanceOf('\StdClass', $result);
  104. $this->assertInstanceOf('\StdClass', $stub->Magic);
  105. $this->assertEquals('Magic', $stub->Magic->name);
  106. }
  107. /**
  108. * test alternate default model type.
  109. *
  110. * @group deprecated
  111. * @return void
  112. */
  113. public function testModelType()
  114. {
  115. $this->deprecated(function () {
  116. $stub = new Stub();
  117. $stub->setProps('Articles');
  118. FactoryLocator::add('Test', function ($name) {
  119. $mock = new \StdClass();
  120. $mock->name = $name;
  121. return $mock;
  122. });
  123. $stub->modelType('Test');
  124. $result = $stub->loadModel('Magic');
  125. $this->assertInstanceOf('\StdClass', $result);
  126. $this->assertInstanceOf('\StdClass', $stub->Magic);
  127. $this->assertEquals('Magic', $stub->Magic->name);
  128. });
  129. }
  130. /**
  131. * test getModelType() and setModelType()
  132. *
  133. * @return void
  134. */
  135. public function testGetSetModelType()
  136. {
  137. $stub = new Stub();
  138. $stub->setProps('Articles');
  139. FactoryLocator::add('Test', function ($name) {
  140. $mock = new \StdClass();
  141. $mock->name = $name;
  142. return $mock;
  143. });
  144. $stub->setModelType('Test');
  145. $this->assertSame('Test', $stub->getModelType());
  146. }
  147. /**
  148. * test MissingModelException being thrown
  149. *
  150. * @return void
  151. */
  152. public function testMissingModelException()
  153. {
  154. $this->expectException(\Cake\Datasource\Exception\MissingModelException::class);
  155. $this->expectExceptionMessage('Model class "Magic" of type "Test" could not be found.');
  156. $stub = new Stub();
  157. FactoryLocator::add('Test', function ($name) {
  158. return false;
  159. });
  160. $stub->loadModel('Magic', 'Test');
  161. }
  162. public function tearDown()
  163. {
  164. FactoryLocator::drop('Test');
  165. parent::tearDown();
  166. }
  167. }