FactoryLocatorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\LocatorInterface;
  18. use Cake\TestSuite\TestCase;
  19. use InvalidArgumentException;
  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. $factory = FactoryLocator::get('Table');
  33. $this->assertTrue(is_callable($factory) || $factory instanceof LocatorInterface);
  34. }
  35. /**
  36. * Test get non existing factory
  37. *
  38. * @return void
  39. */
  40. public function testGetNonExisting()
  41. {
  42. $this->expectException(\InvalidArgumentException::class);
  43. $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.');
  44. FactoryLocator::get('Test');
  45. }
  46. /**
  47. * test add()
  48. *
  49. * @return void
  50. */
  51. public function testAdd()
  52. {
  53. FactoryLocator::add('Test', function ($name) {
  54. $mock = new \stdClass();
  55. $mock->name = $name;
  56. return $mock;
  57. });
  58. $this->assertIsCallable(FactoryLocator::get('Test'));
  59. $locator = $this->getMockBuilder(LocatorInterface::class)->getMock();
  60. FactoryLocator::add('MyType', $locator);
  61. $this->assertInstanceOf(LocatorInterface::class, FactoryLocator::get('MyType'));
  62. }
  63. public function testFactoryAddException()
  64. {
  65. $this->expectException(InvalidArgumentException::class);
  66. $this->expectExceptionMessage(
  67. '`$factory` must be an instance of Cake\Datasource\LocatorInterface or a callable.'
  68. . ' Got type `string` instead.'
  69. );
  70. FactoryLocator::add('Test', 'fail');
  71. }
  72. /**
  73. * test drop()
  74. *
  75. * @return void
  76. */
  77. public function testDrop()
  78. {
  79. $this->expectException(\InvalidArgumentException::class);
  80. $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.');
  81. FactoryLocator::drop('Test');
  82. FactoryLocator::get('Test');
  83. }
  84. public function tearDown(): void
  85. {
  86. FactoryLocator::drop('Test');
  87. FactoryLocator::drop('MyType');
  88. parent::tearDown();
  89. }
  90. }