ModelAwareTraitTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\ModelAwareTrait;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Testing stub.
  19. */
  20. class Stub
  21. {
  22. use ModelAwareTrait;
  23. public function setProps($name)
  24. {
  25. $this->_setModelClass($name);
  26. }
  27. }
  28. /**
  29. * ModelAwareTrait test case
  30. */
  31. class ModelAwareTraitTest extends TestCase
  32. {
  33. /**
  34. * Test set modelClass
  35. *
  36. * @return void
  37. */
  38. public function testSetModelClass()
  39. {
  40. $stub = new Stub();
  41. $this->assertNull($stub->modelClass);
  42. $stub->setProps('StubArticles');
  43. $this->assertEquals('StubArticles', $stub->modelClass);
  44. }
  45. /**
  46. * test loadModel()
  47. *
  48. * @return void
  49. */
  50. public function testLoadModel()
  51. {
  52. $stub = new Stub();
  53. $stub->setProps('Articles');
  54. $stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
  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->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
  76. $stub->modelType('Table');
  77. $result = $stub->loadModel('TestPlugin.Comments');
  78. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  79. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  80. $result = $stub->loadModel('Comments');
  81. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  82. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  83. }
  84. /**
  85. * test alternate model factories.
  86. *
  87. * @return void
  88. */
  89. public function testModelFactory()
  90. {
  91. $stub = new Stub();
  92. $stub->setProps('Articles');
  93. $stub->modelFactory('Test', function ($name) {
  94. $mock = new \StdClass();
  95. $mock->name = $name;
  96. return $mock;
  97. });
  98. $result = $stub->loadModel('Magic', 'Test');
  99. $this->assertInstanceOf('\StdClass', $result);
  100. $this->assertInstanceOf('\StdClass', $stub->Magic);
  101. $this->assertEquals('Magic', $stub->Magic->name);
  102. }
  103. /**
  104. * test alternate default model type.
  105. *
  106. * @return void
  107. */
  108. public function testModelType()
  109. {
  110. $stub = new Stub();
  111. $stub->setProps('Articles');
  112. $stub->modelFactory('Test', function ($name) {
  113. $mock = new \StdClass();
  114. $mock->name = $name;
  115. return $mock;
  116. });
  117. $stub->modelType('Test');
  118. $result = $stub->loadModel('Magic');
  119. $this->assertInstanceOf('\StdClass', $result);
  120. $this->assertInstanceOf('\StdClass', $stub->Magic);
  121. $this->assertEquals('Magic', $stub->Magic->name);
  122. }
  123. /**
  124. * test MissingModelException being thrown
  125. *
  126. * @return void
  127. * @expectedException \Cake\Datasource\Exception\MissingModelException
  128. * @expectedExceptionMessage Model class "Magic" of type "Test" could not be found.
  129. */
  130. public function testMissingModelException()
  131. {
  132. $stub = new Stub();
  133. $stub->modelFactory('Test', function ($name) {
  134. return false;
  135. });
  136. $stub->loadModel('Magic', 'Test');
  137. }
  138. }