Use @dataProvider for all tests

This commit is contained in:
Jeremy Dormitzer 2019-04-11 23:41:25 -04:00
parent 1bb7fef9cd
commit c6305508bf
22 changed files with 1093 additions and 946 deletions

View File

@ -69,10 +69,10 @@ class AcceptHandlerTest extends APTestCase
);
}
public function testHandleInbox()
public function provideTestHandleInbox()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -102,8 +102,8 @@ class AcceptHandlerTest extends APTestCase
'expectedNewFollowing' => array(
'id' => 'https://elsewhere.com/actors/1',
)
),
array(
) ),
array( array(
'id' => 'acceptForSomeoneElsesFollow',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -130,8 +130,8 @@ class AcceptHandlerTest extends APTestCase
) ) )
)
),
),
array(
) ),
array( array(
'id' => 'noFollowingTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -158,48 +158,53 @@ class AcceptHandlerTest extends APTestCase
'expectedNewFollowing' => array(
'id' => 'https://elsewhere.com/actors/1',
)
),
) ),
);
foreach( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewFollowing', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewFollowing'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$acceptHandler = new AcceptHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $acceptHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}
public function testHandleOutbox()
/**
* @dataProvider provideTestHandleInbox
*/
public function testHandleInbox( $testCase )
{
$testCases = array(
array(
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewFollowing', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewFollowing'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$acceptHandler = new AcceptHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $acceptHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
public function provideTestHandleOutbox()
{
return array(
array( array(
'id' => 'notAutoAccepted',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -231,8 +236,8 @@ class AcceptHandlerTest extends APTestCase
'expectedNewFollower' => array(
'id' => 'https://elsewhere.com/actors/1',
)
),
array(
) ),
array( array(
'id' => 'autoAccepted',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -277,8 +282,8 @@ class AcceptHandlerTest extends APTestCase
'expectedNewFollower' => array(
'id' => 'https://elsewhere.com/actors/1',
)
),
array(
) ),
array( array(
'id' => 'noFollowersCollection',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -304,42 +309,47 @@ class AcceptHandlerTest extends APTestCase
'expectedNewFollower' => array(
'id' => 'https://elsewhere.com/actors/1',
)
),
) ),
);
foreach( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewFollower', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewFollower'] )
);
}
/**
* @dataProvider provideTestHandleOutbox
*/
public function testHandleOutbox( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
return null;
}
$contextProvider = new ContextProvider();
$acceptHandler = new AcceptHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $acceptHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewFollower', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewFollower'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$acceptHandler = new AcceptHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $acceptHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}

View File

@ -26,10 +26,10 @@ class AddHandlerTest extends APTestCase
);
}
public function testHandleAdd()
public function provideTestHandleAdd()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -58,8 +58,8 @@ class AddHandlerTest extends APTestCase
'expectedNewItem' => array(
'id' => 'https://elsewhere.com/notes/1',
)
),
array(
) ),
array( array(
'id' => 'outboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -88,8 +88,8 @@ class AddHandlerTest extends APTestCase
'expectedNewItem' => array(
'id' => 'https://example.com/notes/1',
)
),
array(
) ),
array( array(
'id' => 'notAuthorizedTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -116,40 +116,45 @@ class AddHandlerTest extends APTestCase
)
),
'expectedException' => AccessDeniedHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewItem', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewItem'] )
);
}
/**
* @dataProvider provideTestHandleAdd
*/
public function testHandleAdd( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
return null;
}
$addHandler = new AddHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $addHandler );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewItem', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewItem'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$addHandler = new AddHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $addHandler );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}

View File

@ -29,10 +29,10 @@ class AnnounceHandlerTest extends APTestCase
);
}
public function testAnnounceHandler()
public function provideTestAnnounceHandler()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -58,8 +58,8 @@ class AnnounceHandlerTest extends APTestCase
'type' => 'Announce',
'object' => 'https://example.com/notes/withshares',
)
),
array(
) ),
array( array(
'id' => 'generatesSharesCollectionTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -85,9 +85,15 @@ class AnnounceHandlerTest extends APTestCase
'type' => 'Announce',
'object' => 'https://example.com/notes/withoutshares',
)
),
) ),
);
foreach( $testCases as $testCase ) {
}
/**
* @dataProvider provideTestAnnounceHandler
*/
public function testAnnounceHandler( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
@ -120,6 +126,5 @@ class AnnounceHandlerTest extends APTestCase
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $announceHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}
}

View File

@ -46,10 +46,10 @@ class CreateHandlerTest extends APTestCase
$this->eventDispatcher->addSubscriber( $createHandler );
}
public function testCreateHandler()
public function provideTestCreateHandler()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -84,8 +84,8 @@ class CreateHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com/inbox', Request::METHOD_POST )
),
),
array(
) ),
array( array(
'id' => 'basicOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -125,8 +125,8 @@ class CreateHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com/outbox', Request::METHOD_POST )
),
),
array(
) ),
array( array(
'id' => 'copiesAudienceOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -198,8 +198,8 @@ class CreateHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com/outbox', Request::METHOD_POST )
),
),
array(
) ),
array( array(
'id' => 'moreCopiesAudienceOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -257,15 +257,20 @@ class CreateHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com/outbox', Request::METHOD_POST )
),
),
) ),
);
}
/**
* @dataProvider provideTestCreateHandler
*/
public function testCreateHandler( $testCase )
{
$event = $testCase['event'];
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
$this->assertEquals(
$testCase['expectedEvent'], $event, "Error on test $testCase[id]"
);
foreach ( $testCases as $testCase ) {
$event = $testCase['event'];
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
$this->assertEquals(
$testCase['expectedEvent'], $event, "Error on test $testCase[id]"
);
}
}
}

View File

