FactoryLocatorTest.php 4.6 KB

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