1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phprest; |
4
|
|
|
|
5
|
|
|
use League\Route\Http\Exception\NotFoundException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Phprest\Router\RouteCollection; |
10
|
|
|
use League\Container\ContainerInterface; |
11
|
|
|
use Phprest\Config; |
12
|
|
|
use Phprest\Stub\Controller\Simple; |
13
|
|
|
|
14
|
|
|
class ApplicationTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Config |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Application |
23
|
|
|
*/ |
24
|
|
|
protected $app; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->config = new Config('phprest-test', 1, true); |
29
|
|
|
$this->app = new Application($this->config); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testInstantiation(): void |
33
|
|
|
{ |
34
|
|
|
$this->assertTrue( |
35
|
|
|
$this->app->getContainer()->isSingleton(Service\Hateoas\Config::getServiceName()) |
36
|
|
|
); |
37
|
|
|
$this->assertTrue( |
38
|
|
|
$this->app->getContainer()->isSingleton(Service\Logger\Config::getServiceName()) |
39
|
|
|
); |
40
|
|
|
$this->assertEquals('phprest-test', $this->app->getContainer()->get(Application::CONTAINER_ID_VENDOR)); |
41
|
|
|
$this->assertEquals(1, $this->app->getContainer()->get(Application::CONTAINER_ID_API_VERSION)); |
42
|
|
|
$this->assertTrue($this->app->getContainer()->get(Application::CONTAINER_ID_DEBUG)); |
43
|
|
|
$this->assertInstanceOf( |
44
|
|
|
RouteCollection::class, |
45
|
|
|
$this->app->getContainer()->get(Application::CONTAINER_ID_ROUTER) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testRun(): void |
50
|
|
|
{ |
51
|
|
|
$request = Request::create('/welcome'); |
52
|
|
|
$request->headers->set('Accept', '*/*'); |
53
|
|
|
|
54
|
|
|
$this->app->get('/1.0/welcome', static function (Request $request) { |
55
|
|
|
return new Response('Hello Phprest World', 200); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
ob_start(); |
59
|
|
|
$this->app->run($request); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('Hello Phprest World', ob_get_clean()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @expectedException \League\Route\Http\Exception\NotFoundException |
66
|
|
|
*/ |
67
|
|
|
public function testRunNotFound(): void |
68
|
|
|
{ |
69
|
|
|
$this->app->run(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testRegisterController(): void |
73
|
|
|
{ |
74
|
|
|
$this->app->registerController(Simple::class); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf( |
77
|
|
|
Simple::class, |
78
|
|
|
$this->app->getContainer()->get(Simple::class) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testHead(): void |
83
|
|
|
{ |
84
|
|
|
$this->app->head('/test-head', 'test-handler'); |
85
|
|
|
|
86
|
|
|
$this->assertEquals( |
87
|
|
|
['method' => 'HEAD', 'route' => '/test-head', 'handler' => 'test-handler'], |
88
|
|
|
$this->app->getRouter()->getRoutingTable()[0] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testOptions(): void |
93
|
|
|
{ |
94
|
|
|
$this->app->options('/test-options', 'test-handler'); |
95
|
|
|
|
96
|
|
|
$this->assertEquals( |
97
|
|
|
['method' => 'OPTIONS', 'route' => '/test-options', 'handler' => 'test-handler'], |
98
|
|
|
$this->app->getRouter()->getRoutingTable()[0] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testGetters(): void |
103
|
|
|
{ |
104
|
|
|
$this->assertInstanceOf(Config::class, $this->app->getConfiguration()); |
105
|
|
|
$this->assertInstanceOf(ContainerInterface::class, $this->app->getContainer()); |
106
|
|
|
$this->assertInstanceOf(RouteCollection::class, $this->app->getRouter()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|