@ -16,10 +16,10 @@ use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class DeleteHandlerTest extends APTestCase
{
public function testDeleteHandler()
public function provideTestDeleteHandler()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -45,8 +45,8 @@ class DeleteHandlerTest extends APTestCase
'type' => 'Tombstone',
'deleted' => '2014-01-05T21:31:40+0000',
),
),
array(
) ),
array( array(
'id' => 'basicOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -72,8 +72,8 @@ class DeleteHandlerTest extends APTestCase
'type' => 'Tombstone',
'deleted' => '2014-01-05T21:31:40+0000',
),
),
array(
) ),
array( array(
'id' => 'outboxAuthTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -93,8 +93,8 @@ class DeleteHandlerTest extends APTestCase
)
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
) ),
array( array(
'id' => 'inboxAuthTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -114,42 +114,47 @@ class DeleteHandlerTest extends APTestCase
)
),
'expectedException' => UnauthorizedHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$eventDispatcher = new EventDispatcher();
$dateTimeProvider = new TestDateTimeProvider( array(
'activities.delete' => DateTime::createFromFormat(
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:31:40 GMT'
),
) );
$objectsService = $this->getMockBuilder( ObjectsService::class )
->disableOriginalConstructor()
->setMethods( array( 'dereference', 'replace' ) )
->getMock();
$objectsService->method( 'dereference' )->will( $this->returnCallback(
function ( $id ) {
if ( array_key_exists( $id, self::getObjects() ) ) {
$objects = self::getObjects();
return TestActivityPubObject::fromArray( $objects[$id] );
}
return null;
}
/**
* @dataProvider provideTestDeleteHandler
*/
public function testDeleteHandler( $testCase )
{
$eventDispatcher = new EventDispatcher();
$dateTimeProvider = new TestDateTimeProvider( array(
'activities.delete' => DateTime::createFromFormat(
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:31:40 GMT'
),
) );
$objectsService = $this->getMockBuilder( ObjectsService::class )
->disableOriginalConstructor()
->setMethods( array( 'dereference', 'replace' ) )
->getMock();
$objectsService->method( 'dereference' )->will( $this->returnCallback(
function ( $id ) {
if ( array_key_exists( $id, self::getObjects() ) ) {
$objects = self::getObjects();
return TestActivityPubObject::fromArray( $objects[$id] );
}
) );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
} else {
$objectsService->expects( $this->once() )
->method( 'replace' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedTombstone'] )
);
return null;
}
$deleteHandler = new DeleteHandler( $dateTimeProvider, $objectsService );
$eventDispatcher->addSubscriber( $deleteHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
) );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
} else {
$objectsService->expects( $this->once() )
->method( 'replace' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedTombstone'] )
);
}
$deleteHandler = new DeleteHandler( $dateTimeProvider, $objectsService );
$eventDispatcher->addSubscriber( $deleteHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
private static function getObjects()

View File

@ -32,10 +32,10 @@ class LikeHandlerTest extends APTestCase
);
}
public function testHandleInbox()
public function provideTestHandleInbox()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -61,8 +61,8 @@ class LikeHandlerTest extends APTestCase
'type' => 'Like',
'object' => 'https://example.com/notes/haslikes'
),
),
array(
) ),
array( array(
'id' => 'dereferencedObjectInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -92,8 +92,8 @@ class LikeHandlerTest extends APTestCase
'id' => 'https://example.com/notes/haslikes',
),
),
),
array(
) ),
array( array(
'id' => 'itCreatesLikesInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -123,48 +123,53 @@ class LikeHandlerTest extends APTestCase
'id' => 'https://example.com/notes/nolikes',
),
),
),
) ),
);
foreach( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewLikes', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewLikes'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$likeHandler = new LikeHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $likeHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}
public function testHandleOutbox()
/**
* @dataProvider provideTestHandleInbox
*/
public function testHandleInbox( $testCase )
{
$testCases = array(
array(
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewLikes', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewLikes'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$likeHandler = new LikeHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $likeHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
public function provideTestHandleOutbox()
{
return array(
array( array(
'id' => 'basicOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -194,8 +199,8 @@ class LikeHandlerTest extends APTestCase
'expectedNewLiked' => array(
'id' => 'https://elsewhere.com/notes/1',
)
),
array(
) ),
array( array(
'id' => 'createsLikedCollectionOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -221,41 +226,46 @@ class LikeHandlerTest extends APTestCase
'expectedNewLiked' => array(
'id' => 'https://elsewhere.com/notes/1',
)
),
) ),
);
foreach( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewLiked', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewLiked'] )
);
}
/**
* @dataProvider provideTestHandleOutbox
*/
public function testHandleOutbox( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
return null;
}
$contextProvider = new ContextProvider();
$likeHandler = new LikeHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $likeHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
});
$objectsService->method( 'update')->willReturnCallback( function( $id, $arr ) {
return TestActivityPubObject::fromArray( $arr );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'addItem' ) )
->getMock();
if ( array_key_exists( 'expectedNewLiked', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'addItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedNewLiked'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'addItem' );
}
$contextProvider = new ContextProvider();
$likeHandler = new LikeHandler( $objectsService, $collectionsService, $contextProvider );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $likeHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}

View File

@ -11,12 +11,10 @@ use Symfony\Component\HttpFoundation\Request;
class NonActivityHandlerTest extends APTestCase
{
public function testNonActivityHandler()
public function provideTestNonActivityHandler()
{
$contextProvider = new ContextProvider();
$nonActivityHandler = new NonActivityHandler( $contextProvider );
$testCases = array(
array(
return array(
array( array(
'id' => 'testItWrapsNonObjectActivity',
'activity' => array(
'type' => 'Note'
@ -32,8 +30,8 @@ class NonActivityHandlerTest extends APTestCase
'type' => 'Note',
),
),
),
array(
) ),
array( array(
'id' => 'testItDoesNotWrapActivity',
'activity' => array(
'type' => 'Update'
@ -44,8 +42,8 @@ class NonActivityHandlerTest extends APTestCase
'expectedActivity' => array(
'type' => 'Update',
),
),
array(
) ),
array( array(
'id' => 'testItPassesAudience',
'activity' => array(
'type' => 'Note',
@ -88,20 +86,27 @@ class NonActivityHandlerTest extends APTestCase
'baz',
),
),
)
) )
);
}
/**
* @dataProvider provideTestNonActivityHandler
*/
public function testNonActivityHandler( $testCase )
{
$contextProvider = new ContextProvider();
$nonActivityHandler = new NonActivityHandler( $contextProvider );
$actor = $testCase['actor'];
$activity = $testCase['activity'];
$request = Request::create( 'https://example.com/whatever' );
$event = new OutboxActivityEvent( $activity, $actor, $request );
$nonActivityHandler->handle( $event );
$this->assertEquals(
$testCase['expectedActivity'],
$event->getActivity(),
"Error on test $testCase[id]"
);
foreach ( $testCases as $testCase ) {
$actor = $testCase['actor'];
$activity = $testCase['activity'];
$request = Request::create( 'https://example.com/whatever' );
$event = new OutboxActivityEvent( $activity, $actor, $request );
$nonActivityHandler->handle( $event );
$this->assertEquals(
$testCase['expectedActivity'],
$event->getActivity(),
"Error on test $testCase[id]"
);
}
}
}

View File

@ -26,10 +26,10 @@ class RemoveHandlerTest extends APTestCase
);
}
public function testHandleRemove()
public function provideTestHandleRemove()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -56,8 +56,8 @@ class RemoveHandlerTest extends APTestCase
)
),
'expectedRemovedItemId' => 'https://elsewhere.com/notes/1',
),
array(
) ),
array( array(
'id' => 'outboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -84,8 +84,8 @@ class RemoveHandlerTest extends APTestCase
)
),
'expectedRemovedItemId' => 'https://example.com/notes/1',
),
array(
) ),
array( array(
'id' => 'notAuthorizedTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -112,40 +112,45 @@ class RemoveHandlerTest extends APTestCase
)
),
'expectedException' => AccessDeniedHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
return null;
}
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'removeItem' ) )
->getMock();
if ( array_key_exists( 'expectedRemovedItemId', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'removeItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedRemovedItemId'] )
);
}
/**
* @dataProvider provideTestHandleRemove
*/
public function testHandleRemove( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference')->willReturnCallback( function( $id ) {
$objects = self::getObjects();
if ( array_key_exists( $id, $objects ) ) {
return TestActivityPubObject::fromArray( $objects[$id] );
} else {
$collectionsService->expects( $this->never() )
->method( 'removeItem' );
return null;
}
$removeHandler = new RemoveHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $removeHandler );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
});
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'removeItem' ) )
->getMock();
if ( array_key_exists( 'expectedRemovedItemId', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'removeItem' )
->with(
$this->anything(),
$this->equalTo( $testCase['expectedRemovedItemId'] )
);
} else {
$collectionsService->expects( $this->never() )
->method( 'removeItem' );
}
$removeHandler = new RemoveHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $removeHandler );
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}

