FactoryLocatorTest.php 4.7 KB

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