Test ControllerResolver and fix errors

This commit is contained in:
Jeremy Dormitzer 2019-01-08 17:23:04 -05:00
parent acb10b682d
commit d33c221a89
6 changed files with 154 additions and 17 deletions

View File

@ -9,11 +9,11 @@ use Symfony\Component\HttpFoundation\Request;
*/
class GetObjectController
{
private $objectService;
private $objectsService;
public function __construct( ObjectService $objectService )
public function __construct( ObjectsService $objectsService )
{
$this->objectService = $objectService;
$this->objectsService = $objectsService;
}
/**

View File

@ -1,7 +1,7 @@
<?php
namespace ActivityPub\Controllers\Inbox;
use ActivityPub\Objects\ObjectService;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\HttpFoundation\Request;
/**
@ -9,11 +9,11 @@ use Symfony\Component\HttpFoundation\Request;
*/
class DefaultInboxController
{
private $objectService;
private $ObjectsService;
public function __construct( ObjectService $objectService )
public function __construct( ObjectsService $ObjectsService )
{
$this->objectService = $objectService;
$this->ObjectsService = $ObjectsService;
}
/**

View File

@ -1,16 +1,16 @@
<?php
namespace ActivityPub\Controllers\Outbox;
use ActivityPub\Objects\ObjectService;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\HttpFoundation\Request;
class DefaultOutboxController
{
private $objectService;
private $objectsService;
public function __construct( ObjectService $objectService )
public function __construct( ObjectsService $objectsService )
{
$this->objectService = $objectService;
$this->objectsService = $objectsService;
}
/**

View File

@ -1,6 +1,10 @@
<?php
namespace ActivityPub\Http;
use ActivityPub\Controllers\GetObjectController;
use ActivityPub\Controllers\Inbox\DefaultInboxController;
use ActivityPub\Controllers\Outbox\DefaultOutboxController;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@ -12,7 +16,7 @@ class ControllerResolver implements ControllerResolverInterface
private $inboxControllers;
private $outboxControllers;
public function __construct( ObjectService $objectService )
public function __construct( ObjectsService $objectService )
{
$this->objectService = $objectService;
$this->inboxControllers = array();
@ -27,7 +31,7 @@ class ControllerResolver implements ControllerResolverInterface
*/
public function registerInboxController( Callable $controller, string $type )
{
$this->inboxControllers[$type] = $ccontroller;
$this->inboxControllers[$type] = $controller;
}
/**
@ -38,7 +42,7 @@ class ControllerResolver implements ControllerResolverInterface
*/
public function registerOutboxController( Callable $controller, string $type )
{
this->outboxControllers[$type] = $ccontroller;
$this->outboxControllers[$type] = $controller;
}
/**

View File

@ -18,7 +18,7 @@ abstract class SQLiteTestCase extends TestCase
protected function setUp()
{
parent::setUp();
$dbPath = dirname( __FILE__ ) . '/../db.sqlite';
$dbPath = $this->getDbPath();
if ( file_exists( $dbPath ) ) {
unlink( $dbPath );
}
@ -34,16 +34,21 @@ abstract class SQLiteTestCase extends TestCase
protected function tearDown()
{
parent::tearDown();
unlink( dirname( __FILE__ ) . '/../db.sqlite' );
unlink( $this->getDbPath() );
unset( $this->conn );
unset( $this->pdo );
}
protected function getDbPath()
{
return dirname( __FILE__ ) . '/db.sqlite';
}
final public function getConnection()
{
if ( $this->conn === null ) {
if ( $this->pdo === null ) {
$this->dbPath = dirname( __FILE__ ) . '/../db.sqlite';
$this->dbPath = $this->getDbPath();
$this->pdo = new \PDO( "sqlite:{$this->dbPath}" );
}
$this->conn = $this->createDefaultDBConnection( $this->pdo, $this->dbPath );

View File

@ -0,0 +1,128 @@
<?php
namespace ActivityPub\Test\Http;
use ActivityPub\Controllers\Inbox\DefaultInboxController;
use ActivityPub\Controllers\Outbox\DefaultOutboxController;
use ActivityPub\Controllers\GetObjectController;
use ActivityPub\Http\ControllerResolver;
use ActivityPub\Objects\ObjectsService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
class ControllerResolverTest extends TestCase
{
const INBOX_URI = 'https://example.com/inbox';
const OUTBOX_URI = 'https://example.com/outbox';
private $controllerResolver;
public function setUp()
{
$objectsService = $this->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->controllerResolver = new ControllerResolver( $objectsService );
}
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->assertInstanceOf( GetObjectController::class, $controller[0] );
$this->assertEquals( 'handle', $controller[1] );
}
public function testItChecksForType()
{
$request = Request::create( 'https://example.com/inbox', Request::METHOD_POST );
$this->expectException( BadRequestHttpException::class );
$controller = $this->controllerResolver->getController( $request );
}
public function testItReturnsDefaultInboxController()
{
$request = $this->createRequestWithBody(
'https://example.com/inbox', Request::METHOD_POST, array( 'type' => 'Foo' )
);
$controller = $this->controllerResolver->getController( $request );
$this->assertIsCallable( $controller );
$this->assertInstanceOf( DefaultInboxController::class, $controller[0] );
$this->assertEquals( 'handle', $controller[1] );
}
public function testItReturnsDefaultOutboxController()
{
$request = $this->createRequestWithBody(
'https://example.com/outbox', Request::METHOD_POST, array( 'type' => 'Foo' )
);
$controller = $this->controllerResolver->getController( $request );
$this->assertIsCallable( $controller );
$this->assertInstanceOf( DefaultOutboxController::class, $controller[0] );
$this->assertEquals( 'handle', $controller[1] );
}
public function testItRegistersNewInboxController()
{
$this->controllerResolver->registerInboxController( function() {
return 'barCallable';
}, 'Bar' );
$request = $this->createRequestWithBody(
'https://example.com/inbox', Request::METHOD_POST, array( 'type' => 'Bar' )
);
$controller = $this->controllerResolver->getController( $request );
$this->assertIsCallable( $controller );
$this->assertEquals( 'barCallable', call_user_func( $controller ) );
}
public function testItRegistersNewOutboxController()
{
$this->controllerResolver->registerOutboxController( function() {
return 'barCallable';
}, 'Bar' );
$request = $this->createRequestWithBody(
'https://example.com/outbox', Request::METHOD_POST, array( 'type' => 'Bar' )
);
$controller = $this->controllerResolver->getController( $request );
$this->assertIsCallable( $controller );
$this->assertEquals( 'barCallable', call_user_func( $controller ) );
}
public function testItDisallowsPostToInvalidUrl()
{
$request = $this->createRequestWithBody(
'https://example.com/object', Request::METHOD_POST, array( 'type' => 'Foo' )
);
$this->expectException( MethodNotAllowedHttpException::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 );
}
}
?>