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