ModelAwareTraitTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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->modelType('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->modelType('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. * @return void
  106. */
  107. public function testModelType()
  108. {
  109. $stub = new Stub();
  110. $stub->setProps('Articles');
  111. FactoryLocator::add('Test', function ($name) {
  112. $mock = new \StdClass();
  113. $mock->name = $name;
  114. return $mock;
  115. });
  116. $stub->modelType('Test');
  117. $result = $stub->loadModel('Magic');
  118. $this->assertInstanceOf('\StdClass', $result);
  119. $this->assertInstanceOf('\StdClass', $stub->Magic);
  120. $this->assertEquals('Magic', $stub->Magic->name);
  121. }
  122. /**
  123. * test getModelType() and setModelType()
  124. *
  125. * @return void
  126. */
  127. public function testGetSetModelType()
  128. {
  129. $stub = new Stub();
  130. $stub->setProps('Articles');
  131. FactoryLocator::add('Test', function ($name) {
  132. $mock = new \StdClass();
  133. $mock->name = $name;
  134. return $mock;
  135. });
  136. $stub->setModelType('Test');
  137. $this->assertSame('Test', $stub->getModelType());
  138. }
  139. /**
  140. * test MissingModelException being thrown
  141. *
  142. * @return void
  143. */
  144. public function testMissingModelException()
  145. {
  146. $this->expectException(\Cake\Datasource\Exception\MissingModelException::class);
  147. $this->expectExceptionMessage('Model class "Magic" of type "Test" could not be found.');
  148. $stub = new Stub();
  149. FactoryLocator::add('Test', function ($name) {
  150. return false;
  151. });
  152. $stub->loadModel('Magic', 'Test');
  153. }
  154. public function tearDown()
  155. {
  156. FactoryLocator::drop('Test');
  157. parent::tearDown();
  158. }
  159. }