createMock( ObjectsService::class ); $objectsService->method( 'query' )->will( $this->returnCallback( function ( $query ) { if ( array_key_exists( 'inbox', $query ) && $query['inbox'] == self::INBOX_URI ) { return array( 'objectWithInbox' ); } if ( array_key_exists( 'outbox', $query ) && $query['outbox'] == self::OUTBOX_URI ) { return array( 'objectWithOutbox' ); } return array(); }) ); $this->getObjectController = $this->createMock( GetObjectController::class ); $this->inboxController = $this->createMock( InboxController::class ); $this->outboxController = $this->createMock( OutboxController::class ); $this->controllerResolver = new ControllerResolver( $objectsService, $this->getObjectController, $this->inboxController, $this->outboxController ); } private function createRequestWithBody( $uri, $method, $body ) { $json = json_encode( $body ); return Request::create($uri, $method, array(), array(), array(), array(), $json); } public function testItReturnsGetObjectController() { $request = Request::create( 'https://example.com/object', Request::METHOD_GET ); $controller = $this->controllerResolver->getController( $request ); $this->assertIsCallable( $controller ); $this->assertEquals( array( $this->getObjectController, 'handle' ), $controller ); } public function testItChecksForType() { $request = Request::create( 'https://example.com/inbox', Request::METHOD_POST ); $this->expectException( BadRequestHttpException::class ); $controller = $this->controllerResolver->getController( $request ); } public function testItReturnsInboxController() { $request = $this->createRequestWithBody( 'https://example.com/inbox', Request::METHOD_POST, array( 'type' => 'Foo' ) ); $controller = $this->controllerResolver->getController( $request ); $this->assertEquals( array( $this->inboxController, 'handle' ), $controller ); } public function testItReturnsDefaultOutboxController() { $request = $this->createRequestWithBody( 'https://example.com/outbox', Request::METHOD_POST, array( 'type' => 'Foo' ) ); $controller = $this->controllerResolver->getController( $request ); $this->assertEquals( array( $this->outboxController, 'handle' ), $controller ); } public function testItDisallowsPostToInvalidUrl() { $request = $this->createRequestWithBody( 'https://example.com/object', Request::METHOD_POST, array( 'type' => 'Foo' ) ); $this->expectException( NotFoundHttpException::class ); $this->controllerResolver->getController( $request ); } public function testItDisallowsNonGetPostMethods() { $request = $this->createRequestWithBody( 'https://example.com/inbox', Request::METHOD_PUT, array( 'type' => 'Foo' ) ); $this->expectException( MethodNotAllowedHttpException::class ); $this->controllerResolver->getController( $request ); } } ?>