prophesize(Resolver::class); $resolverProphecy->getControllerClass()->willReturn($class); $request = new \common_http_Request('http:://fixture.test'); $sessionBuilderProphecy = $this->prophesize(SessionBuilder::class); $sessionBuilderProphecy->isApplicable($request)->willReturn(false); $sessionBuilderProphecy2 = $this->prophesize(SessionBuilder::class); $sessionBuilderProphecy2->isApplicable($request)->willReturn(true); $sessionBuilderProphecy2->getSession($request)->shouldBeCalled(); $service = new RestSessionFactoryTester(); $service->builders = [ $sessionBuilderProphecy->reveal(), $sessionBuilderProphecy2->reveal() ]; $service->createSessionFromRequest($request, $resolverProphecy->reveal()); $this->assertEquals(1, $service->isSessionStarted); } public function testCreateSessionFromRequestWithInalidRestController() { $class = \stdClass::class; $resolverMock = $this->prophesize(Resolver::class); $resolverMock->getControllerClass()->willReturn($class); $request = new \common_http_Request('http:://fixture.test'); $service = new RestSessionFactoryTester(); $this->assertFalse($service->createSessionFromRequest($request, $resolverMock->reveal())); } public function testCreateSessionFromRequestWithNoBuilders() { $this->expectException(common_Exception::class); $class = TestRest::class; $resolverProphecy = $this->prophesize(Resolver::class); $resolverProphecy->getControllerClass()->willReturn($class); $request = new \common_http_Request('http:://fixture.test'); $service = new RestSessionFactoryTester([ RestSessionFactoryTester::OPTION_BUILDERS => [] ]); $service->createSessionFromRequest($request, $resolverProphecy->reveal()); } public function testCreateSessionFromRequestWithNoApplicableBuilders() { $this->expectException(common_Exception::class); $class = TestRest::class; $resolverProphecy = $this->prophesize(Resolver::class); $resolverProphecy->getControllerClass()->willReturn($class); $request = new \common_http_Request('http:://fixture.test'); $mock = $this->prophesize(SessionBuilder::class); $mock->isApplicable($request)->willReturn(false); $mock2 = $this->prophesize(SessionBuilder::class); $mock2->isApplicable($request)->willReturn(false); $service = new RestSessionFactoryTester(); $service->builders = [ $mock->reveal(), $mock2->reveal() ]; $service->createSessionFromRequest($request, $resolverProphecy->reveal()); } public function testGetSessionBuilders() { $mock = $this->getMockForAbstractClass(SessionBuilder::class); $service = new RestSessionFactoryTester([ RestSessionFactoryTester::OPTION_BUILDERS => [ $mock ] ]); $this->assertEquals([$mock], $service->getSessionBuilders()); } public function testGetSessionBuildersWithInvalidBuilder() { $this->expectException(LogicException::class); $mock = $this->getMockForAbstractClass(SessionBuilder::class); $service = new RestSessionFactoryTester([ RestSessionFactoryTester::OPTION_BUILDERS => [ $mock, new \stdClass() ] ]); $this->assertEquals([$mock], $service->getSessionBuilders()); } /** * @dataProvider testIsRestControllerProvider * @param $class * @param $expected */ public function testIsRestController($class, $expected) { $service = new RestSessionFactoryTester(); $resolverProphecy = $this->prophesize(Resolver::class); $resolverProphecy->getControllerClass()->willReturn($class); $this->assertEquals($expected, $service->isRestController($resolverProphecy->reveal())); } /** * Provider of testIsRestController *@doesNotPerformAssertions * @return array */ public function testIsRestControllerProvider() { return [ [TestRest::class, true,], ['toto', false], [SubTestRest::class, true,], [RestSessionFactoryTest::class, false,], [RestSessionFactoryTest::class, false,], ]; } } class TestRest extends \tao_actions_RestController { } class SubTestRest extends TestRest { } class RestSessionFactoryTester extends RestSessionFactory { public function isRestController(Resolver $resolver): bool { return parent::isRestController($resolver); } public $builders; public $isSessionStarted = 0; public function getSessionBuilders() { if ($this->builders) { return $this->builders; } return parent::getSessionBuilders(); } public function startSession($session) { $this->isSessionStarted = 1; } }