EventTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fireEvent() 0 4 1
A testEventFire() 0 7 1
A testSubscription() 0 11 1
A testProcessing() 0 28 1
1
<?php
2
3
namespace Test;
4
5
use Basis\Converter;
6
use Basis\Event;
7
use Basis\Http;
8
use Basis\Test;
9
10
class EventTest extends Test
11
{
12
    public $mocks = [
13
        ['event.fire', [], 'fireEvent']
14
    ];
15
16
    private $firedEvents = [];
17
18
    public function fireEvent($params)
19
    {
20
        $this->firedEvents[] = $params;
21
    }
22
23
    public function testEventFire()
24
    {
25
        $this->get(Event::class)->fire('person.authorized', ['name' => 'nekufa']);
26
        $this->assertCount(1, $this->firedEvents);
0 ignored issues
show
Documentation introduced by
$this->firedEvents is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
        $this->assertSame($this->firedEvents[0]->event, 'test.person.authorized');
28
        $this->assertSame(get_object_vars($this->firedEvents[0]->context), ['name' => 'nekufa']);
29
    }
30
31
    public function testSubscription()
32
    {
33
        $event = $this->get(Event::class);
34
        $subscription = $event->getSubscription();
35
36
        $this->assertArrayHasKey('person.created', $subscription);
37
        $this->assertSame($subscription['person.created'], ['CreateLogin']);
38
39
        $this->assertArrayHasKey('person.person.*', $subscription);
40
        $this->assertSame($subscription['person.person.*'], ['CreateLogin']);
41
    }
42
43
    public function testProcessing()
44
    {
45
        $_REQUEST = array_merge($_REQUEST, [
46
            'event' => 'person.created',
47
            'context' => json_encode([ 'name' => 'nekufa' ]),
48
        ]);
49
50
        $result = $this->get(Http::class)->process('/event');
51
        $response = json_decode($result);
52
53
        $this->assertTrue($response->success);
54
        $this->assertNotNull($response->data->CreateLogin);
55
56
        $this->assertSame($response->data->CreateLogin->msg, 'person was created with name nekufa');
57
58
        $_REQUEST = array_merge($_REQUEST, [
59
            'event' => 'person.person.updated',
60
            'context' => json_encode([ 'name' => 'dmitry' ]),
61
        ]);
62
63
        $result = $this->get(Http::class)->process('/event');
64
        $response = json_decode($result);
65
66
        $this->assertTrue($response->success);
67
        $this->assertNotNull($response->data->CreateLogin);
68
69
        $this->assertSame($response->data->CreateLogin->msg, 'person.person was updated with name dmitry');
70
    }
71
}
72