1
|
|
|
<?php namespace Phprest\ErrorHandler\Formatter; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
use LogicException; |
5
|
|
|
use Phprest\Application; |
6
|
|
|
use Phprest\Config; |
7
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
8
|
|
|
use Phprest\Exception\BadRequest; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
class JsonXmlTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Config |
16
|
|
|
*/ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->config = new Config('phprest', 1, true); |
22
|
|
|
$this->setContainerElements($this->config); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testFormatWithSimpleException(): void |
26
|
|
|
{ |
27
|
|
|
$jsonXmlFormatter = new JsonXml($this->config); |
28
|
|
|
|
29
|
|
|
$this->assertContains( |
30
|
|
|
'"code":9,"message":"test","details":[]', |
31
|
|
|
$jsonXmlFormatter->format(new LogicException('test', 9)) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testFormatWithDetailedException(): void |
36
|
|
|
{ |
37
|
|
|
$jsonXmlFormatter = new JsonXml($this->config); |
38
|
|
|
|
39
|
|
|
$this->assertContains( |
40
|
|
|
'"code":11,"message":"Bad Request","details":[1,2,3,["a","b"]]', |
41
|
|
|
$jsonXmlFormatter->format(new BadRequest(11, [1,2,3,['a','b']])) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testFormatWithNotAcceptable(): void |
46
|
|
|
{ |
47
|
|
|
$request = Request::createFromGlobals(); |
48
|
|
|
$request->headers->set('Accept', 'yaml'); |
49
|
|
|
|
50
|
|
|
$jsonXmlFormatter = new JsonXml($this->config, $request); |
51
|
|
|
|
52
|
|
|
$this->assertContains( |
53
|
|
|
'"code":0,"message":"Not Acceptable","details":["yaml is not supported"]', |
54
|
|
|
$jsonXmlFormatter->format(new Exception()) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Config $config |
60
|
|
|
*/ |
61
|
|
|
protected function setContainerElements(Config $config): void |
62
|
|
|
{ |
63
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
64
|
|
|
|
65
|
|
|
$config->getHateoasService()->register( |
66
|
|
|
$config->getContainer(), |
67
|
|
|
$config->getHateoasConfig() |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$config->getContainer()->add(Application::CONTAINER_ID_VENDOR, $config->getVendor()); |
71
|
|
|
$config->getContainer()->add(Application::CONTAINER_ID_API_VERSION, $config->getApiVersion()); |
72
|
|
|
$config->getContainer()->add(Application::CONTAINER_ID_DEBUG, $config->isDebug()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|