|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Cache; |
|
6
|
|
|
use Basis\Test; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use GuzzleHttp\Handler\MockHandler; |
|
10
|
|
|
use GuzzleHttp\HandlerStack; |
|
11
|
|
|
use GuzzleHttp\Middleware; |
|
12
|
|
|
use GuzzleHttp\Psr7\Response; |
|
13
|
|
|
|
|
14
|
|
|
class DispatcherTest extends Test |
|
15
|
|
|
{ |
|
16
|
|
|
public $disableRemote = false; |
|
17
|
|
|
|
|
18
|
|
|
public function test() |
|
19
|
|
|
{ |
|
20
|
|
|
$container = []; |
|
21
|
|
|
$mock = new MockHandler(); |
|
22
|
|
|
|
|
23
|
|
|
$handler = HandlerStack::create($mock); |
|
24
|
|
|
$handler->push(Middleware::history($container)); |
|
25
|
|
|
|
|
26
|
|
|
$client = new Client(['handler' => $handler]); |
|
27
|
|
|
$this->app->share(Client::class, $client); |
|
28
|
|
|
|
|
29
|
|
|
$mock->append( |
|
30
|
|
|
new Response(200, [], json_encode([ |
|
31
|
|
|
'success' => true, |
|
32
|
|
|
'data' => ['mocked' => true] |
|
33
|
|
|
])) |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$result = $this->app->dispatch('service.hello'); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$mock->append( |
|
39
|
|
|
new Response(200, [], json_encode([ |
|
40
|
|
|
'success' => true, |
|
41
|
|
|
'data' => [ |
|
42
|
|
|
['message' => 'hello, nekufa'], |
|
43
|
|
|
['message' => 'hello, rybakit'], |
|
44
|
|
|
] |
|
45
|
|
|
])) |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$results = $this->app->dispatch('service.hello', [['name' => 'nekufa'], ['name' => 'rybakit']]); |
|
49
|
|
|
$this->assertCount(2, $results); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertCount(2, $container); |
|
|
|
|
|
|
52
|
|
|
$body = json_encode([ |
|
53
|
|
|
'job' => 'service.hello', |
|
54
|
|
|
'params' => [ |
|
55
|
|
|
['name' => 'nekufa'], |
|
56
|
|
|
['name' => 'rybakit'] |
|
57
|
|
|
] |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertNotEquals(-1, strpos($container[1]['request']->getBody(), $body)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testExpire() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->mock('say.hello')->willReturn(function($params) { |
|
66
|
|
|
return [ |
|
67
|
|
|
'msg' => 'Hello, ' . $params['name'], |
|
68
|
|
|
'hash' => md5(microtime(1)), |
|
69
|
|
|
'expire' => Carbon::now()->addHour()->getTimestamp(), |
|
70
|
|
|
]; |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
$nekufa1 = $this->dispatch('say.hello', ['name' => 'nekufa']); |
|
74
|
|
|
$nekufa2 = $this->dispatch('say.hello', ['name' => 'nekufa']); |
|
75
|
|
|
$vasya1 = $this->dispatch('say.hello', ['name' => 'vasya']); |
|
76
|
|
|
$this->assertSame($nekufa1->hash, $nekufa2->hash); |
|
77
|
|
|
$this->assertNotSame($nekufa1->hash, $vasya1->hash); |
|
78
|
|
|
|
|
79
|
|
|
Carbon::setTestNow(Carbon::now()->addDay()); |
|
80
|
|
|
$nekufa3 = $this->dispatch('say.hello', ['name' => 'nekufa']); |
|
81
|
|
|
$this->assertNotSame($nekufa1->hash, $nekufa3->hash); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function tearDown(): void |
|
85
|
|
|
{ |
|
86
|
|
|
parent::tearDown(); |
|
87
|
|
|
$this->get(Cache::class)->clear(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.