ModelAwareTraitTest.php 4.0 KB

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