Completed
Pull Request — master (#35)
by
unknown
03:36
created

QueueShellTest::testAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Queue\Test\TestCase\Shell;
3
4
use Cake\Console\Shell;
5
use Cake\Core\Configure;
6
use Cake\Datasource\ConnectionManager;
7
use Cake\TestSuite\TestCase;
8
use Queue\Shell\QueueShell;
9
10
/**
11
 * QueueShell Wrapper.
12
 */
13
class QueueShellWrapper extends QueueShell
14
{
15
16
    /**
17
     * A list with error messages.
18
     *
19
     * @var array
20
     */
21
    protected $_err = [];
22
23
    /**
24
     * A list with out messages.
25
     *
26
     * @var array
27
     */
28
    protected $_out = [];
29
30
    /**
31
     * Test double of `parent::err`.
32
     *
33
     * @return void
34
     */
35
    public function err($message = null, $newlines = 1)
36
    {
37
        $this->_err[] = $message;
38
    }
39
40
    /**
41
     * Test double of `parent::out`.
42
     *
43
     * @return void
44
     */
45
    public function out($message = null, $newlines = 1, $level = Shell::NORMAL)
46
    {
47
        $this->_out[] = $message;
48
    }
49
50
    /**
51
     *
52
     * {@inheritdoc}
53
     * @see \Cake\Console\Shell::_stop()
54
     */
55
    protected function _stop($status = 0)
56
    {
57
        return $status;
58
    }
59
60
    /**
61
     *
62
     * {@inheritDoc}
63
     * @see \Queue\Shell\QueueShell::_timeNeeded()
64
     */
65
    public function timeNeeded()
66
    {
67
        return parent::_timeNeeded();
68
    }
69
70
    /**
71
     *
72
     * {@inheritDoc}
73
     * @see \Queue\Shell\QueueShell::_memoryUsage()
74
     */
75
    public function memoryUsage()
76
    {
77
        return parent::_memoryUsage();
78
    }
79
80
    /**
81
     *
82
     * {@inheritDoc}
83
     * @see \Queue\Shell\QueueShell::_stringToArray()
84
     */
85
    public function stringToArray($param)
86
    {
87
        return parent::_stringToArray($param);
88
    }
89
}
90
91
class QueueShellTest extends TestCase
92
{
93
94
    /**
95
     *
96
     * @var QueueShellWrapper
97
     */
98
    public $QueueShell;
99
100
    /**
101
     * Fixtures to load
102
     *
103
     * @var array
104
     */
105
    public $fixtures = [
106
        'plugin.Queue.QueuedTasks'
107
    ];
108
109
    /**
110
     * Setup Defaults
111
     *
112
     * @return void
113
     */
114
    public function setUp()
115
    {
116
        parent::setUp();
117
118
        $this->QueueShell = $this->getMockBuilder(QueueShellWrapper::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Qu...', '_stop'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Queue\Test\TestCase\Shell\QueueShellWrapper of property $QueueShell.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
            ->setMethods([
120
                'in',
121
                'err',
122
                '_stop'
123
            ])
124
            ->getMock();
125
126
        $this->QueueShell->initialize();
0 ignored issues
show
Bug introduced by
The method initialize() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
        $this->QueueShell->/** @scrutinizer ignore-call */ 
127
                           initialize();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
        Configure::write('Queue', [
129
            'sleepTime' => 2,
130
            'defaultWorkerTimeout' => 3,
131
            'workerMaxRuntime' => 5,
132
            'cleanupTimeout' => 10,
133
            'exitWhenNothingToDo' => false,
134
            'log' => false
135
        ]);
136
    }
137
138
    /**
139
     *
140
     * @return void
141
     */
142
    public function testObject()
143
    {
144
        $this->assertTrue(is_object($this->QueueShell));
145
        $this->assertInstanceOf(QueueShell::class, $this->QueueShell);
146
    }
147
148
    /**
149
     *
150
     * @return void
151
     */
152
    public function testStats()
153
    {
154
        $this->_needsConnection();
155
156
        $this->QueueShell->stats();
157
        $this->assertContains('Total unfinished jobs: 0', $this->QueueShell->_out);
0 ignored issues
show
Bug Best Practice introduced by
The property $_out is declared protected in Queue\Test\TestCase\Shell\QueueShellWrapper. Since you implement __get, consider adding a @property or @property-read.
Loading history...
158
    }
159
160
    /**
161
     *
162
     * @return void
163
     */
164
    public function testSettings()
165
    {
166
        $this->QueueShell->settings();
167
        $this->assertContains('* cleanupTimeout: 10', $this->QueueShell->_out);
0 ignored issues
show
Bug Best Practice introduced by
The property $_out is declared protected in Queue\Test\TestCase\Shell\QueueShellWrapper. Since you implement __get, consider adding a @property or @property-read.
Loading history...
168
    }
169
170
    /**
171
     *
172
     * @return void
173
     */
174
    public function testAddInexistent()
175
    {
176
        $this->QueueShell->args[] = 'FooBar';
177
        $this->QueueShell->add();
178
        $this->assertContains('Error: Task not found: FooBar', $this->QueueShell->_out);
0 ignored issues
show
Bug Best Practice introduced by
The property $_out is declared protected in Queue\Test\TestCase\Shell\QueueShellWrapper. Since you implement __get, consider adding a @property or @property-read.
Loading history...
179
    }
180
181
    /**
182
     *
183
     * @return void
184
     */
185
    public function testAdd()
186
    {
187
        $this->QueueShell->args[] = 'Example';
188
        $this->QueueShell->add();
189
190
        $this->assertContains('OK, job created, now run the worker', $this->QueueShell->_out, print_r($this->QueueShell->_out, true));
0 ignored issues
show
Bug Best Practice introduced by
The property $_out is declared protected in Queue\Test\TestCase\Shell\QueueShellWrapper. Since you implement __get, consider adding a @property or @property-read.
Loading history...
191
    }
192
193
    /**
194
     *
195
     * @return void
196
     */
197
    public function testTimeNeeded()
198
    {
199
        $this->QueueShell = $this->getMockBuilder(QueueShellWrapper::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Qu...ay('_time'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Queue\Test\TestCase\Shell\QueueShellWrapper of property $QueueShell.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
200
            ->setMethods([
201
                '_time'
202
            ])
203
            ->getMock();
204
205
        $first = time();
206
        $second = $first - HOUR + MINUTE;
207
        $this->QueueShell->expects($this->at(0))
208
            ->method('_time')
209
            ->will($this->returnValue($first));
210
        $this->QueueShell->expects($this->at(1))
211
            ->method('_time')
212
            ->will($this->returnValue($second));
213
        $this->QueueShell->expects($this->exactly(2))
214
            ->method('_time')
215
            ->withAnyParameters();
216
217
        $result = $this->QueueShell->timeNeeded();
0 ignored issues
show
Bug introduced by
The method timeNeeded() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
        /** @scrutinizer ignore-call */ 
218
        $result = $this->QueueShell->timeNeeded();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
        $this->assertSame('3540s', $result);
219
    }
220
221
    /**
222
     *
223
     * @return void
224
     */
225
    public function testMemoryUsage()
226
    {
227
        $result = $this->QueueShell->memoryUsage();
228
        $this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.');
229
    }
230
231
    /**
232
     *
233
     * @return void
234
     */
235
    public function testStringToArray()
236
    {
237
        $string = 'Foo,Bar,';
238
        $result = $this->QueueShell->stringToArray($string);
239
240
        $expected = [
241
            'Foo',
242
            'Bar'
243
        ];
244
        $this->assertSame($expected, $result);
245
    }
246
247
    /**
248
     * Helper method for skipping tests that need a real connection.
249
     *
250
     * @return void
251
     */
252
    protected function _needsConnection()
253
    {
254
        $config = ConnectionManager::getConfig('test');
255
        $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.');
256
    }
257
}
258