ModelAwareTraitTest.php 4.9 KB

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