From 0dc3bbe27587ba62871853a06b51b9f38a220d7c Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 10 Jan 2019 10:20:05 -0500 Subject: [PATCH] Test the GetObjectController --- test/Controllers/GetObjectControllerTest.php | 72 ++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/Controllers/GetObjectControllerTest.php diff --git a/test/Controllers/GetObjectControllerTest.php b/test/Controllers/GetObjectControllerTest.php new file mode 100644 index 0000000..b807ee7 --- /dev/null +++ b/test/Controllers/GetObjectControllerTest.php @@ -0,0 +1,72 @@ + array( + 'id' => 'https://example.com/objects/1', + 'object' => array( + 'id' => 'https://example.com/objects/2', + 'type' => 'Note', + ), + 'type' => 'Create', + ), + ); + + private $getObjectController; + + public function setUp() + { + $objectsService = $this->createMock( ObjectsService::class ); + $objectsService->method( 'getObject' )->will( + $this->returnCallback( function( $uri ) { + if ( array_key_exists( $uri, self::OBJECTS ) ) { + return $this->objectFromArray( self::OBJECTS[$uri] ); + } + }) + ); + $this->getObjectController = new GetObjectController( $objectsService ); + } + + private function objectFromArray( $array ) { + $object = new ActivityPubObject(); + foreach ( $array as $name => $value ) { + if ( is_array( $value ) ) { + $child = $this->objectFromArray( $value ); + Field::withObject( $object, $name, $child ); + } else { + Field::withValue( $object, $name, $value ); + } + } + return $object; + } + + public function testItRendersPersistedObject() + { + $request = Request::create( 'https://example.com/objects/1' ); + $response = $this->getObjectController->handle( $request ); + $this->assertNotNull( $response ); + $this->assertEquals( + json_encode( self::OBJECTS['https://example.com/objects/1'] ), + $response->getContent() + ); + $this->assertEquals( 'application/json', $response->headers->get( 'Content-Type' ) ); + } + + public function testItThrowsNotFound() + { + $request = Request::create( 'https://example.com/objects/notreal' ); + $this->expectException( NotFoundHttpException::class ); + $this->getObjectController->handle( $request ); + } +} +?>