Repository navigation
Expand file tree
/
Copy pathClientTest.php
More file actions
98 lines (83 loc) · 3.44 KB
/
Copy pathClientTest.php
File metadata and controls
98 lines (83 loc) · 3.44 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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Testing;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Di\Container;
use Hyperf\Di\MethodDefinitionCollector;
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
use Hyperf\HttpServer\CoreMiddleware;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\Router;
use Hyperf\Testing\Client;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Utils\Serializer\SimpleNormalizer;
use HyperfTest\Testing\Stub\Exception\Handler\FooExceptionHandler;
use HyperfTest\Testing\Stub\FooController;
use Mockery;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ClientTest extends TestCase
{
public function testClientRequest()
{
$container = $this->getContainer();
$client = new Client($container);
$data = $client->get('/');
$this->assertSame(0, $data['code']);
$this->assertSame('Hello Hyperf!', $data['data']);
}
public function testClientException()
{
$container = $this->getContainer();
$client = new Client($container);
$data = $client->get('/exception');
$this->assertSame(500, $data['code']);
$this->assertSame('Server Error', $data['message']);
}
public function getContainer()
{
$container = Mockery::mock(Container::class);
$container->shouldReceive('get')->with(HttpDispatcher::class)->andReturn(new HttpDispatcher($container));
$container->shouldReceive('get')->with(ExceptionHandlerDispatcher::class)->andReturn(new ExceptionHandlerDispatcher($container));
$container->shouldReceive('get')->with(DispatcherFactory::class)->andReturn($factory = new DispatcherFactory());
$container->shouldReceive('get')->with(NormalizerInterface::class)->andReturn(new SimpleNormalizer());
$container->shouldReceive('get')->with(MethodDefinitionCollectorInterface::class)->andReturn(new MethodDefinitionCollector());
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
'exceptions' => [
'handler' => [
'http' => [
FooExceptionHandler::class,
],
],
],
]));
$container->shouldReceive('get')->with(Filesystem::class)->andReturn(new Filesystem());
$container->shouldReceive('get')->with(FooController::class)->andReturn(new FooController());
$container->shouldReceive('has')->andReturn(true);
$container->shouldReceive('get')->with(FooExceptionHandler::class)->andReturn(new FooExceptionHandler());
$container->shouldReceive('make')->with(CoreMiddleware::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new CoreMiddleware(...array_values($args));
});
ApplicationContext::setContainer($container);
Router::init($factory);
Router::get('/', [FooController::class, 'index']);
Router::get('/exception', [FooController::class, 'exception']);
return $container;
}
}