FactoryLocatorTest.php 2.7 KB

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