FactoryLocatorTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /**
  21. * FactoryLocatorTest test case
  22. */
  23. class FactoryLocatorTest extends TestCase
  24. {
  25. /**
  26. * Test get factory
  27. */
  28. public function testGet(): void
  29. {
  30. $factory = FactoryLocator::get('Table');
  31. $this->assertTrue(is_callable($factory) || $factory instanceof LocatorInterface);
  32. }
  33. /**
  34. * Test get nonexistent factory
  35. */
  36. public function testGetNonExistent(): void
  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. public function testAdd(): void
  46. {
  47. $locator = $this->getMockBuilder(LocatorInterface::class)->getMock();
  48. FactoryLocator::add('MyType', $locator);
  49. $this->assertInstanceOf(LocatorInterface::class, FactoryLocator::get('MyType'));
  50. }
  51. /**
  52. * test drop()
  53. */
  54. public function testDrop(): void
  55. {
  56. $this->expectException(InvalidArgumentException::class);
  57. $this->expectExceptionMessage('Unknown repository type `Test`. Make sure you register a type before trying to use it.');
  58. FactoryLocator::drop('Test');
  59. FactoryLocator::get('Test');
  60. }
  61. public function tearDown(): void
  62. {
  63. FactoryLocator::drop('Test');
  64. FactoryLocator::drop('MyType');
  65. parent::tearDown();
  66. }
  67. }