assertTrue(is_callable($factory) || $factory instanceof LocatorInterface); } /** * Test get non existing factory * * @return void */ public function testGetNonExisting() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.'); FactoryLocator::get('Test'); } /** * test add() * * @return void */ public function testAdd() { FactoryLocator::add('Test', function ($name) { $mock = new \stdClass(); $mock->name = $name; return $mock; }); $this->assertIsCallable(FactoryLocator::get('Test')); $locator = $this->getMockBuilder(LocatorInterface::class)->getMock(); FactoryLocator::add('MyType', $locator); $this->assertInstanceOf(LocatorInterface::class, FactoryLocator::get('MyType')); } public function testFactoryAddException() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( '`$factory` must be an instance of Cake\Datasource\Locator\LocatorInterface or a callable.' . ' Got type `string` instead.' ); FactoryLocator::add('Test', 'fail'); } /** * test drop() * * @return void */ public function testDrop() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unknown repository type "Test". Make sure you register a type before trying to use it.'); FactoryLocator::drop('Test'); FactoryLocator::get('Test'); } public function tearDown(): void { FactoryLocator::drop('Test'); FactoryLocator::drop('MyType'); parent::tearDown(); } }