View File

@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class UndoHandlerTest extends APTestCase
{
public function testUndoHandler()
public function provideTestUndoHandler()
{
$followForUndoFollowInbox = TestActivityPubObject::fromArray( array(
'id' => 'https://elsewhere.com/follows/1',
@ -67,8 +67,8 @@ class UndoHandlerTest extends APTestCase
'id' => 'https://elsewhere.com/notes/1',
),
) );
$testCases = array(
array(
return array(
array( array(
'id' => 'undoFollowInbox',
'objects' => array(
'https://elsewhere.com/follows/1' => $followForUndoFollowInbox,
@ -95,8 +95,8 @@ class UndoHandlerTest extends APTestCase
),
'collectionToRemoveFrom' => $followForUndoFollowInbox['object']['followers'],
'itemToRemove' => 'https://elsewhere.com/actors/1',
),
array(
) ),
array( array(
'id' => 'undoLikeInbox',
'objects' => array(
'https://elsewhere.com/likes/1' => $likeForUndoLikeInbox,
@ -116,8 +116,8 @@ class UndoHandlerTest extends APTestCase
),
'collectionToRemoveFrom' => $likeForUndoLikeInbox['object']['likes'],
'itemToRemove' => 'https://elsewhere.com/likes/1',
),
array(
) ),
array( array(
'id' => 'undoFollowOutbox',
'objects' => array(
'https://example.com/follows/1' => $followForUndoFollowOutbox,
@ -137,8 +137,8 @@ class UndoHandlerTest extends APTestCase
),
'collectionToRemoveFrom' => $followForUndoFollowOutbox['actor']['following'],
'itemToRemove' => 'https://elsewhere.com/actors/1',
),
array(
) ),
array( array(
'id' => 'undoLikeOutbox',
'objects' => array(
'https://example.com/likes/1' => $likeForUndoLikeOutbox,
@ -158,8 +158,8 @@ class UndoHandlerTest extends APTestCase
),
'collectionToRemoveFrom' => $likeForUndoLikeOutbox['actor']['liked'],
'itemToRemove' => $likeForUndoLikeOutbox['object']['id']
),
array(
) ),
array( array(
'id' => 'undoActorDoesNotMatchObjectActor',
'objects' => array(
'https://elsewhere.com/follows/1' => TestActivityPubObject::fromArray( array(
@ -185,43 +185,48 @@ class UndoHandlerTest extends APTestCase
Request::create( 'https://example.com/actors/1/inbox' )
),
'expectedException' => AccessDeniedHttpException::class,
) )
);
}
/**
* @dataProvider provideTestUndoHandler
*/
public function testUndoHandler( $testCase )
{
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference' )->will(
$this->returnCallback(
function( $id) use ( $testCase ) {
$objects = $testCase['objects'];
if ( array_key_exists( $id, $objects ) ) {
return $objects[$id];
} else {
return null;
}
}
)
);
foreach ( $testCases as $testCase ) {
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'dereference' )->will(
$this->returnCallback(
function( $id) use ( $testCase ) {
$objects = $testCase['objects'];
if ( array_key_exists( $id, $objects ) ) {
return $objects[$id];
} else {
return null;
}
}
)
);
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'removeItem' ) )
->getMock();
if ( array_key_exists( 'collectionToRemoveFrom', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'removeItem' )
->with(
$testCase['collectionToRemoveFrom'],
$testCase['itemToRemove']
);
} else {
$collectionsService->expects( $this->never() )->method( 'removeItem' );
}
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$undoHandler = new UndoHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $undoHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
$collectionsService = $this->getMockBuilder( CollectionsService::class )
->disableOriginalConstructor()
->setMethods( array( 'removeItem' ) )
->getMock();
if ( array_key_exists( 'collectionToRemoveFrom', $testCase ) ) {
$collectionsService->expects( $this->once() )
->method( 'removeItem' )
->with(
$testCase['collectionToRemoveFrom'],
$testCase['itemToRemove']
);
} else {
$collectionsService->expects( $this->never() )->method( 'removeItem' );
}
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$undoHandler = new UndoHandler( $objectsService, $collectionsService );
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addSubscriber( $undoHandler );
$eventDispatcher->dispatch( $testCase['eventName'], $testCase['event'] );
}
}

View File

@ -72,10 +72,10 @@ class UpdateHandlerTest extends APTestCase
);
}
public function testUpdateHandler()
public function provideTestUpdateHandler()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicInboxTest',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -115,8 +115,8 @@ class UpdateHandlerTest extends APTestCase
) ) )
)
),
),
array(
) ),
array( array(
'id' => 'basicOutboxTest',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -160,8 +160,8 @@ class UpdateHandlerTest extends APTestCase
) ) )
)
),
),
array(
) ),
array( array(
'id' => 'checksInboxAuth',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -185,8 +185,8 @@ class UpdateHandlerTest extends APTestCase
)
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
) ),
array( array(
'id' => 'checksOutboxAuth',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -210,19 +210,24 @@ class UpdateHandlerTest extends APTestCase
)
),
'expectedException' => UnauthorizedHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$event = $testCase['event'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$this->assertEquals(
$testCase['expectedEvent'], $event, "Error on test $testCase[id]"
);
}
}
/**
* @dataProvider provideTestUpdateHandler
*/
public function testUpdateHandler( $testCase )
{
$event = $testCase['event'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$this->assertEquals(
$testCase['expectedEvent'], $event, "Error on test $testCase[id]"
);
}
}
}

View File

