FactoryLocatorTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.3.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Datasource;
  15. use Cake\Datasource\FactoryLocator;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * FactoryLocatorTest test case
  19. */
  20. class FactoryLocatorTest extends TestCase
  21. {
  22. /**
  23. * Test get factory
  24. *
  25. * @return void
  26. */
  27. public function testGet()
  28. {
  29. $this->assertInternalType('callable', FactoryLocator::get('Table'));
  30. }
  31. /**
  32. * Test get non existing factory
  33. *
  34. * @return void
  35. */
  36. public function testGetNonExisting()
  37. {
  38. $this->expectException(\InvalidArgumentException::class);
  39. $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.');
  40. FactoryLocator::get('Test');
  41. }
  42. /**
  43. * test add()
  44. *
  45. * @return void
  46. */
  47. public function testAdd()
  48. {
  49. FactoryLocator::add('Test', function ($name) {
  50. $mock = new \StdClass();
  51. $mock->name = $name;
  52. return $mock;
  53. });
  54. $this->assertInternalType('callable', FactoryLocator::get('Test'));
  55. }
  56. /**
  57. * test drop()
  58. *
  59. * @return void
  60. */
  61. public function testDrop()
  62. {
  63. $this->expectException(\InvalidArgumentException::class);
  64. $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.');
  65. FactoryLocator::drop('Test');
  66. FactoryLocator::get('Test');
  67. }
  68. /**
  69. * test loadModel() with plugin prefixed models
  70. *
  71. * Load model should not be called with Foo.Model Bar.Model Model
  72. * But if it is, the first call wins.
  73. *
  74. * @return void
  75. */
  76. public function testLoadModelPlugin()
  77. {
  78. $stub = new Stub();
  79. $stub->setProps('Articles');
  80. $stub->setModelType('Table');
  81. $result = $stub->loadModel('TestPlugin.Comments');
  82. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  83. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  84. $result = $stub->loadModel('Comments');
  85. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
  86. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $stub->Comments);
  87. }
  88. /**
  89. * test alternate model factories.
  90. *
  91. * @return void
  92. */
  93. public function testModelFactory()
  94. {
  95. $stub = new Stub();
  96. $stub->setProps('Articles');
  97. $stub->modelFactory('Table', function ($name) {
  98. $mock = new \StdClass();
  99. $mock->name = $name;
  100. return $mock;
  101. });
  102. $result = $stub->loadModel('Magic', 'Table');
  103. $this->assertInstanceOf('\StdClass', $result);
  104. $this->assertInstanceOf('\StdClass', $stub->Magic);
  105. $this->assertEquals('Magic', $stub->Magic->name);
  106. }
  107. /**
  108. * test alternate default model type.
  109. *
  110. * @return void
  111. */
  112. public function testModelType()
  113. {
  114. $stub = new Stub();
  115. $stub->setProps('Articles');
  116. FactoryLocator::add('Test', function ($name) {
  117. $mock = new \StdClass();
  118. $mock->name = $name;
  119. return $mock;
  120. });
  121. $stub->setModelType('Test');
  122. $result = $stub->loadModel('Magic');
  123. $this->assertInstanceOf('\StdClass', $result);
  124. $this->assertInstanceOf('\StdClass', $stub->Magic);
  125. $this->assertEquals('Magic', $stub->Magic->name);
  126. }
  127. /**
  128. * test MissingModelException being thrown
  129. *
  130. * @return void
  131. */
  132. public function testMissingModelException()
  133. {
  134. $this->expectException(\Cake\Datasource\Exception\MissingModelException::class);
  135. $this->expectExceptionMessage('Model class "Magic" of type "Test" could not be found.');
  136. $stub = new Stub();
  137. FactoryLocator::add('Test', function ($name) {
  138. return false;
  139. });
  140. $stub->loadModel('Magic', 'Test');
  141. }
  142. public function tearDown()
  143. {
  144. FactoryLocator::drop('Test');
  145. parent::tearDown();
  146. }
  147. }