1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Update\Test\Unit;
use Magento\Update\Queue;
class QueueTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\Reader
*/
private $reader;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\Writer
*/
private $writer;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Update\Queue\JobFactory
*/
private $jobFactory;
/**
* @var Queue
*/
private $queue;
public function setUp()
{
$this->reader = $this->getMock('Magento\Update\Queue\Reader', [], [], '', false);
$this->writer = $this->getMock('Magento\Update\Queue\Writer', [], [], '', false);
$this->jobFactory = $this->getMock('Magento\Update\Queue\JobFactory', [], [], '', false);
$this->queue = new Queue($this->reader, $this->writer, $this->jobFactory);
}
public function testPeek()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"jobs": [{"name": "job A", "params" : []}, {"name": "job B", "params" : []}]}');
$this->assertEquals(['name' => 'job A', 'params' => []], $this->queue->peek());
}
public function testPeekEmpty()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('');
$this->assertEquals([], $this->queue->peek());
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "params" field is missing for one or more jobs
*/
public function testPeekException()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"jobs": [{"name": "job A"}, {"name": "job B"}]}');
$this->queue->peek();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "jobs" field is missing or is not an array
*/
public function testPeekExceptionNoJobsKey()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"foo": "bar"}');
$this->queue->peek();
}
public function testPopQueuedJob()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"jobs": [{"name": "job A", "params" : []}, {"name": "job B", "params" : []}]}');
$job = $this->getMockForAbstractClass('Magento\Update\Queue\AbstractJob', [], '', false);
$this->jobFactory->expects($this->once())->method('create')->with('job A', [])->willReturn($job);
$rawData = ['jobs' => [['name' => 'job B', 'params' => []]]];
$this->writer->expects($this->once())->method('write')->with(json_encode($rawData, JSON_PRETTY_PRINT));
$this->assertEquals($job, $this->queue->popQueuedJob());
}
public function testPopQueuedJobEmptyAfter()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"jobs": [{"name": "job A", "params" : []}]}');
$job = $this->getMockForAbstractClass('Magento\Update\Queue\AbstractJob', [], '', false);
$this->jobFactory->expects($this->once())->method('create')->with('job A', [])->willReturn($job);
$this->writer->expects($this->once())->method('write')->with('');
$this->assertEquals($job, $this->queue->popQueuedJob());
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "params" field is missing for one or more jobs
*/
public function testPopQueuedJobException()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"jobs": [{"name": "job A"}, {"name": "job B"}]}');
$this->writer->expects($this->never())->method('write');
$this->queue->popQueuedJob();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "jobs" field is missing or is not an array
*/
public function testPopQueuedJobExceptionNoJobsKey()
{
$this->reader->expects($this->once())
->method('read')
->willReturn('{"foo": "bar"}');
$this->writer->expects($this->never())->method('write');
$this->queue->popQueuedJob();
}
public function testAddJobs()
{
$queue = ['jobs' => []];
$this->reader->expects($this->at(0))->method('read')->willReturn('');
$queue['jobs'][] = ['name' => 'job A', 'params' => []];
$this->writer->expects($this->at(0))
->method('write')
->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->reader->expects($this->at(1))
->method('read')
->willReturn(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$queue['jobs'][] = ['name' => 'job B', 'params' => []];
$this->writer->expects($this->at(1))
->method('write')
->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->queue->addJobs([['name' => 'job A', 'params' => []], ['name' => 'job B', 'params' => []]]);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage field is missing for one or more jobs
*/
public function testAddJobsInvalidJobs()
{
$this->queue->addJobs([['no_name' => 'no job', 'no_params' => []]]);
}
}