ModelAwareTraitTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.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 MissingModelException being thrown
  124. *
  125. * @return void
  126. * @expectedException \Cake\Datasource\Exception\MissingModelException
  127. * @expectedExceptionMessage Model class "Magic" of type "Test" could not be found.
  128. */
  129. public function testMissingModelException()
  130. {
  131. $stub = new Stub();
  132. FactoryLocator::add('Test', function ($name) {
  133. return false;
  134. });
  135. $stub->loadModel('Magic', 'Test');
  136. }
  137. public function tearDown()
  138. {
  139. FactoryLocator::drop('Test');
  140. parent::tearDown();
  141. }
  142. }