ModelAwareTraitTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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->assertNull($stub->modelClass);
  34. $stub->setProps('StubArticles');
  35. $this->assertEquals('StubArticles', $stub->modelClass);
  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 loadModel() with plugin prefixed models
  60. *
  61. * Load model should not be called with Foo.Model Bar.Model Model
  62. * But if it is, the first call wins.
  63. *
  64. * @return void
  65. */
  66. public function testLoadModelPlugin()
  67. {
  68. $stub = new Stub();
  69. $stub->setProps('Articles');
  70. $stub->setModelType('Table');
  71. $result = $stub->loadModel('TestPlugin.Comments');
  72. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  73. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  74. $result = $stub->loadModel('Comments');
  75. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  76. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  77. }
  78. /**
  79. * test alternate model factories.
  80. *
  81. * @return void
  82. */
  83. public function testModelFactory()
  84. {
  85. $stub = new Stub();
  86. $stub->setProps('Articles');
  87. $stub->modelFactory('Table', function ($name) {
  88. $mock = new \StdClass();
  89. $mock->name = $name;
  90. return $mock;
  91. });
  92. $result = $stub->loadModel('Magic', 'Table');
  93. $this->assertInstanceOf('stdClass', $result);
  94. $this->assertInstanceOf('stdClass', $stub->Magic);
  95. $this->assertEquals('Magic', $stub->Magic->name);
  96. }
  97. /**
  98. * test getModelType() and setModelType()
  99. *
  100. * @return void
  101. */
  102. public function testGetSetModelType()
  103. {
  104. $stub = new Stub();
  105. $stub->setProps('Articles');
  106. FactoryLocator::add('Test', function ($name) {
  107. $mock = new \StdClass();
  108. $mock->name = $name;
  109. return $mock;
  110. });
  111. $stub->setModelType('Test');
  112. $this->assertSame('Test', $stub->getModelType());
  113. }
  114. /**
  115. * test MissingModelException being thrown
  116. *
  117. * @return void
  118. */
  119. public function testMissingModelException()
  120. {
  121. $this->expectException(\Cake\Datasource\Exception\MissingModelException::class);
  122. $this->expectExceptionMessage('Model class "Magic" of type "Test" could not be found.');
  123. $stub = new Stub();
  124. FactoryLocator::add('Test', function ($name) {
  125. return false;
  126. });
  127. $stub->loadModel('Magic', 'Test');
  128. }
  129. public function tearDown()
  130. {
  131. FactoryLocator::drop('Test');
  132. parent::tearDown();
  133. }
  134. }