@ -25,10 +25,10 @@ class ValidationHandlerTest extends APTestCase
$this->eventDispatcher->addSubscriber( $validationHandler );
}
public function testValidationHandler()
public function provideTestValidationHandler()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'inboxRequiredFields',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -41,8 +41,8 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: type,id,actor',
),
array(
) ),
array( array(
'id' => 'outboxRequiredFields',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -55,8 +55,8 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: type,actor',
),
array(
) ),
array( array(
'id' => 'inboxPassesValidActivity',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
@ -74,8 +74,8 @@ class ValidationHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com' )
),
),
array(
) ),
array( array(
'id' => 'outboxPassesValidActivity',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
@ -92,12 +92,13 @@ class ValidationHandlerTest extends APTestCase
) ),
Request::create( 'https://example.com' )
),
),
array(
) ),
array( array(
'id' => 'outboxRequiresObjectFields',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/creates/1',
'type' => 'Create',
'actor' => 'https://example.com/actor/1',
),
@ -109,13 +110,15 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: object',
),
array(
) ),
array( array(
'id' => 'inboxRequiresObjectFields',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/activities/1',
'type' => 'Create',
'actor' => 'https://elsewhere.com/actors/1'
),
TestActivityPubObject::fromArray( array(
'id' => 'https://notexample.com/actor/1',
@ -125,12 +128,15 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: object',
),
array(
) ),
array( array(
'id' => 'inboxRequiresTargetFields',
'eventName' => InboxActivityEvent::NAME,
'event' => new InboxActivityEvent(
array(
'id' => 'https://elsewhere.com/activities/1',
'actor' => 'https://elsewhere.com/actors/1',
'object' => 'https://elsewhere.com/collections/1',
'type' => 'Add',
),
TestActivityPubObject::fromArray( array(
@ -141,12 +147,14 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: target',
),
array(
) ),
array( array(
'id' => 'outboxRequiresTargetFields',
'eventName' => OutboxActivityEvent::NAME,
'event' => new OutboxActivityEvent(
array(
'id' => 'https://example.com/activities/1',
'object' => 'https://example.com/collections/1',
'type' => 'Remove',
'actor' => 'https://example.com/actor/1',
),
@ -158,21 +166,26 @@ class ValidationHandlerTest extends APTestCase
),
'expectedException' => BadRequestHttpException::class,
'expectedExceptionMessage' => 'Missing activity fields: target',
),
) ),
);
foreach ( $testCases as $testCase ) {
$event = $testCase['event'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$expectedExceptionMessage = '';
if ( array_key_exists( 'expectedExceptionMessage', $testCase ) ) {
$expectedExceptionMessage = $testCase['expectedExceptionMessage'];
}
$this->setExpectedException(
$testCase['expectedException'], $expectedExceptionMessage
);
}
/**
* @dataProvider provideTestValidationHandler
*/
public function testValidationHandler( $testCase )
{
$event = $testCase['event'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$expectedExceptionMessage = '';
if ( array_key_exists( 'expectedExceptionMessage', $testCase ) ) {
$expectedExceptionMessage = $testCase['expectedExceptionMessage'];
}
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
$this->setExpectedException(
$testCase['expectedException'], $expectedExceptionMessage
);
}
$this->eventDispatcher->dispatch( $testCase['eventName'], $event );
}
}

View File

@ -28,10 +28,10 @@ class AuthListenerTest extends APTestCase
) ) );
}
public function testAuthListener()
public function provideTestAuthListener()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'authFunction' => function () {
return 'https://example.com/actor/1';
@ -41,8 +41,8 @@ class AuthListenerTest extends APTestCase
'id' => 'https://example.com/actor/1',
) ),
),
),
array(
) ),
array( array(
'id' => 'existingActorTest',
'authFunction' => function () {
return 'https://example.com/actor/1';
@ -57,45 +57,50 @@ class AuthListenerTest extends APTestCase
'id' => 'https://example.com/actor/2',
) ),
),
),
array(
) ),
array( array(
'id' => 'defaultAuthTest',
'authFunction' => function () {
return false;
},
'expectedAttributes' => array(),
),
) ),
);
foreach ( $testCases as $testCase ) {
$event = $this->getEvent();
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
foreach ( $testCase['requestAttributes'] as $attribute => $value ) {
$event->getRequest()->attributes->set( $attribute, $value );
}
}
/**
* @dataProvider provideTestAuthListener
*/
public function testAuthListener( $testCase )
{
$event = $this->getEvent();
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
foreach ( $testCase['requestAttributes'] as $attribute => $value ) {
$event->getRequest()->attributes->set( $attribute, $value );
}
$authListener = new AuthListener(
$testCase['authFunction'], $this->objectsService
}
$authListener = new AuthListener(
$testCase['authFunction'], $this->objectsService
);
$authListener->checkAuth( $event );
foreach ( $testCase['expectedAttributes'] as $expectedKey => $expectedValue ) {
$this->assertTrue(
$event->getRequest()->attributes->has( $expectedKey ),
"Error on test $testCase[id]"
);
$authListener->checkAuth( $event );
foreach ( $testCase['expectedAttributes'] as $expectedKey => $expectedValue ) {
if ( $expectedValue instanceof ActivityPubObject ) {
$this->assertTrue(
$event->getRequest()->attributes->has( $expectedKey ),
$expectedValue->equals(
$event->getRequest()->attributes->get( $expectedKey )
),
"Error on test $testCase[id]"
);
} else {
$this->assertEquals(
$expectedValue,
$event->getRequest()->attributes->get( $expectedKey ),
"Error on test $testCase[id]"
);
if ( $expectedValue instanceof ActivityPubObject ) {
$this->assertTrue(
$expectedValue->equals(
$event->getRequest()->attributes->get( $expectedKey )
),
"Error on test $testCase[id]"
);
} else {
$this->assertEquals(
$expectedValue,
$event->getRequest()->attributes->get( $expectedKey ),
"Error on test $testCase[id]"
);
}
}
}
}

View File

@ -19,32 +19,32 @@ class AuthServiceTest extends APTestCase
$this->authService = new AuthService();
}
public function testAuthService()
public function provideTestAuthService()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'addressedTo',
'actor' => 'https://example.com/actor/1',
'object' => array(
'to' => 'https://example.com/actor/1',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'noAuth',
'object' => array(
'to' => 'https://example.com/actor/1',
),
'expectedResult' => false,
),
array(
) ),
array( array(
'id' => 'noAudience',
'object' => array(
'type' => 'Note'
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'actor',
'object' => array(
'actor' => 'https://example.com/actor/1',
@ -52,8 +52,8 @@ class AuthServiceTest extends APTestCase
),
'actor' => 'https://example.com/actor/1',
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'attributedTo',
'object' => array(
'attributedTo' => 'https://example.com/actor/1',
@ -61,19 +61,24 @@ class AuthServiceTest extends APTestCase
),
'actor' => 'https://example.com/actor/1',
'expectedResult' => true,
),
) ),
);
foreach ( $testCases as $testCase ) {
$request = Request::create( 'https://example.com/objects/1' );
if ( array_key_exists( 'actor', $testCase ) ) {
$request->attributes->set( 'actor', $testCase['actor'] );
}
$object = TestActivityPubObject::fromArray( $testCase['object'] );
$actual = $this->authService->isAuthorized( $request, $object );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
/**
* @dataProvider provideTestAuthService
*/
public function testAuthService( $testCase )
{
$request = Request::create( 'https://example.com/objects/1' );
if ( array_key_exists( 'actor', $testCase ) ) {
$request->attributes->set( 'actor', $testCase['actor'] );
}
$object = TestActivityPubObject::fromArray( $testCase['object'] );
$actual = $this->authService->isAuthorized( $request, $object );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
}

View File

@ -62,10 +62,10 @@ oYi+1hqp1fIekaxsyQIDAQAB
return array( 'id' => self::ACTOR_ID );
}
public function testSignatureListener()
public function provideTestSignatureListener()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'headers' => array(
'Authorization' => 'Signature keyId="https://example.com/actor/1/key",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
@ -76,8 +76,8 @@ oYi+1hqp1fIekaxsyQIDAQAB
'id' => 'https://example.com/actor/1',
) ),
),
),
array(
) ),
array( array(
'id' => 'existingActorTest',
'headers' => array(
'Authorization' => 'Signature keyId="https://example.com/actor/1/key",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
@ -93,8 +93,8 @@ oYi+1hqp1fIekaxsyQIDAQAB
'id' => 'https://example.com/actor/2',
) ),
),
),
array(
) ),
array( array(
'id' => 'signatureHeaderTest',
'headers' => array(
'Signature' => 'keyId="https://example.com/actor/1/key",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
@ -105,31 +105,36 @@ oYi+1hqp1fIekaxsyQIDAQAB
'id' => 'https://example.com/actor/1',
) ),
),
),
array(
) ),
array( array(
'id' => 'noSignatureTest',
'expectedAttributes' => array(),
),
) ),
);
foreach ( $testCases as $testCase ) {
$event = $this->getEvent();
if ( array_key_exists( 'headers', $testCase ) ) {
foreach ( $testCase['headers'] as $header => $value ) {
$event->getRequest()->headers->set( $header, $value );
}
}
/**
* @dataProvider provideTestSignatureListener
*/
public function testSignatureListener( $testCase )
{
$event = $this->getEvent();
if ( array_key_exists( 'headers', $testCase ) ) {
foreach ( $testCase['headers'] as $header => $value ) {
$event->getRequest()->headers->set( $header, $value );
}
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
foreach ( $testCase['requestAttributes'] as $attribute => $value ) {
$event->getRequest()->attributes->set( $attribute, $value );
}
}
$this->signatureListener->validateHttpSignature( $event );
$this->assertEquals(
$testCase['expectedAttributes'],
$event->getRequest()->attributes->all(),
"Error on test $testCase[id]"
);
}
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
foreach ( $testCase['requestAttributes'] as $attribute => $value ) {
$event->getRequest()->attributes->set( $attribute, $value );
}
}
$this->signatureListener->validateHttpSignature( $event );
$this->assertEquals(
$testCase['expectedAttributes'],
$event->getRequest()->attributes->all(),
"Error on test $testCase[id]"
);
}
private function getEvent()

