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