~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to tests/integration/DaypartTest.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (DaypartTest.php)
6
 
 */
7
 
 
8
 
namespace Xibo\Tests\Integration;
9
 
 
10
 
use Xibo\Helper\Random;
11
 
use Xibo\OAuth2\Client\Entity\XiboDaypart;
12
 
use Xibo\Tests\LocalWebTestCase;
13
 
 
14
 
/**
15
 
 * Class DaypartTest
16
 
 * @package Xibo\Tests\Integration
17
 
 */
18
 
 
19
 
class DaypartTest extends LocalWebTestCase
20
 
{
21
 
        protected $startDayparts;
22
 
    /**
23
 
     * setUp - called before every test automatically
24
 
     */
25
 
    public function setup()
26
 
    {  
27
 
        parent::setup();
28
 
        $this->startDayParts = (new XiboDaypart($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
29
 
    }
30
 
    /**
31
 
     * tearDown - called after every test automatically
32
 
     */
33
 
    public function tearDown()
34
 
    {
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 */
40
 
            $flag = true;
41
 
            foreach ($this->startDayparts as $startDaypart) {
42
 
               if ($startDaypart->dayPartId == $daypart->dayPartId) {
43
 
                   $flag = false;
44
 
               }
45
 
            }
46
 
            if ($flag) {
47
 
                try {
48
 
                    $daypart->delete();
49
 
                } catch (\Exception $e) {
50
 
                    fwrite(STDERR, 'Unable to delete ' . $daypart->dayPartId . '. E:' . $e->getMessage());
51
 
                }
52
 
            }
53
 
        }
54
 
        parent::tearDown();
55
 
    }
56
 
 
57
 
        /**
58
 
     * testAddSuccess - test adding various daypart that should be valid
59
 
     * @dataProvider provideSuccessCases
60
 
     */
61
 
    public function testAddSuccess($name, $description, $startTime, $endTime, $exceptionDays,  $exceptionStartTimes, $exceptionEndTimes)
62
 
    {
63
 
        # Create daypart with arguments from provideSuccessCases
64
 
        $response = $this->client->post('/daypart', [
65
 
                        'name' => $name,
66
 
                        'description' => $description,
67
 
                        'startTime' => $startTime,
68
 
                        'endTime' => $endTime,
69
 
                        'exceptionDays' => $exceptionDays,
70
 
                        'exceptionStartTimes' => $exceptionStartTimes,
71
 
                        'exceptionEndTimes' => $exceptionEndTimes
72
 
        ]);
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);
88
 
    }
89
 
    
90
 
    /**
91
 
     * testAddFailure - test adding various daypart that should be invalid
92
 
     * @dataProvider provideFailureCases
93
 
     */
94
 
    public function testAddFailure($name, $description, $startTime, $endTime, $exceptionDays,  $exceptionStartTimes, $exceptionEndTimes)
95
 
    {
96
 
        # Create daypart with arguments from provideFailureCases
97
 
        $response = $this->client->post('/daypart', [
98
 
                        'name' => $name,
99
 
                        'description' => $description,
100
 
                        'startTime' => $startTime,
101
 
                        'endTime' => $endTime,
102
 
                        'exceptionDays' => $exceptionDays,
103
 
                        'exceptionStartTimes' => $exceptionStartTimes,
104
 
                        'exceptionEndTimes' => $exceptionEndTimes
105
 
        ]);
106
 
        # check if they fail as expected
107
 
        $this->assertSame(500, $this->client->response->status(), 'Expecting failure, received ' . $this->client->response->status());
108
 
    }
109
 
 
110
 
    /**
111
 
     * Each array is a test run
112
 
     * Format ($name, $description, $startTime, $endTime, $exceptionDays,  $exceptionStartTimes, $exceptionEndTimes)
113
 
     * @return array
114
 
     */
115
 
    public function provideSuccessCases()
116
 
    {
117
 
        # Data for testAddSuccess, easily expandable - just add another set of data below
118
 
        return [
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']]
121
 
        ];
122
 
    }
123
 
    /**
124
 
     * Each array is a test run
125
 
     * Format ($name, $description, $startTime, $endTime, $exceptionDays,  $exceptionStartTimes, $exceptionEndTimes)
126
 
     * @return array
127
 
     */
128
 
    public function provideFailureCases()
129
 
    {
130
 
        # Data for testAddfailure, easily expandable - just add another set of data below
131
 
        return [
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']]
136
 
        ];
137
 
    }
138
 
 
139
 
    /**
140
 
    * Edit an existing daypart
141
 
    */
142
 
    public function testEdit()
143
 
    {
144
 
        #Create new daypart
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, [
150
 
            'name' => $name,
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']);
158
 
        
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
171
 
        $daypart->delete();
172
 
    }
173
 
 
174
 
     /**
175
 
     * Test delete
176
 
     * @group minimal
177
 
     */
178
 
    public function testDelete()
179
 
    {
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));
193
 
        $flag = false;
194
 
        foreach ($dayparts as $daypart) {
195
 
            if ($daypart->dayPartId == $daypart1->dayPartId) {
196
 
                $flag = true;
197
 
            }
198
 
        }
199
 
        $this->assertTrue($flag, 'Daypart ID ' . $daypart1->dayPartId . ' was not found after deleting a different daypart');
200
 
        $daypart1->delete();
201
 
    }
202
 
}