Fix tests and CS

This commit is contained in:
Toby Zerner 2015-12-05 22:31:33 +10:30
parent 0cf000122f
commit d743e56bc1
10 changed files with 100 additions and 41 deletions

View File

View File

@ -33,7 +33,7 @@ class ModelNotFoundExceptionHandler implements ExceptionHandlerInterface
{
$status = 404;
$error = [
'status' => '404',
'status' => (string) $status,
'code' => 'resource_not_found'
];

View File

@ -30,7 +30,7 @@ class AuthenticateWithSession implements MiddlewareInterface
$request = $request->withAttribute('actor', $actor);
return $out ? $out($request, $response) : $response;;
return $out ? $out($request, $response) : $response;
}
private function getActor(SessionInterface $session)

View File

@ -1,28 +0,0 @@
<?php
namespace Tests\Flarum\Admin\Middleware;
use Flarum\Admin\Middleware\AuthenticateWithCookie;
use Flarum\Admin\Middleware\RequireAdministrateAbility;
use Flarum\Core\Access\Gate;
use Flarum\Core\Exception\PermissionDeniedException;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tests\Test\TestCase;
class RequireAdministrateAbilityTest extends TestCase
{
public function test_it_should_not_allow_invalid_logins()
{
$this->setExpectedException(PermissionDeniedException::class);
$gate = m::mock(Gate::class);
$request = m::mock(ServerRequestInterface::class)->shouldIgnoreMissing();
$response = m::mock(ResponseInterface::class);
$gate->shouldReceive('forUser->allows')->andReturn(false);
$middleware = new RequireAdministrateAbility($gate);
$middleware->__invoke($request, $response);
}
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\FloodingExceptionHandler;
use Flarum\Core\Exception\FloodingException;
use Tests\Test\TestCase;
@ -16,7 +26,7 @@ class FloodingExceptionHandlerTest extends TestCase
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new FloodingException));
}
@ -25,6 +35,11 @@ class FloodingExceptionHandlerTest extends TestCase
$result = $this->handler->handle(new FloodingException);
$this->assertEquals(429, $result->getStatus());
$this->assertEquals([[]], $result->getErrors());
$this->assertEquals([
[
'status' => '429',
'code' => 'too_many_requests'
]
], $result->getErrors());
}
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\ValidationExceptionHandler;
use Flarum\Core\Exception\ValidationException;
use Tests\Test\TestCase;
@ -18,7 +28,7 @@ class IlluminateValidationExceptionHandlerTest extends TestCase
{
$validException = new ValidationException(['messages']);
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages($validException));
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\InvalidConfirmationTokenExceptionHandler;
use Flarum\Core\Exception\InvalidConfirmationTokenException;
use Tests\Test\TestCase;
@ -16,7 +26,7 @@ class InvalidConfirmationTokenExceptionHandlerTest extends TestCase
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new InvalidConfirmationTokenException));
}
@ -25,6 +35,11 @@ class InvalidConfirmationTokenExceptionHandlerTest extends TestCase
$response = $this->handler->handle(new InvalidConfirmationTokenException);
$this->assertEquals(403, $response->getStatus());
$this->assertEquals([['code' => 'invalid_confirmation_token']], $response->getErrors());
$this->assertEquals([
[
'status' => '403',
'code' => 'invalid_confirmation_token'
]
], $response->getErrors());
}
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\ModelNotFoundExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Tests\Test\TestCase;
@ -16,7 +26,7 @@ class ModelNotFoundExceptionHandlerTest extends TestCase
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new ModelNotFoundException));
}
@ -25,6 +35,11 @@ class ModelNotFoundExceptionHandlerTest extends TestCase
$response = $this->handler->handle(new ModelNotFoundException);
$this->assertEquals(404, $response->getStatus());
$this->assertEquals([[]], $response->getErrors());
$this->assertEquals([
[
'status' => '404',
'code' => 'resource_not_found'
]
], $response->getErrors());
}
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\PermissionDeniedExceptionHandler;
use Flarum\Core\Exception\PermissionDeniedException;
use Tests\Test\TestCase;
@ -16,7 +26,7 @@ class PermissionDeniedExceptionHandlerTest extends TestCase
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new PermissionDeniedException));
}
@ -25,6 +35,11 @@ class PermissionDeniedExceptionHandlerTest extends TestCase
$response = $this->handler->handle(new PermissionDeniedException);
$this->assertEquals(401, $response->getStatus());
$this->assertEquals([[]], $response->getErrors());
$this->assertEquals([
[
'status' => '401',
'code' => 'permission_denied'
]
], $response->getErrors());
}
}

View File

@ -1,6 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Flarum\Api\Handler;
use Exception;
use Flarum\Api\Handler\ValidationExceptionHandler;
use Flarum\Core\Exception\ValidationException;
use Tests\Test\TestCase;
@ -16,7 +26,7 @@ class ValidationExceptionHandlerTest extends TestCase
public function test_it_handles_recognisable_exceptions()
{
$this->assertFalse($this->handler->manages(new \Exception));
$this->assertFalse($this->handler->manages(new Exception));
$this->assertTrue($this->handler->manages(new ValidationException([])));
}
@ -25,6 +35,13 @@ class ValidationExceptionHandlerTest extends TestCase
$response = $this->handler->handle(new ValidationException(['There was an error']));
$this->assertEquals(422, $response->getStatus());
$this->assertEquals([['source' => ['pointer' => '/data/attributes/0'], 'detail' => 'There was an error']], $response->getErrors());
$this->assertEquals([
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'There was an error',
'source' => ['pointer' => '/data/attributes/0']
]
], $response->getErrors());
}
}