View File

@ -25,7 +25,152 @@ class PostControllerTest extends APTestCase
*/
private $refs;
public function testPostController()
public function provideTestPostController()
{
$this->objects = self::getObjects();
$this->refs = self::getRefs();
return array(
array( array(
'id' => 'basicInboxTest',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedEventName' => InboxActivityEvent::NAME,
'expectedEvent' => new InboxActivityEvent(
array(
'type' => 'Create',
'actor' => 'https://elsewhere.com/actor/1'
),
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
$this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
)
),
) ),
array( array(
'id' => 'basicOutboxTest',
'request' => $this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
)
),
'expectedEventName' => OutboxActivityEvent::NAME,
'expectedEvent' => new OutboxActivityEvent(
array( 'type' => 'Create' ),
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
$this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
)
)
),
) ),
array( array(
'id' => 'inboxRequestMustBeSigned',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => UnauthorizedHttpException::class,
) ),
array( array(
'id' => 'outboxRequestsMustBeAuthed',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array()
),
'expectedException' => UnauthorizedHttpException::class,
) ),
array( array(
'id' => '404sIfNotFound',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => NotFoundHttpException::class,
) ),
array( array(
'id' => 'BadRequestIfNoBody',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => BadRequestHttpException::class,
) ),
array( array(
'id' => 'BadRequestIfMalformedBody',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'this is not JSON',
array(
'signed' => 'true',
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => BadRequestHttpException::class,
) ),
);
}
/**
* @dataProvider provideTestPostController
*/
public function testPostController( $testCase )
{
$this->objects = self::getObjects();
$this->refs = self::getRefs();
@ -60,161 +205,23 @@ class PostControllerTest extends APTestCase
}
} )
);
$testCases = array(
array(
'id' => 'basicInboxTest',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedEventName' => InboxActivityEvent::NAME,
'expectedEvent' => new InboxActivityEvent(
array(
'type' => 'Create',
'actor' => 'https://elsewhere.com/actor/1'
),
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
$this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
)
),
),
array(
'id' => 'basicOutboxTest',
'request' => $this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
)
),
'expectedEventName' => OutboxActivityEvent::NAME,
'expectedEvent' => new OutboxActivityEvent(
array( 'type' => 'Create' ),
TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
$this->makeRequest(
'https://example.com/actor/1/outbox',
Request::METHOD_POST,
'{"type": "Create"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://example.com/actor/1']
),
)
)
),
),
array(
'id' => 'inboxRequestMustBeSigned',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
'id' => 'outboxRequestsMustBeAuthed',
'request' => $this->makeRequest(
'https://example.com/actor/1/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array()
),
'expectedException' => UnauthorizedHttpException::class,
),
array(
'id' => '404sIfNotFound',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'{"type": "Create", "actor": "https://elsewhere.com/actor/1"}',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => NotFoundHttpException::class,
),
array(
'id' => 'BadRequestIfNoBody',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'',
array(
'signed' => true,
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => BadRequestHttpException::class,
),
array(
'id' => 'BadRequestIfMalformedBody',
'request' => $this->makeRequest(
'https://example.com/actor/notreal/inbox',
Request::METHOD_POST,
'this is not JSON',
array(
'signed' => 'true',
'actor' => TestActivityPubObject::fromArray(
$this->objects['https://elsewhere.com/actor/1']
),
)
),
'expectedException' => BadRequestHttpException::class,
),
);
foreach ( $testCases as $testCase ) {
$eventDispatcher = $this->getMockBuilder( EventDispatcher::class )
->setMethods( array( 'dispatch' ) )
->getMock();
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$eventDispatcher->expects( $this->once() )
->method( 'dispatch' )
->with(
$this->equalTo( $testCase['expectedEventName'] ),
$this->equalTo( $testCase['expectedEvent'] )
);
}
$postController = new PostController( $eventDispatcher, $objectsService );
$request = $testCase['request'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$postController->handle( $request );
$eventDispatcher = $this->getMockBuilder( EventDispatcher::class )
->setMethods( array( 'dispatch' ) )
->getMock();
if ( array_key_exists( 'expectedEvent', $testCase ) ) {
$eventDispatcher->expects( $this->once() )
->method( 'dispatch' )
->with(
$this->equalTo( $testCase['expectedEventName'] ),
$this->equalTo( $testCase['expectedEvent'] )
);
}
$postController = new PostController( $eventDispatcher, $objectsService );
$request = $testCase['request'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$postController->handle( $request );
}
private static function getObjects()

View File

@ -49,78 +49,78 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
$this->httpSignatureService = new HttpSignatureService( $dateTimeProvider );
}
public function testItVerifies()
public function provideTestItVerifies()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'defaultTest',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",signature="SjWJWbWN7i0wzBvtPl8rbASWz5xQW6mcJmn+ibttBqtifLN7Sazz6m79cNfwwb8DMJ5cou1s7uEGKKCs+FLEEaDV5lp7q25WqS+lavg7T8hc0GppauB6hbgEKTwblDHYGEtbGmtdHgVCk9SuS13F0hZ8FD0k/5OxEPXe5WozsbM="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'basicTest',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'allHeadersTest',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'defaultTestSigHeader',
'headers' => array(
'Signature' => 'keyId="Test",algorithm="rsa-sha256",signature="SjWJWbWN7i0wzBvtPl8rbASWz5xQW6mcJmn+ibttBqtifLN7Sazz6m79cNfwwb8DMJ5cou1s7uEGKKCs+FLEEaDV5lp7q25WqS+lavg7T8hc0GppauB6hbgEKTwblDHYGEtbGmtdHgVCk9SuS13F0hZ8FD0k/5OxEPXe5WozsbM="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'basicTestSigHeader',
'headers' => array(
'Signature' => 'keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'allHeadersTestSigHeader',
'headers' => array(
'Signature' => 'keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE="',
),
'expectedResult' => true,
),
array(
) ),
array( array(
'id' => 'noHeaders',
'headers' => array(),
'expectedResult' => false,
),
array(
) ),
array( array(
'id' => 'headerMissing',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length x-foo-header",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE="',
),
'expectedResult' => false,
),
array(
) ),
array( array(
'id' => 'malformedHeader',
'headers' => array(
'Authorization' => 'not a real auth header',
),
'expectedResult' => false,
),
array(
) ),
array( array(
'id' => 'partlyMalformedHeader',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers-malformed="(request-target) host date content-type digest content-length",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE="',
),
'expectedResult' => false,
),
array(
) ),
array( array(
'id' => 'dateTooFarInPast',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
@ -129,8 +129,8 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
'currentDatetime' => DateTime::createFromFormat(
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:36:41 GMT'
),
),
array(
) ),
array( array(
'id' => 'dateTooFarInFuture',
'headers' => array(
'Authorization' => 'Signature keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date", signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
@ -139,24 +139,29 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
'currentDatetime' => DateTime::createFromFormat(
DateTime::RFC2822, 'Sun, 05 Jan 2014 21:26:39 GMT'
),
),
) ),
);
foreach ( $testCases as $testCase ) {
if ( array_key_exists( 'currentDatetime', $testCase ) ) {
$dateTimeProvider = new TestDateTimeProvider( array(
'http-signature.verify' => $testCase['currentDatetime'],
) );
$this->httpSignatureService = new HttpSignatureService( $dateTimeProvider );
}
$request = self::getSymfonyRequest();
foreach ( $testCase['headers'] as $header => $value ) {
$request->headers->set( $header, $value );
}
$actual = $this->httpSignatureService->verify( $request, self::PUBLIC_KEY );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
/**
* @dataProvider provideTestItVerifies
*/
public function testItVerifies( $testCase )
{
if ( array_key_exists( 'currentDatetime', $testCase ) ) {
$dateTimeProvider = new TestDateTimeProvider( array(
'http-signature.verify' => $testCase['currentDatetime'],
) );
$this->httpSignatureService = new HttpSignatureService( $dateTimeProvider );
}
$request = self::getSymfonyRequest();
foreach ( $testCase['headers'] as $header => $value ) {
$request->headers->set( $header, $value );
}
$actual = $this->httpSignatureService->verify( $request, self::PUBLIC_KEY );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
private static function getSymfonyRequest()
@ -180,15 +185,15 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
return $request;
}
public function testItSigns()
public function provideTestItSigns()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'keyId' => 'Test',
'expected' => 'keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date",signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0="',
),
array(
) ),
array( array(
'id' => 'allHeadersTest',
'keyId' => 'Test',
'headers' => array(
@ -200,23 +205,28 @@ G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI
'content-length',
),
'expected' => 'keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE="',
),
) ),
);
foreach ( $testCases as $testCase ) {
$request = self::getPsrRequest();
if ( array_key_exists( 'headers', $testCase ) ) {
$actual = $this->httpSignatureService->sign(
$request, self::PRIVATE_KEY, $testCase['keyId'], $testCase['headers']
);
} else {
$actual = $this->httpSignatureService->sign(
$request, self::PRIVATE_KEY, $testCase['keyId']
);
}
$this->assertEquals(
$testCase['expected'], $actual, "Error on test $testCase[id]"
}
/**
* @dataProvider provideTestItSigns
*/
public function testItSigns( $testCase )
{
$request = self::getPsrRequest();
if ( array_key_exists( 'headers', $testCase ) ) {
$actual = $this->httpSignatureService->sign(
$request, self::PRIVATE_KEY, $testCase['keyId'], $testCase['headers']
);
} else {
$actual = $this->httpSignatureService->sign(
$request, self::PRIVATE_KEY, $testCase['keyId']
);
}
$this->assertEquals(
$testCase['expected'], $actual, "Error on test $testCase[id]"
);
}
private static function getPsrRequest()

View File

@ -31,40 +31,47 @@ class RouterTest extends APTestCase
$this->kernel = $this->getMock( HttpKernel::class );
}
public function testRouter()
public function provideTestRouter()
{
$testCases = array(
array(
$this->getController = $this->getMock( GetController::class );
$this->postController = $this->getMock( PostController::class );
return array(
array( array(
'id' => 'GET',
'request' => Request::create( 'https://foo.com', Request::METHOD_GET ),
'expectedController' => array( $this->getController, 'handle' ),
),
array(
) ),
array( array(
'id' => 'POST',
'request' => Request::create( 'https://foo.com', Request::METHOD_POST ),
'expectedController' => array( $this->postController, 'handle' ),
),
array(
) ),
array( array(
'id' => 'MethodNotAllowed',
'request' => Request::create( 'https://foo.com', Request::METHOD_PUT ),
'expectedException' => MethodNotAllowedHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$request = $testCase['request'];
$event = new GetResponseEvent(
$this->kernel, $request, HttpKernelInterface::MASTER_REQUEST
);
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$this->router->route( $event );
$this->assertEquals(
$testCase['expectedController'],
$request->attributes->get( '_controller' ),
"Error on test $testCase[id]"
);
}
/**
* @dataProvider provideTestRouter
*/
public function testRouter( $testCase )
{
$request = $testCase['request'];
$event = new GetResponseEvent(
$this->kernel, $request, HttpKernelInterface::MASTER_REQUEST
);
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$this->router->route( $event );
$this->assertEquals(
$testCase['expectedController'],
$request->attributes->get( '_controller' ),
"Error on test $testCase[id]"
);
}
}

View File

@ -50,10 +50,10 @@ class BlockServiceTest extends SQLiteTestCase
$this->blockService = new BlockService( $this->objectsService );
}
public function testGetBlockedActorIds()
public function provideTestGetBlockedActorIds()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'blocksExist',
'initialData' => array(
array(
@ -69,13 +69,13 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array( 'https://elsewhere.com/actors/1' ),
),
array(
) ),
array( array(
'id' => 'noBlocksExist',
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array(),
),
array(
) ),
array( array(
'id' => 'multipleBlocksExist',
'initialData' => array(
array(
@ -101,8 +101,8 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array( 'https://elsewhere.com/actors/1', 'https://elsewhere.com/actors/2' ),
),
array(
) ),
array( array(
'id' => 'blocksExistsFromDifferentActor',
'initialData' => array(
array(
@ -128,8 +128,8 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/2',
'expectedBlockedActorIds' => array(),
),
array(
) ),
array( array(
'id' => 'differentTypesOfActivitiesExistWithSameObjectAndActor',
'initialData' => array(
array(
@ -155,8 +155,8 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array( 'https://elsewhere.com/actors/2' ),
),
array(
) ),
array( array(
'id' => 'undoneBlocks',
'initialData' => array(
array(
@ -199,8 +199,8 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array( 'https://elsewhere.com/actors/2' ),
),
array(
) ),
array( array(
'id' => 'irrelevantUndonBlocks',
'initialData' => array(
array(
@ -243,21 +243,24 @@ class BlockServiceTest extends SQLiteTestCase
),
'blockingActorId' => 'https://example.com/actors/1',
'expectedBlockedActorIds' => array( 'https://elsewhere.com/actors/1', 'https://elsewhere.com/actors/2' ),
)
) )
);
foreach ( $testCases as $testCase ) {
self::setUp();
if ( array_key_exists( 'initialData', $testCase ) ) {
foreach ( $testCase['initialData'] as $object ) {
$this->objectsService->persist( $object );
}
}
/**
* @dataProvider provideTestGetBlockedActorIds
*/
public function testGetBlockedActorIds( $testCase )
{
if ( array_key_exists( 'initialData', $testCase ) ) {
foreach ( $testCase['initialData'] as $object ) {
$this->objectsService->persist( $object );
}
$blockedActorIds = $this->blockService->getBlockedActorIds( $testCase['blockingActorId'] );
$this->assertEquals(
$testCase['expectedBlockedActorIds'], $blockedActorIds, "Error on test $testCase[id]"
);
self::tearDown();
}
$blockedActorIds = $this->blockService->getBlockedActorIds( $testCase['blockingActorId'] );
$this->assertEquals(
$testCase['expectedBlockedActorIds'], $blockedActorIds, "Error on test $testCase[id]"
);
}
/**

View File

@ -58,13 +58,13 @@ class CollectionsServiceDbTest extends SQLiteTestCase
'driver' => 'pdo_sqlite',
'path' => $this->getDbPath(),
);
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
$this->dateTimeProvider = new TestDateTimeProvider( array(
'objects-service.create' => new DateTime( "12:00" ),
'objects-service.update' => new DateTime( "12:01" ),
'collections-service.add' => new DateTime( "12:03" ),
'collections-service.remove' => new DateTime( "12:04" ),
) );
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
$this->httpClient = $this->getMock( Client::class );
$this->httpClient->method( 'send' )
->willReturn( new Response( 404 ) );
@ -77,15 +77,23 @@ class CollectionsServiceDbTest extends SQLiteTestCase
private function getTime( $context )
{
if ( ! $this->dateTimeProvider ) {
$this->dateTimeProvider = new TestDateTimeProvider( array(
'objects-service.create' => new DateTime( "12:00" ),
'objects-service.update' => new DateTime( "12:01" ),
'collections-service.add' => new DateTime( "12:03" ),
'collections-service.remove' => new DateTime( "12:04" ),
) );
}
return $this->dateTimeProvider
->getTime( $context )
->format( "Y-m-d H:i:s" );
}
public function testAddItem()
public function provideTestAddItem()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'collection' => array(
'id' => 'https://example.com/collections/1',
@ -180,8 +188,8 @@ class CollectionsServiceDbTest extends SQLiteTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'createItemsField',
'collection' => array(
'id' => 'https://example.com/collections/1',
@ -275,8 +283,8 @@ class CollectionsServiceDbTest extends SQLiteTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'existingItems',
'collection' => array(
'id' => 'https://example.com/collections/1',
@ -398,32 +406,34 @@ class CollectionsServiceDbTest extends SQLiteTestCase
),
),
),
),
) ),
);
foreach ( $testCases as $testCase )
{
$this->setUp();
$collection = $this->objectsService->persist( $testCase['collection'] );
$this->collectionsService->addItem( $collection, $testCase['item'] );
$expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] );
$expectedObjects = $expectedDataSet->getTable( 'objects' );
$expectedFields = $expectedDataSet->getTable( 'fields' );
$actualObjects = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$actualFields = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]");
$this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]");
$this->tearDown();
}
}
public function testRemoveItem()
/**
* @dataProvider provideTestAddItem
*/
public function testAddItem( $testCase )
{
$testCases = array(
array(
$collection = $this->objectsService->persist( $testCase['collection'] );
$this->collectionsService->addItem( $collection, $testCase['item'] );
$expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] );
$expectedObjects = $expectedDataSet->getTable( 'objects' );
$expectedFields = $expectedDataSet->getTable( 'fields' );
$actualObjects = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$actualFields = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]");
$this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]");
}
public function provideTestRemoveItem()
{
return array(
array( array(
'id' => 'basicRemoveTest',
'collection' => array(
'id' => 'https://example.com/collections/1',
@ -538,26 +548,29 @@ class CollectionsServiceDbTest extends SQLiteTestCase
),
),
),
),
) ),
);
foreach ( $testCases as $testCase )
{
$this->setUp();
$collection = $this->objectsService->persist( $testCase['collection'] );
$this->collectionsService->removeItem( $collection, $testCase['itemIdToRemove'] );
$expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] );
$expectedObjects = $expectedDataSet->getTable( 'objects' );
$expectedFields = $expectedDataSet->getTable( 'fields' );
$actualObjects = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$actualFields = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]");
$this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]");
$this->tearDown();
}
}
/**
* @dataProvider provideTestRemoveItem
*/
public function testRemoveItem( $testCase )
{
$collection = $this->objectsService->persist( $testCase['collection'] );
$this->collectionsService->removeItem( $collection, $testCase['itemIdToRemove'] );
$expectedDataSet = new ArrayDataSet( $testCase['expectedDataSet'] );
$expectedObjects = $expectedDataSet->getTable( 'objects' );
$expectedFields = $expectedDataSet->getTable( 'fields' );
$actualObjects = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$actualFields = $this->getConnection()->createQueryTable(
'fields', 'SELECT * FROM fields'
);
$this->assertTablesEqual( $expectedObjects, $actualObjects, "Error on test $testCase[id]");
$this->assertTablesEqual( $expectedFields, $actualFields, "Error on test $testCase[id]");
$this->tearDown();
}
/**

View File

@ -55,10 +55,10 @@ class CollectionsServiceTest extends APTestCase
);
}
public function testCollectionPaging()
public function provideTestCollectionPaging()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'lessThanOnePage',
'collection' => array(
'@context' => array(
@ -112,8 +112,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'moreThanOnePage',
'collection' => array(
'@context' => array(
@ -177,8 +177,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'notFirstPage',
'collection' => array(
'@context' => array(
@ -227,8 +227,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'authFilteringPublic',
'collection' => array(
'@context' => array(
@ -292,8 +292,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'authFilteringSpecificActor',
'collection' => array(
'@context' => array(
@ -364,8 +364,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'sortAsc',
'collection' => array(
'@context' => array(
@ -429,8 +429,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'authFilteringSpecificActorSortAsc',
'collection' => array(
'@context' => array(
@ -501,8 +501,8 @@ class CollectionsServiceTest extends APTestCase
),
),
),
),
array(
) ),
array( array(
'id' => 'nonExistentPage',
'collection' => array(
'@context' => array(
@ -528,58 +528,63 @@ class CollectionsServiceTest extends APTestCase
Request::METHOD_GET
),
'expectedException' => NotFoundHttpException::class,
),
) ),
);
foreach ( $testCases as $testCase ) {
$this->authService = new AuthService();
$contextProvider = new ContextProvider();
$httpClient = $this->getMock( Client::class );
$httpClient->method( 'send' )->willReturn(
new Psr7Response( 200, array(), json_encode( array(
'type' => 'OrderedCollectionPage',
'orderedItems' => array(
'item3',
'item4',
),
) ) )
);
$entityManager = $this->getMock( EntityManager::class );
$collection = $testCase['collection'];
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'update' )->willReturn( TestActivityPubObject::fromArray( $collection ) );
$this->collectionsService = new CollectionsService(
4,
$this->authService,
$contextProvider,
$httpClient,
new SimpleDateTimeProvider(),
$entityManager,
$objectsService
);
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$request = $testCase['request'];
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
$request->attributes->add( $testCase['requestAttributes'] );
}
$actual = $this->collectionsService->pageAndFilterCollection(
$testCase['request'],
TestActivityPubObject::fromArray( $testCase['collection'] ),
function( ActivityPubObject $item ) use ( $testCase ) {
return $this->authService->isAuthorized( $testCase['request'], $item );
}
);
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
}
public function testCollectionNormalizing()
/**
* @dataProvider provideTestCollectionPaging
*/
public function testCollectionPaging( $testCase )
{
$testCases = array(
array(
$this->authService = new AuthService();
$contextProvider = new ContextProvider();
$httpClient = $this->getMock( Client::class );
$httpClient->method( 'send' )->willReturn(
new Psr7Response( 200, array(), json_encode( array(
'type' => 'OrderedCollectionPage',
'orderedItems' => array(
'item3',
'item4',
),
) ) )
);
$entityManager = $this->getMock( EntityManager::class );
$collection = $testCase['collection'];
$objectsService = $this->getMock( ObjectsService::class );
$objectsService->method( 'update' )->willReturn( TestActivityPubObject::fromArray( $collection ) );
$this->collectionsService = new CollectionsService(
4,
$this->authService,
$contextProvider,
$httpClient,
new SimpleDateTimeProvider(),
$entityManager,
$objectsService
);
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$request = $testCase['request'];
if ( array_key_exists( 'requestAttributes', $testCase ) ) {
$request->attributes->add( $testCase['requestAttributes'] );
}
$actual = $this->collectionsService->pageAndFilterCollection(
$testCase['request'],
TestActivityPubObject::fromArray( $testCase['collection'] ),
function( ActivityPubObject $item ) use ( $testCase ) {
return $this->authService->isAuthorized( $testCase['request'], $item );
}
);
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
public function provideTestCollectionNormalizing()
{
return array(
array( array(
'id' => 'basicNormalizingTest',
'collection' => array(
'type' => 'Collection',
@ -598,8 +603,8 @@ class CollectionsServiceTest extends APTestCase
'item2',
),
),
),
array(
) ),
array( array(
'id' => 'orderedNormalizingTest',
'collection' => array(
'type' => 'OrderedCollection',
@ -618,8 +623,8 @@ class CollectionsServiceTest extends APTestCase
'item2',
),
),
),
array(
) ),
array( array(
'id' => 'pageTraversal',
'collection' => array(
'type' => 'OrderedCollection',
@ -647,8 +652,8 @@ class CollectionsServiceTest extends APTestCase
'item4',
),
),
),
array(
) ),
array( array(
'id' => 'pageTraversal',
'collection' => array(
'type' => 'OrderedCollection',
@ -670,18 +675,23 @@ class CollectionsServiceTest extends APTestCase
'item4',
),
),
),
) ),
);
foreach ( $testCases as $testCase ) {
$collection = $testCase['collection'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$actual = $this->collectionsService->normalizeCollection( $collection );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
/**
* @dataProvider provideTestCollectionNormalizing
*/
public function testCollectionNormalizing( $testCase )
{
$collection = $testCase['collection'];
if ( array_key_exists( 'expectedException', $testCase ) ) {
$this->setExpectedException( $testCase['expectedException'] );
}
$actual = $this->collectionsService->normalizeCollection( $collection );
$this->assertEquals(
$testCase['expectedResult'], $actual, "Error on test $testCase[id]"
);
}
}

View File

@ -30,41 +30,46 @@ class IdProviderTest extends APTestCase
} ) );
}
public function testIdProvider()
public function provideTestIdProvider()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'providesId',
'providedRnd' => array( 'foo' ),
'expectedId' => 'https://example.com/ap/objects/foo',
),
array(
) ),
array( array(
'id' => 'checksForExisting',
'providedRnd' => array( self::EXISTING_ID_STR, 'bar' ),
'expectedId' => 'https://example.com/ap/objects/bar',
),
array(
) ),
array( array(
'id' => 'addsPath',
'providedRnd' => array( 'foo' ),
'path' => 'notes',
'expectedId' => 'https://example.com/ap/notes/foo',
),
) ),
);
foreach ( $testCases as $testCase ) {
$randomProvider = $this->getMock( RandomProvider::class );
call_user_func_array(
array( $randomProvider->method( 'randomString' ), 'willReturnOnConsecutiveCalls' ),
$testCase['providedRnd']
);
$idProvider = new IdProvider( $this->objectsService, $randomProvider, 'ap' );
$request = Request::create( 'https://example.com' );
if ( array_key_exists( 'path', $testCase ) ) {
$id = $idProvider->getId( $request, $testCase['path'] );
} else {
$id = $idProvider->getId( $request );
}
$this->assertEquals( $testCase['expectedId'], $id, "Error on test $testCase[id]" );
}
/**
* @dataProvider provideTestIdProvider
*/
public function testIdProvider( $testCase )
{
$randomProvider = $this->getMock( RandomProvider::class );
call_user_func_array(
array( $randomProvider->method( 'randomString' ), 'willReturnOnConsecutiveCalls' ),
$testCase['providedRnd']
);
$idProvider = new IdProvider( $this->objectsService, $randomProvider, 'ap' );
$request = Request::create( 'https://example.com' );
if ( array_key_exists( 'path', $testCase ) ) {
$id = $idProvider->getId( $request, $testCase['path'] );
} else {
$id = $idProvider->getId( $request );
}
$this->assertEquals( $testCase['expectedId'], $id, "Error on test $testCase[id]" );
}
}

