ModelAwareTraitTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 alternate model factories.
  64. *
  65. * @return void
  66. */
  67. public function testModelFactory()
  68. {
  69. $stub = new Stub();
  70. $stub->setProps('Articles');
  71. $stub->modelFactory('Test', function ($name) {
  72. $mock = new \StdClass();
  73. $mock->name = $name;
  74. return $mock;
  75. });
  76. $result = $stub->loadModel('Magic', 'Test');
  77. $this->assertInstanceOf('\StdClass', $result);
  78. $this->assertInstanceOf('\StdClass', $stub->Magic);
  79. $this->assertEquals('Magic', $stub->Magic->name);
  80. }
  81. /**
  82. * test MissingModelException being thrown
  83. *
  84. * @return void
  85. * @expectedException \Cake\Datasource\Exception\MissingModelException
  86. * @expectedExceptionMessage Model class "Magic" of type "Test" could not be found.
  87. */
  88. public function testMissingModelException()
  89. {
  90. $stub = new Stub();
  91. $stub->modelFactory('Test', function ($name) {
  92. return false;
  93. });
  94. $stub->loadModel('Magic', 'Test');
  95. }
  96. }