MongoFailedJobProviderTest::mockData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
169
170
        return $provider;
171
    }
172
}
173