activitypub-php/test/Http/RouterTest.php

78 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2019-02-16 17:51:24 +00:00
namespace ActivityPub\Test\Http;
2019-01-23 14:43:23 +00:00
use ActivityPub\Controllers\GetController;
use ActivityPub\Controllers\PostController;
use ActivityPub\Http\Router;
use ActivityPub\Test\TestConfig\APTestCase;
2019-01-23 14:43:23 +00:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\HttpKernel;
2019-01-23 14:43:23 +00:00
use Symfony\Component\HttpKernel\HttpKernelInterface;
class RouterTest extends APTestCase
{
2019-01-23 14:43:23 +00:00
/**
* @var Router
*/
private $router;
private $getController;
private $postController;
private $kernel;
public function setUp()
{
$this->getController = $this->getMock( GetController::class );
$this->postController = $this->getMock( PostController::class );
2019-01-23 14:43:23 +00:00
$this->router = new Router( $this->getController, $this->postController );
$this->kernel = $this->getMock( HttpKernel::class );
2019-01-23 14:43:23 +00:00
}
2019-02-16 17:51:24 +00:00
2019-04-12 03:41:25 +00:00
public function provideTestRouter()
{
2019-04-12 03:41:25 +00:00
$this->getController = $this->getMock( GetController::class );
$this->postController = $this->getMock( PostController::class );
return array(
array( array(
2019-01-23 14:43:23 +00:00
'id' => 'GET',
'request' => Request::create( 'https://foo.com', Request::METHOD_GET ),
'expectedController' => array( $this->getController, 'handle' ),
2019-04-12 03:41:25 +00:00
) ),
array( array(
2019-01-23 14:43:23 +00:00
'id' => 'POST',
'request' => Request::create( 'https://foo.com', Request::METHOD_POST ),
'expectedController' => array( $this->postController, 'handle' ),
2019-04-12 03:41:25 +00:00
) ),
array( array(
2019-01-23 14:43:23 +00:00
'id' => 'MethodNotAllowed',
'request' => Request::create( 'https://foo.com', Request::METHOD_PUT ),
'expectedException' => MethodNotAllowedHttpException::class,
2019-04-12 03:41:25 +00:00
) ),
2019-01-23 14:43:23 +00:00
);
2019-04-12 03:41:25 +00:00
}
/**
* @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'] );
2019-01-23 14:43:23 +00:00
}
2019-04-12 03:41:25 +00:00
$this->router->route( $event );
$this->assertEquals(
$testCase['expectedController'],
$request->attributes->get( '_controller' ),
"Error on test $testCase[id]"
);
}
}