Completed
Push — master ( 04839b...f87650 )
by Dmitry
03:31
created

DispatcherTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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()
85
    {
86
        parent::tearDown();
87
        $this->get(Cache::class)->clear();
88
    }
89
}
90