ModelAwareTraitTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $result = $stub->loadModel();
  56. $this->assertInstanceOf('Cake\ORM\Table', $result);
  57. $this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);
  58. $result = $stub->loadModel('Comments');
  59. $this->assertInstanceOf('Cake\ORM\Table', $result);
  60. $this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
  61. }
  62. /**
  63. * test loadModel() with plugin prefixed models
  64. *
  65. * Load model should not be called with Foo.Model Bar.Model Model
  66. * But if it is, the first call wins.
  67. *
  68. * @return void
  69. */
  70. public function testLoadModelPlugin()
  71. {
  72. $stub = new Stub();
  73. $stub->setProps('Articles');
  74. $stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
  75. $result = $stub->loadModel('TestPlugin.Comments');
  76. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  77. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  78. $result = $stub->loadModel('Comments');
  79. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  80. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  81. }
  82. /**
  83. * test alternate model factories.
  84. *
  85. * @return void
  86. */
  87. public function testModelFactory()
  88. {
  89. $stub = new Stub();
  90. $stub->setProps('Articles');
  91. $stub->modelFactory('Test', function ($name) {
  92. $mock = new \StdClass();
  93. $mock->name = $name;
  94. return $mock;
  95. });
  96. $result = $stub->loadModel('Magic', 'Test');
  97. $this->assertInstanceOf('\StdClass', $result);
  98. $this->assertInstanceOf('\StdClass', $stub->Magic);
  99. $this->assertEquals('Magic', $stub->Magic->name);
  100. }
  101. /**
  102. * test MissingModelException being thrown
  103. *
  104. * @return void
  105. * @expectedException \Cake\Datasource\Exception\MissingModelException
  106. * @expectedExceptionMessage Model class "Magic" of type "Test" could not be found.
  107. */
  108. public function testMissingModelException()
  109. {
  110. $stub = new Stub();
  111. $stub->modelFactory('Test', function ($name) {
  112. return false;
  113. });
  114. $stub->loadModel('Magic', 'Test');
  115. }
  116. }