3
* Spring Signage Ltd - http://www.springsignage.com
4
* Copyright (C) 2015 Spring Signage Ltd
8
namespace Xibo\Tests\Integration;
10
use Xibo\Helper\Random;
11
use Xibo\OAuth2\Client\Entity\XiboDaypart;
12
use Xibo\Tests\LocalWebTestCase;
16
* @package Xibo\Tests\Integration
19
class DaypartTest extends LocalWebTestCase
21
protected $startDayparts;
23
* setUp - called before every test automatically
25
public function setup()
28
$this->startDayParts = (new XiboDaypart($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
31
* tearDown - called after every test automatically
33
public function tearDown()
35
// tearDown all dayparts that weren't there initially
36
$finalDayparts = (new XiboDaypart($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
37
# Loop over any remaining dayparts and nuke them
38
foreach ($finalDayparts as $daypart) {
39
/** @var XiboDaypart $daypart */
41
foreach ($this->startDayparts as $startDaypart) {
42
if ($startDaypart->dayPartId == $daypart->dayPartId) {
49
} catch (\Exception $e) {
50
fwrite(STDERR, 'Unable to delete ' . $daypart->dayPartId . '. E:' . $e->getMessage());
58
* testAddSuccess - test adding various daypart that should be valid
59
* @dataProvider provideSuccessCases
61
public function testAddSuccess($name, $description, $startTime, $endTime, $exceptionDays, $exceptionStartTimes, $exceptionEndTimes)
63
# Create daypart with arguments from provideSuccessCases
64
$response = $this->client->post('/daypart', [
66
'description' => $description,
67
'startTime' => $startTime,
68
'endTime' => $endTime,
69
'exceptionDays' => $exceptionDays,
70
'exceptionStartTimes' => $exceptionStartTimes,
71
'exceptionEndTimes' => $exceptionEndTimes
73
$this->assertSame(200, $this->client->response->status(), "Not successful: " . $response);
74
$object = json_decode($this->client->response->body());
75
$this->assertObjectHasAttribute('data', $object);
76
$this->assertObjectHasAttribute('id', $object);
77
$this->assertSame($name, $object->data->name);
78
$this->assertSame($description, $object->data->description);
79
# Check that the daypart was really added
80
$dayparts = (new XiboDaypart($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
81
$this->assertEquals(count($this->startDayparts) + 1, count($dayparts));
82
# Check that the daypart was added correctly
83
$daypart = (new XiboDaypart($this->getEntityProvider()))->getById($object->id);
84
$this->assertSame($name, $daypart->name);
85
$this->assertSame($description, $daypart->description);
86
# Clean up the daypart as we no longer need it
87
$this->assertTrue($daypart->delete(), 'Unable to delete ' . $daypart->dayPartId);
91
* testAddFailure - test adding various daypart that should be invalid
92
* @dataProvider provideFailureCases
94
public function testAddFailure($name, $description, $startTime, $endTime, $exceptionDays, $exceptionStartTimes, $exceptionEndTimes)
96
# Create daypart with arguments from provideFailureCases
97
$response = $this->client->post('/daypart', [
99
'description' => $description,
100
'startTime' => $startTime,
101
'endTime' => $endTime,
102
'exceptionDays' => $exceptionDays,
103
'exceptionStartTimes' => $exceptionStartTimes,
104
'exceptionEndTimes' => $exceptionEndTimes
106
# check if they fail as expected
107
$this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
111
* Each array is a test run
112
* Format ($name, $description, $startTime, $endTime, $exceptionDays, $exceptionStartTimes, $exceptionEndTimes)
115
public function provideSuccessCases()
117
# Data for testAddSuccess, easily expandable - just add another set of data below
119
'No exceptions' => ['phpunit daypart', 'API', '02:00', '06:00', NULL, NULL, NULL],
120
'Except Monday' => ['phpunit daypart exception', NULL, '02:00', '06:00', ['Monday'], ['00:01'], ['23:59']]
124
* Each array is a test run
125
* Format ($name, $description, $startTime, $endTime, $exceptionDays, $exceptionStartTimes, $exceptionEndTimes)
128
public function provideFailureCases()
130
# Data for testAddfailure, easily expandable - just add another set of data below
132
'Empty title' => [NULL, 'should be invalid', '07:00', '10:00', NULL, NULL, NULL],
133
// 'Description over 254 characters' => ['Too long description', Random::generateString(255), '07:00', '10:00', NULL, NULL, NULL],
134
// 'Wrong time data type' => ['Time as integer','should be incorrect', 21, 22, NULL, NULL, NULL],
135
// 'Wrong day name' => ['phpunit daypart exception', NULL, '02:00', '06:00', ['Cabbage'], ['00:01'], ['23:59']]
140
* Edit an existing daypart
142
public function testEdit()
145
$daypart = (new XiboDaypart($this->getEntityProvider()))->create('phpunit daypart', 'API', '02:00', '06:00', NULL, NULL, NULL);
146
# Change the daypart name and description
147
$name = Random::generateString(8, 'phpunit');
148
$description = Random::generateString(8, 'description');
149
$this->client->put('/daypart/' . $daypart->dayPartId, [
151
'description' => $description,
152
'startTime' => '02:00',
153
'endTime' => '06:00',
154
'exceptionDays' => $daypart->exceptionDays,
155
'exceptionStartTimes' => $daypart->exceptionStartTimes,
156
'exceptionEndTimes' => $daypart->exceptionEndTimes
157
], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
159
$this->assertSame(200, $this->client->response->status(), 'Not successful: ' . $this->client->response->body());
160
$object = json_decode($this->client->response->body());
161
# Examine the returned object and check that it's what we expect
162
$this->assertObjectHasAttribute('data', $object);
163
$this->assertObjectHasAttribute('id', $object);
164
$this->assertSame($name, $object->data->name);
165
$this->assertSame($description, $object->data->description);
166
# Check that the daypart was actually renamed
167
$daypart = (new XiboDaypart($this->getEntityProvider()))->getById($object->id);
168
$this->assertSame($name, $daypart->name);
169
$this->assertSame($description, $daypart->description);
170
# Clean up the Daypart as we no longer need it
178
public function testDelete()
180
$name1 = Random::generateString(8, 'phpunit');
181
$name2 = Random::generateString(8, 'phpunit');
182
# Load in a couple of known Dayparts
183
$daypart1 = (new XiboDaypart($this->getEntityProvider()))->create($name1, 'API', '02:00', '06:00', NULL, NULL, NULL);
184
$daypart2 = (new XiboDaypart($this->getEntityProvider()))->create($name2, 'API', '12:00', '16:00', NULL, NULL, NULL);
185
# Delete the one we created last
186
$this->client->delete('/daypart/' . $daypart2->dayPartId);
187
# This should return 204 for success
188
$response = json_decode($this->client->response->body());
189
$this->assertSame(204, $response->status, $this->client->response->body());
190
# Check only one remains
191
$dayparts = (new XiboDaypart($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
192
$this->assertEquals(count($this->startDayparts) + 1, count($dayparts));
194
foreach ($dayparts as $daypart) {
195
if ($daypart->dayPartId == $daypart1->dayPartId) {
199
$this->assertTrue($flag, 'Daypart ID ' . $daypart1->dayPartId . ' was not found after deleting a different daypart');