ModelAwareTraitTest.php 4.9 KB

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