1
|
|
|
<?php namespace Phprest\Util; |
2
|
|
|
|
3
|
|
|
use Phprest\Application; |
4
|
|
|
use League\Container\Container; |
5
|
|
|
use Phprest\Util\DataStructure\MimeProcessResult; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class MimeTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
use Mime; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Container |
14
|
|
|
*/ |
15
|
|
|
private $container; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->container = new Container(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testSimpleAcceptJsonMime(): void |
23
|
|
|
{ |
24
|
|
|
$this->setVendorInContainer('phprest-test'); |
25
|
|
|
$this->setApiVersionInContainer('1.5'); |
26
|
|
|
|
27
|
|
|
$result = $this->processMime('application/json'); |
28
|
|
|
|
29
|
|
|
$this->assertInstanceOf(MimeProcessResult::class, $result); |
30
|
|
|
|
31
|
|
|
$this->assertEquals('application/vnd.phprest-test-v1.5+json', $result->mime); |
32
|
|
|
$this->assertEquals('phprest-test', $result->vendor); |
33
|
|
|
$this->assertEquals('1.5', $result->apiVersion); |
34
|
|
|
$this->assertEquals('json', $result->format); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSimpleAcceptXmlMime(): void |
38
|
|
|
{ |
39
|
|
|
$this->setVendorInContainer('phprest-test'); |
40
|
|
|
$this->setApiVersionInContainer('1.5'); |
41
|
|
|
|
42
|
|
|
$result = $this->processMime('application/xml'); |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf(MimeProcessResult::class, $result); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('application/vnd.phprest-test-v1.5+xml', $result->mime); |
47
|
|
|
$this->assertEquals('xml', $result->format); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testComplexVersionFormatAcceptJsonMime(): void |
51
|
|
|
{ |
52
|
|
|
$this->setVendorInContainer('phprest-test'); |
53
|
|
|
$this->setApiVersionInContainer('2.7'); |
54
|
|
|
|
55
|
|
|
$result = $this->processMime('application/vnd.phprest-test-v2.7+json'); |
56
|
|
|
|
57
|
|
|
$this->assertInstanceOf(MimeProcessResult::class, $result); |
58
|
|
|
|
59
|
|
|
$this->assertEquals('application/vnd.phprest-test-v2.7+json', $result->mime); |
60
|
|
|
$this->assertEquals('2.7', $result->apiVersion); |
61
|
|
|
$this->assertEquals('json', $result->format); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testComplexVersionFormatAcceptXmlMime(): void |
65
|
|
|
{ |
66
|
|
|
$this->setVendorInContainer('phprest-test'); |
67
|
|
|
$this->setApiVersionInContainer(3); |
68
|
|
|
|
69
|
|
|
$result = $this->processMime('application/vnd.phprest-test+xml; version=3'); |
70
|
|
|
|
71
|
|
|
$this->assertInstanceOf(MimeProcessResult::class, $result); |
72
|
|
|
|
73
|
|
|
$this->assertEquals('application/vnd.phprest-test+xml; version=3', $result->mime); |
74
|
|
|
$this->assertEquals(3, $result->apiVersion); |
75
|
|
|
$this->assertEquals('xml', $result->format); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $vendor |
80
|
|
|
*/ |
81
|
|
|
protected function setVendorInContainer($vendor): void |
82
|
|
|
{ |
83
|
|
|
$this->container->add(Application::CONTAINER_ID_VENDOR, $vendor); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|integer $apiVersion |
88
|
|
|
*/ |
89
|
|
|
protected function setApiVersionInContainer($apiVersion): void |
90
|
|
|
{ |
91
|
|
|
$this->container->add(Application::CONTAINER_ID_API_VERSION, $apiVersion); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the DI container |
96
|
|
|
* |
97
|
|
|
* @return Container |
98
|
|
|
*/ |
99
|
|
|
protected function getContainer(): Container |
100
|
|
|
{ |
101
|
|
|
return $this->container; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|