mirror of
https://github.com/flarum/core.git
synced 2025-07-30 21:20:24 +02:00
chore: throw an exception when no serializer is provided to the controller (#3614)
* chore: throw an exception when no serializer is provided to the controller Signed-off-by: Sami Mazouz <ilyasmazouz@gmail.com> * test: no serializer set throws exception Signed-off-by: Sami Mazouz <sychocouldy@gmail.com> Signed-off-by: Sami Mazouz <ilyasmazouz@gmail.com> Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\api;
|
||||
|
||||
use Flarum\Api\Controller\AbstractSerializeController;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Testing\integration\TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
use Tobscure\JsonApi\SerializerInterface;
|
||||
|
||||
class AbstractSerializeControllerTest extends TestCase
|
||||
{
|
||||
public function test_missing_serializer_class_throws_exception()
|
||||
{
|
||||
$this->extend(
|
||||
(new Extend\Routes('api'))
|
||||
->get('/dummy-serialize', 'dummy-serialize', DummySerializeController::class)
|
||||
);
|
||||
|
||||
$response = $this->send(
|
||||
$this->request('GET', '/api/dummy-serialize')
|
||||
);
|
||||
|
||||
$json = json_decode((string) $response->getBody(), true);
|
||||
|
||||
$this->assertEquals(500, $response->getStatusCode());
|
||||
$this->assertStringStartsWith('InvalidArgumentException: Serializer required for controller: '.DummySerializeController::class, $json['errors'][0]['detail']);
|
||||
}
|
||||
}
|
||||
|
||||
class DummySerializeController extends AbstractSerializeController
|
||||
{
|
||||
public $serializer = null;
|
||||
|
||||
protected function data(ServerRequestInterface $request, Document $document)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function createElement($data, SerializerInterface $serializer)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user