mirror of
https://github.com/flarum/core.git
synced 2025-08-11 19:04:29 +02:00
Extract pure unit tests so that they can run fast
- Move to separate directory (base for a separate test suite) - Inherit directly from PhpUnit - Configure test suite with dedicated XML file
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\FloodingExceptionHandler;
|
||||
use Flarum\Post\Exception\FloodingException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FloodingExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new FloodingExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new FloodingException));
|
||||
}
|
||||
|
||||
public function test_it_provides_expected_output()
|
||||
{
|
||||
$result = $this->handler->handle(new FloodingException);
|
||||
|
||||
$this->assertEquals(429, $result->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '429',
|
||||
'code' => 'too_many_requests'
|
||||
]
|
||||
], $result->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\IlluminateValidationExceptionHandler;
|
||||
use Illuminate\Translation\ArrayLoader;
|
||||
use Illuminate\Translation\Translator;
|
||||
use Illuminate\Validation\Factory;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IlluminateValidationExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new IlluminateValidationExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_familiar_exceptions()
|
||||
{
|
||||
$validException = new ValidationException($this->makeValidator());
|
||||
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages($validException));
|
||||
}
|
||||
|
||||
public function test_it_creates_the_desired_output()
|
||||
{
|
||||
$exception = new ValidationException($this->makeValidator(['foo' => ''], ['foo' => 'required']));
|
||||
|
||||
$response = $this->handler->handle($exception);
|
||||
|
||||
$this->assertEquals(422, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'validation.required',
|
||||
'source' => ['pointer' => '/data/attributes/foo']
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
|
||||
private function makeValidator($data = [], $rules = [])
|
||||
{
|
||||
$translator = new Translator(new ArrayLoader(), 'en');
|
||||
$factory = new Factory($translator);
|
||||
|
||||
return $factory->make($data, $rules);
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\Exception\InvalidAccessTokenException;
|
||||
use Flarum\Api\ExceptionHandler\InvalidAccessTokenExceptionHandler;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class InvalidAccessTokenExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new InvalidAccessTokenExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new InvalidAccessTokenException));
|
||||
}
|
||||
|
||||
public function test_output()
|
||||
{
|
||||
$response = $this->handler->handle(new InvalidAccessTokenException);
|
||||
|
||||
$this->assertEquals(401, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '401',
|
||||
'code' => 'invalid_access_token'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\InvalidConfirmationTokenExceptionHandler;
|
||||
use Flarum\User\Exception\InvalidConfirmationTokenException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class InvalidConfirmationTokenExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new InvalidConfirmationTokenExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new InvalidConfirmationTokenException));
|
||||
}
|
||||
|
||||
public function test_output()
|
||||
{
|
||||
$response = $this->handler->handle(new InvalidConfirmationTokenException);
|
||||
|
||||
$this->assertEquals(403, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '403',
|
||||
'code' => 'invalid_confirmation_token'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\MethodNotAllowedExceptionHandler;
|
||||
use Flarum\Http\Exception\MethodNotAllowedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MethodNotAllowedExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new MethodNotAllowedExceptionHandler();
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new MethodNotAllowedException()));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new MethodNotAllowedException);
|
||||
|
||||
$this->assertEquals(405, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '405',
|
||||
'code' => 'method_not_allowed'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\ModelNotFoundExceptionHandler;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ModelNotFoundExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new ModelNotFoundExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new ModelNotFoundException));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new ModelNotFoundException);
|
||||
|
||||
$this->assertEquals(404, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '404',
|
||||
'code' => 'resource_not_found'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\PermissionDeniedExceptionHandler;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PermissionDeniedExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new PermissionDeniedExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new PermissionDeniedException));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new PermissionDeniedException);
|
||||
|
||||
$this->assertEquals(401, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '401',
|
||||
'code' => 'permission_denied'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\RouteNotFoundExceptionHandler;
|
||||
use Flarum\Http\Exception\RouteNotFoundException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RouteNotFoundExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new RouteNotFoundExceptionHandler();
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new RouteNotFoundException()));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new RouteNotFoundException);
|
||||
|
||||
$this->assertEquals(404, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '404',
|
||||
'code' => 'route_not_found'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\TokenMismatchExceptionHandler;
|
||||
use Flarum\Http\Exception\TokenMismatchException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class TokenMismatchExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new TokenMismatchExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new TokenMismatchException()));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new TokenMismatchException);
|
||||
|
||||
$this->assertEquals(400, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '400',
|
||||
'code' => 'csrf_token_mismatch'
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
<?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 Flarum\Tests\unit\Api\ExceptionHandler;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Api\ExceptionHandler\ValidationExceptionHandler;
|
||||
use Flarum\Foundation\ValidationException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ValidationExceptionHandlerTest extends TestCase
|
||||
{
|
||||
private $handler;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->handler = new ValidationExceptionHandler;
|
||||
}
|
||||
|
||||
public function test_it_handles_recognisable_exceptions()
|
||||
{
|
||||
$this->assertFalse($this->handler->manages(new Exception));
|
||||
$this->assertTrue($this->handler->manages(new ValidationException([])));
|
||||
}
|
||||
|
||||
public function test_managing_exceptions()
|
||||
{
|
||||
$response = $this->handler->handle(new ValidationException(
|
||||
['foo' => 'Attribute error'],
|
||||
['bar' => 'Relationship error']
|
||||
));
|
||||
|
||||
$this->assertEquals(422, $response->getStatus());
|
||||
$this->assertEquals([
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'Attribute error',
|
||||
'source' => ['pointer' => '/data/attributes/foo']
|
||||
],
|
||||
[
|
||||
'status' => '422',
|
||||
'code' => 'validation_error',
|
||||
'detail' => 'Relationship error',
|
||||
'source' => ['pointer' => '/data/relationships/bar']
|
||||
]
|
||||
], $response->getErrors());
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?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 Flarum\Tests\unit\Settings;
|
||||
|
||||
use Flarum\Settings\DatabaseSettingsRepository;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Mockery as m;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DatabaseSettingsRepositoryTest extends TestCase
|
||||
{
|
||||
private $connection;
|
||||
private $repository;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->repository = new DatabaseSettingsRepository($this->connection);
|
||||
}
|
||||
|
||||
public function test_requesting_an_existing_setting_should_return_its_value()
|
||||
{
|
||||
$this->connection->shouldReceive('table->where->value')->andReturn('value');
|
||||
|
||||
$this->assertEquals('value', $this->repository->get('key'));
|
||||
}
|
||||
|
||||
public function test_non_existent_setting_values_should_return_null()
|
||||
{
|
||||
$this->connection->shouldReceive('table->where->value')->andReturn(null);
|
||||
|
||||
$this->assertEquals('default', $this->repository->get('key', 'default'));
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
<?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 Flarum\Tests\unit\Settings;
|
||||
|
||||
use Flarum\Settings\MemoryCacheSettingsRepository;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Mockery as m;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MemoryCacheSettingsRepositoryTest extends TestCase
|
||||
{
|
||||
private $baseRepository;
|
||||
private $repository;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->baseRepository = m::mock(SettingsRepositoryInterface::class);
|
||||
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository);
|
||||
}
|
||||
|
||||
public function test_it_should_return_all_settings_when_not_cached()
|
||||
{
|
||||
$this->baseRepository->shouldReceive('all')->once()->andReturn(['key' => 'value']);
|
||||
|
||||
$this->assertEquals(['key' => 'value'], $this->repository->all());
|
||||
$this->assertEquals(['key' => 'value'], $this->repository->all()); // Assert twice to ensure we hit the cache
|
||||
}
|
||||
|
||||
public function test_it_should_retrieve_a_specific_value()
|
||||
{
|
||||
$this->baseRepository->shouldReceive('all')->once()->andReturn(['key1' => 'value1', 'key2' => 'value2']);
|
||||
|
||||
$this->assertEquals('value2', $this->repository->get('key2'));
|
||||
$this->assertEquals('value2', $this->repository->get('key2')); // Assert twice to ensure we hit the cache
|
||||
}
|
||||
|
||||
public function test_it_should_set_a_key_value_pair()
|
||||
{
|
||||
$this->baseRepository->shouldReceive('set')->once();
|
||||
|
||||
$this->repository->set('key', 'value');
|
||||
|
||||
$this->assertEquals('value', $this->repository->get('key'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user