View File

@ -1176,10 +1176,10 @@ class ObjectsServiceTest extends SQLiteTestCase
$this->assertEquals( $expected, $object->asArray( 3 ) );
}
public function testReplace()
public function provideTestReplace()
{
$testCases = array(
array(
return array(
array( array(
'id' => 'basicTest',
'object' => array(
'id' => 'https://example.com/objects/1',
@ -1197,8 +1197,8 @@ class ObjectsServiceTest extends SQLiteTestCase
'type' => 'Note',
'contents' => 'This is a note',
),
),
array(
) ),
array( array(
'id' => 'itDeletesOrphanedNodes',
'object' => array(
'id' => 'https://example.com/objects/2',
@ -1233,30 +1233,34 @@ class ObjectsServiceTest extends SQLiteTestCase
'expectedResult' => array(),
),
),
),
) ),
);
foreach ( $testCases as $testCase ) {
$this->setUp();
$this->objectsService->persist( $testCase['object'] );
$replacement = $this->objectsService->replace(
$testCase['replacementId'],
$testCase['replacement']
);
$this->assertEquals(
$testCase['expectedObject'],
$replacement->asArray(),
"Error on test $testCase[id]"
);
if ( array_key_exists( 'expectedQueryResults', $testCase ) ) {
foreach ( $testCase['expectedQueryResults'] as $expectedQueryResult ) {
$result = array_map(
function ( ActivityPubObject $obj ) {
return $obj->asArray();
},
$this->objectsService->query( $expectedQueryResult['query'] )
);
$this->assertEquals( $expectedQueryResult['expectedResult'], $result );
}
}
/**
* @dataProvider provideTestReplace
*/
public function testReplace( $testCase )
{
$this->objectsService->persist( $testCase['object'] );
$replacement = $this->objectsService->replace(
$testCase['replacementId'],
$testCase['replacement']
);
$this->assertEquals(
$testCase['expectedObject'],
$replacement->asArray(),
"Error on test $testCase[id]"
);
if ( array_key_exists( 'expectedQueryResults', $testCase ) ) {
foreach ( $testCase['expectedQueryResults'] as $expectedQueryResult ) {
$result = array_map(
function ( ActivityPubObject $obj ) {
return $obj->asArray();
},
$this->objectsService->query( $expectedQueryResult['query'] )
);
$this->assertEquals( $expectedQueryResult['expectedResult'], $result );
}
}
}