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

QueueShellTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 27
rs 9.6
c 0
b 0
f 0
1
<?php
2
namespace Queue\Test\TestCase\Shell;
3
4
use Cake\Console\ConsoleIo;
5
use Cake\Core\Configure;
6
use Cake\Datasource\ConnectionManager;
7
use Cake\TestSuite\TestCase;
8
use Queue\Shell\QueueShell;
9
use Tools\TestSuite\ConsoleOutput;
10
use Tools\TestSuite\ToolsTestTrait;
11
12
class QueueShellTest extends TestCase
13
{
14
    use ToolsTestTrait;
15
16
    /**
17
     *
18
     * @var \Queue\Shell\QueueShell|\PHPUnit_Framework_MockObject_MockObject
19
     */
20
    public $QueueShell;
21
22
    /**
23
     *
24
     * @var \Tools\TestSuite\ConsoleOutput
25
     */
26
    public $out;
27
28
    /**
29
     *
30
     * @var \Tools\TestSuite\ConsoleOutput
31
     */
32
    public $err;
33
34
    /**
35
     * Fixtures to load
36
     *
37
     * @var array
38
     */
39
    public $fixtures = [
40
        'plugin.Queue.QueuedTasks'
41
    ];
42
43
    /**
44
     * Setup Defaults
45
     *
46
     * @return void
47
     */
48
    public function setUp()
49
    {
50
        parent::setUp();
51
52
        $this->out = new ConsoleOutput();
53
        $this->err = new ConsoleOutput();
54
        $io = new ConsoleIo($this->out, $this->err);
55
56
        $this->QueueShell = $this->getMockBuilder(QueueShell::class)
57
            ->setMethods([
58
                'in',
59
                'err',
60
                '_stop'
61
            ])
62
            ->setConstructorArgs([
63
                $io
64
            ])
65
            ->getMock();
66
        $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

66
        $this->QueueShell->/** @scrutinizer ignore-call */ 
67
                           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...
67
68
        Configure::write('Queue', [
69
            'sleepTime' => 2,
70
            'defaultWorkerTimeout' => 3,
71
            'workerMaxRuntime' => 5,
72
            'cleanupTimeout' => 10,
73
            'exitWhenNothingToDo' => false,
74
            'log' => false
75
        ]);
76
    }
77
78
    /**
79
     *
80
     * @return void
81
     */
82
    public function testObject()
83
    {
84
        $this->assertTrue(is_object($this->QueueShell));
85
        $this->assertInstanceOf(QueueShell::class, $this->QueueShell);
86
    }
87
88
    /**
89
     *
90
     * @return void
91
     */
92
    public function testStats()
93
    {
94
        $this->_needsConnection();
95
96
        $this->QueueShell->stats();
97
        $this->assertContains('Total unfinished jobs: 0', $this->out->output());
98
    }
99
100
    /**
101
     *
102
     * @return void
103
     */
104
    public function testSettings()
105
    {
106
        $this->QueueShell->settings();
107
        $this->assertContains('* cleanupTimeout: 10', $this->out->output());
108
    }
109
110
    /**
111
     *
112
     * @return void
113
     */
114
    public function testAddInexistent()
115
    {
116
        $this->QueueShell->args[] = 'FooBar';
117
        $this->QueueShell->add();
118
        $this->assertContains('Error: Task not found: FooBar', $this->out->output());
119
    }
120
121
    /**
122
     *
123
     * @return void
124
     */
125
    public function testAdd()
126
    {
127
        $this->QueueShell->args[] = 'Example';
128
        $this->QueueShell->add();
129
130
        $this->assertContains('OK, job created, now run the worker', $this->out->output(), print_r($this->out->output(), true));
131
    }
132
133
    /**
134
     *
135
     * @return void
136
     */
137
    public function testTimeNeeded()
138
    {
139
        $this->QueueShell = $this->getMockBuilder(QueueShell::class)
140
            ->setMethods([
141
                '_time'
142
            ])
143
            ->getMock();
144
145
        $first = time();
146
        $second = $first - HOUR + MINUTE;
147
        $this->QueueShell->expects($this->at(0))
148
            ->method('_time')
149
            ->will($this->returnValue($first));
150
        $this->QueueShell->expects($this->at(1))
151
            ->method('_time')
152
            ->will($this->returnValue($second));
153
        $this->QueueShell->expects($this->exactly(2))
154
            ->method('_time')
155
            ->withAnyParameters();
156
157
        $result = $this->invokeMethod($this->QueueShell, '_timeNeeded');
158
        $this->assertSame('3540s', $result);
159
    }
160
161
    /**
162
     *
163
     * @return void
164
     */
165
    public function testMemoryUsage()
166
    {
167
        $result = $this->invokeMethod($this->QueueShell, '_memoryUsage');
168
        $this->assertRegExp('/^\d+MB/', $result, 'Should be e.g. `17MB` or `17MB/1GB` etc.');
169
    }
170
171
    /**
172
     *
173
     * @return void
174
     */
175
    public function testStringToArray()
176
    {
177
        $string = 'Foo,Bar,';
178
        $result = $this->invokeMethod($this->QueueShell, '_stringToArray', [$string]);
179
180
        $expected = [
181
            'Foo',
182
            'Bar'
183
        ];
184
        $this->assertSame($expected, $result);
185
    }
186
187
    /**
188
     * Helper method for skipping tests that need a real connection.
189
     *
190
     * @return void
191
     */
192
    protected function _needsConnection()
193
    {
194
        $config = ConnectionManager::getConfig('test');
195
        $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.');
196
    }
197
}
198