ZeroMqTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace AsyncPHP\Remit\Tests;
4
5
use AsyncPHP\Remit\Client\ZeroMqClient;
6
use AsyncPHP\Remit\Location\InMemoryLocation;
7
use AsyncPHP\Remit\Server\ZeroMqServer;
8
9
/**
10
 * @covers AsyncPHP\Remit\Client\ZeroMqClient
11
 * @covers AsyncPHP\Remit\Server\ZeroMqServer
12
 */
13
class ZeroMqTest extends Test
14
{
15
    /**
16
     * @var ZeroMqServer
17
     */
18
    protected $server;
19
    /**
20
     * @var ZeroMqClient
21
     */
22
    protected $client;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function setUp()
28
    {
29
        parent::setUp();
30
31
        $location = new InMemoryLocation("127.0.0.1", 5555);
32
33
        $this->server = new ZeroMqServer($location);
34
        $this->client = new ZeroMqClient($location);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function emitsToListeners()
41
    {
42
        $store = 0;
43
44
        $listener = function ($add) use (&$store) {
45
            $store += $add;
46
        };
47
48
        $this->server->addListener("foo", $listener);
49
50
        $this->client->emit("foo", array(1));
51
52
        $ticks = 0;
53
54 View Code Duplication
        while (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $this->server->tick();
56
57
            // the listener should have incremented this
58
59
            if ($store === 1) {
60
                break;
61
            }
62
63
            if (++$ticks >= 3) {
64
                $this->fail();
65
            }
66
67
            sleep(1);
68
        }
69
70
        $this->server->removeListener("foo", $listener);
71
72
        $this->client->emit("foo", array(1));
73
74
        $ticks = 0;
75
76 View Code Duplication
        while (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            $this->server->tick();
78
79
            // the listener should not have incremented this, because it's gone!
80
81
            if ($store === 1) {
82
                break;
83
            }
84
85
            if (++$ticks >= 3) {
86
                $this->fail();
87
            }
88
89
            sleep(1);
90
        }
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function canBeSerialized()
97
    {
98
        $this->assertEquals($this->server, unserialize(serialize($this->server)));
99
        $this->assertEquals($this->client, unserialize(serialize($this->client)));
100
    }
101
}
102