Completed
Push — master ( 625195...735ce8 )
by
unknown
37:22
created

MongoFailedJobProviderTest::testFind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace SfCod\QueueBundle\Tests\Failer;
4
5
use Helmich\MongoMock\MockDatabase;
6
use MongoDB\Database;
7
use PHPUnit\Framework\TestCase;
8
use SfCod\QueueBundle\Base\MongoDriverInterface;
9
use SfCod\QueueBundle\Failer\MongoFailedJobProvider;
10
use Exception;
11
12
/**
13
 * Class MongoFailedJobProviderTest
14
 * @author Virchenko Maksim <[email protected]>
15
 * @package SfCod\QueueBundle\Tests\Failer
16
 */
17
class MongoFailedJobProviderTest extends TestCase
18
{
19
    /**
20
     * Test failed jobs logging
21
     */
22
    public function testLog()
23
    {
24
        list($connection, $queue, $payload, $exception, $collection) = $this->mockData();
25
26
        $database = new MockDatabase();
27
        $provider = $this->mockProvider($database, $collection);
28
29
        $provider->log($connection, $queue, $payload, $exception);
30
31
        $record = $database->selectCollection($collection)->findOne([
32
            'connection' => $connection,
33
            'queue' => $queue,
34
            'payload' => $payload,
35
            'exception' => $exception->getMessage(),
36
        ]);
37
38
        $this->assertNotNull($record, 'Log missed in mongodb.');
39
    }
40
41
    /**
42
     * Test fetching all failed jobs
43
     */
44
    public function testAll()
45
    {
46
        list($connection, $queue, $payload, $exception, $collection) = $this->mockData();
47
48
        $database = new MockDatabase();
49
        $provider = $this->mockProvider($database, $collection);
50
51
        for ($i = 0; $i < 10; $i++) {
52
            $provider->log($connection, $queue, $payload, $exception);
53
        }
54
55
        $count = $database->selectCollection($collection)->count();
56
57
        $this->assertEquals(10, $count);
58
    }
59
60
    /**
61
     * Test find jobs
62
     */
63
    public function testFind()
64
    {
65
        list($connection, $queue, $payload, $exception, $collection) = $this->mockData();
66
67
        $database = new MockDatabase();
68
        $provider = $this->mockProvider($database, $collection);
69
70
        $provider->log($connection, $queue, $payload, $exception);
71
72
        $record = $database->selectCollection($collection)->findOne([
73
            'connection' => $connection,
74
            'queue' => $queue,
75
            'payload' => $payload,
76
            'exception' => $exception->getMessage(),
77
        ]);
78
79
        $this->assertEquals($record, $provider->find($record->_id));
80
    }
81
82
    /**
83
     * Test forget failed job
84
     */
85
    public function testForget()
86
    {
87
        list($connection, $queue, $payload, $exception, $collection) = $this->mockData();
88
89
        $database = new MockDatabase();
90
        $provider = $this->mockProvider($database, $collection);
91
92
        $provider->log($connection, $queue, $payload, $exception);
93
94
        $record = $database->selectCollection($collection)->findOne([
95
            'connection' => $connection,
96
            'queue' => $queue,
97
            'payload' => $payload,
98
            'exception' => $exception->getMessage(),
99
        ]);
100
101
        $provider->forget($record->_id);
102
103
        $record = $database->selectCollection($collection)->findOne(['_id' => $record->_id]);
104
105
        $this->assertNull($record);
106
    }
107
108
    /**
109
     * Test flush failed jobs
110
     */
111
    public function testFlush()
112
    {
113
        list($connection, $queue, $payload, $exception, $collection) = $this->mockData();
114
115
        $database = new MockDatabase();
116
        $provider = $this->mockProvider($database, $collection);
117
118
        for ($i = 0; $i < 10; $i++) {
119
            $provider->log($connection, $queue, $payload, $exception);
120
        }
121
122
        $count = $database->selectCollection($collection)->count();
123
124
        $this->assertEquals(10, $count);
125
126
        $provider->flush();
127
        $count = $database->selectCollection($collection)->count();
128
129
        $this->assertEquals(0, $count);
130
    }
131
132
    /**
133
     * Mock data
134
     *
135
     * @return array
136
     */
137
    private function mockData(): array
138
    {
139
        return array_values([
140
            'connection' => uniqid('connection_'),
141
            'queue' => uniqid('queue_'),
142
            'payload' => json_encode(range(1, 10)),
143
            'exception' => new Exception(uniqid('message_')),
144
            'collection' => 'queue_jobs_failed_test',
145
        ]);
146
    }
147
148
    /**
149
     * Mock mongo failed provider
150
     *
151
     * @param Database $database
152
     * @param string $collection
153
     *
154
     * @return MongoFailedJobProvider
155
     */
156
    private function mockProvider(Database $database, string $collection): MongoFailedJobProvider
157
    {
158
        $mongo = $this->createMock(MongoDriverInterface::class);
159
        $mongo
160
            ->expects($this->any())
161
            ->method('getDatabase')
162
            ->will($this->returnValue($database));
163
164
        $provider = new MongoFailedJobProvider($mongo, $collection);
0 ignored issues
show
Documentation introduced by
$mongo is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<SfCod\QueueBundle...e\MongoDriverInterface>.

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...
165
166
        return $provider;
167
    }
168
}