ModelAwareTraitTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Model;
  15. use Cake\Model\ModelAwareTrait;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * Testing stub.
  19. */
  20. class Stub {
  21. use ModelAwareTrait;
  22. public function setProps($name) {
  23. $this->_setModelClass($name);
  24. }
  25. }
  26. /**
  27. * ModelAwareTrait test case
  28. */
  29. class ModelAwareTraitTest extends TestCase {
  30. /**
  31. * Test set modelClass
  32. *
  33. * @return void
  34. */
  35. public function testSetModelClass() {
  36. $stub = new Stub();
  37. $this->assertNull($stub->modelClass);
  38. $stub->setProps('StubArticles');
  39. $this->assertEquals('StubArticles', $stub->modelClass);
  40. }
  41. /**
  42. * test loadModel()
  43. *
  44. * @return void
  45. */
  46. public function testLoadModel() {
  47. $stub = new Stub();
  48. $stub->setProps('Articles');
  49. $stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
  50. $result = $stub->loadModel();
  51. $this->assertInstanceOf('Cake\ORM\Table', $result);
  52. $this->assertInstanceOf('Cake\ORM\Table', $stub->Articles);
  53. $result = $stub->loadModel('Comments');
  54. $this->assertInstanceOf('Cake\ORM\Table', $result);
  55. $this->assertInstanceOf('Cake\ORM\Table', $stub->Comments);
  56. }
  57. /**
  58. * test alternate model factories.
  59. *
  60. * @return void
  61. */
  62. public function testModelFactory() {
  63. $stub = new Stub();
  64. $stub->setProps('Articles');
  65. $stub->modelFactory('Test', function ($name) {
  66. $mock = new \StdClass();
  67. $mock->name = $name;
  68. return $mock;
  69. });
  70. $result = $stub->loadModel('Magic', 'Test');
  71. $this->assertInstanceOf('\StdClass', $result);
  72. $this->assertInstanceOf('\StdClass', $stub->Magic);
  73. $this->assertEquals('Magic', $stub->Magic->name);
  74. }
  75. /**
  76. * test MissingModelException being thrown
  77. *
  78. * @return void
  79. * @expectedException Cake\Model\Exception\MissingModelException
  80. * @expectedExceptionMessage Model class "Magic" of type "Test" could not be found.
  81. */
  82. public function testMissingModelException() {
  83. $stub = new Stub();
  84. $stub->modelFactory('Test', function ($name) {
  85. return false;
  86. });
  87. $stub->loadModel('Magic', 'Test');
  88. }
  89. }