~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to tests/integration/Widget/ClockWidgetTestCase.php

  • Committer: GitHub
  • Author(s): Dan Garner
  • Date: 2017-02-23 11:57:51 UTC
  • mfrom: (551.3.19)
  • Revision ID: git-v1:3dd6e41bcc903d9c60a0023623a01142526950c2
Merge pull request #236 from PeterMis/feature/test-suite-2

Feature/test suite 2

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
 * (ClockWidgetTestCase.php)
 
6
 */
 
7
 
 
8
namespace Xibo\Tests\Integration\Widget;
 
9
 
 
10
use Xibo\Helper\Random;
 
11
use Xibo\OAuth2\Client\Entity\XiboLayout;
 
12
use Xibo\OAuth2\Client\Entity\XiboRegion;
 
13
use Xibo\OAuth2\Client\Entity\XiboClock;
 
14
use Xibo\OAuth2\Client\Entity\XiboWidget;
 
15
use Xibo\Tests\LocalWebTestCase;
 
16
use Xibo\Tests\Integration\Widget\WidgetTestCase;
 
17
 
 
18
class ClockWidgetTestCase extends WidgetTestCase
 
19
{
 
20
        protected $startLayouts;
 
21
    /**
 
22
     * setUp - called before every test automatically
 
23
     */
 
24
    public function setup()
 
25
    {  
 
26
        parent::setup();
 
27
        $this->startLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
 
28
    }
 
29
    /**
 
30
     * tearDown - called after every test automatically
 
31
     */
 
32
    public function tearDown()
 
33
    {
 
34
        // tearDown all layouts that weren't there initially
 
35
        $finalLayouts = (new XiboLayout($this->getEntityProvider()))->get(['start' => 0, 'length' => 10000]);
 
36
        # Loop over any remaining layouts and nuke them
 
37
        foreach ($finalLayouts as $layout) {
 
38
            /** @var XiboLayout $layout */
 
39
            $flag = true;
 
40
            foreach ($this->startLayouts as $startLayout) {
 
41
               if ($startLayout->layoutId == $layout->layoutId) {
 
42
                   $flag = false;
 
43
               }
 
44
            }
 
45
            if ($flag) {
 
46
                try {
 
47
                    $layout->delete();
 
48
                } catch (\Exception $e) {
 
49
                    fwrite(STDERR, 'Unable to delete ' . $layout->layoutId . '. E:' . $e->getMessage());
 
50
                }
 
51
            }
 
52
        }
 
53
        parent::tearDown();
 
54
    }
 
55
 
 
56
        /**
 
57
         * @group add
 
58
     * @dataProvider provideSuccessCases
 
59
     */
 
60
        public function testAdd($name, $duration, $useDuration, $theme, $clockTypeId, $offset, $format, $showSeconds, $clockFace)
 
61
        {
 
62
                # Create layout 
 
63
        $layout = (new XiboLayout($this->getEntityProvider()))->create('Clock add Layout', 'phpunit description', '', 9);
 
64
        # Add region to our layout
 
65
        $region = (new XiboRegion($this->getEntityProvider()))->create($layout->layoutId, 1000,1000,200,200);
 
66
 
 
67
                $response = $this->client->post('/playlist/widget/clock/' . $region->playlists[0]['playlistId'], [
 
68
                'name' => $name,
 
69
                'duration' => $duration,
 
70
            'useDuration' => $useDuration,
 
71
                'themeId' => $theme,
 
72
                'clockTypeId' => $clockTypeId,
 
73
                'offset' => $offset,
 
74
                'format' => $format,
 
75
                'showSeconds' => $showSeconds,
 
76
                'clockFace' => $clockFace
 
77
                ]);
 
78
        $this->assertSame(200, $this->client->response->status());
 
79
        $this->assertNotEmpty($this->client->response->body());
 
80
        $object = json_decode($this->client->response->body());
 
81
        $this->assertObjectHasAttribute('data', $object, $this->client->response->body());
 
82
        $clockOptions = (new XiboClock($this->getEntityProvider()))->getById($region->playlists[0]['playlistId']);
 
83
        $this->assertSame($name, $clockOptions->name);
 
84
        $this->assertSame($duration, $clockOptions->duration);
 
85
        
 
86
        foreach ($clockOptions->widgetOptions as $option) {
 
87
            if ($option['option'] == 'theme') {
 
88
                $this->assertSame($theme, intval($option['value']));
 
89
            }
 
90
            if ($option['option'] == 'clockTypeId') {
 
91
                $this->assertSame($clockTypeId, intval($option['value']));
 
92
            }
 
93
            if ($option['option'] == 'offset') {
 
94
                $this->assertSame($offset, intval($option['value']));
 
95
            }
 
96
            if ($option['option'] == 'format') {
 
97
                $this->assertSame($format, $option['value']);
 
98
            }
 
99
            if ($option['option'] == 'showSeconds') {
 
100
                $this->assertSame($showSeconds, intval($option['value']));
 
101
            }
 
102
            if ($option['option'] == 'clockFace') {
 
103
                $this->assertSame($clockFace, $option['value']);
 
104
            }
 
105
        }
 
106
        }
 
107
 
 
108
        /**
 
109
     * Each array is a test run
 
110
     * Format ($name, $duration, $useDuration, $theme, $clockTypeId, $offset, $format, $showSeconds, $clockFace)
 
111
     * @return array
 
112
     */
 
113
    public function provideSuccessCases()
 
114
    {
 
115
        # Sets of data used in testAdd
 
116
        return [
 
117
            'Analogue' => ['Api Analogue clock', 20, 1, 1, 1, 0, '', 0, 'TwentyFourHourClock'],
 
118
            'Digital' => ['API digital clock', 20, 1, 0, 2, 0, '[HH:mm]', 0, 'TwentyFourHourClock'],
 
119
            'Flip 24h' => ['API Flip clock 24h', 5, 1, 0, 3, 0, '', 1, 'TwentyFourHourClock'],
 
120
            'Flip counter' => ['API Flip clock Minute counter', 50, 1, 0, 3, 0, '', 1, 'MinuteCounter']
 
121
        ];
 
122
    }
 
123
 
 
124
    public function testEdit()
 
125
    {
 
126
        # Create layout 
 
127
        $layout = (new XiboLayout($this->getEntityProvider()))->create('Clock edit Layout', 'phpunit description', '', 9);
 
128
        # Add region to our layout
 
129
        $region = (new XiboRegion($this->getEntityProvider()))->create($layout->layoutId, 1000,1000,200,200);
 
130
        # Create a clock with wrapper
 
131
        $clock = (new XiboClock($this->getEntityProvider()))->create('Api Analogue clock', 20, 1, 1, 1, NULL, NULL, NULL, NULL, $region->playlists[0]['playlistId']);
 
132
        $nameNew = 'Edited Name';
 
133
        $durationNew = 80;
 
134
        $clockTypeIdNew = 3;
 
135
        $response = $this->client->put('/playlist/widget/' . $clock->widgetId, [
 
136
                'name' => $nameNew,
 
137
                'duration' => $durationNew,
 
138
                'themeId' => $clock->theme,
 
139
                'clockTypeId' => $clockTypeIdNew,
 
140
                'offset' => $clock->offset,
 
141
                'format' => $clock->format,
 
142
                'showSeconds' => $clock->showSeconds,
 
143
                'clockFace' => $clock->clockFace
 
144
                ], ['CONTENT_TYPE' => 'application/x-www-form-urlencoded']);
 
145
        $this->assertSame(200, $this->client->response->status());
 
146
        $this->assertNotEmpty($this->client->response->body());
 
147
        $object = json_decode($this->client->response->body());
 
148
        $this->assertObjectHasAttribute('data', $object, $this->client->response->body());
 
149
        $clockOptions = (new XiboClock($this->getEntityProvider()))->getById($region->playlists[0]['playlistId']);
 
150
        $this->assertSame($nameNew, $clockOptions->name);
 
151
        $this->assertSame($durationNew, $clockOptions->duration);
 
152
        foreach ($clockOptions->widgetOptions as $option) {
 
153
            if ($option['option'] == 'clockTypeId') {
 
154
                $this->assertSame($clockTypeIdNew, intval($option['value']));
 
155
            }
 
156
        }
 
157
    }
 
158
    public function testDelete()
 
159
    {
 
160
        # Create layout 
 
161
        $layout = (new XiboLayout($this->getEntityProvider()))->create('Clock delete Layout', 'phpunit description', '', 9);
 
162
        # Add region to our layout
 
163
        $region = (new XiboRegion($this->getEntityProvider()))->create($layout->layoutId, 1000,1000,200,200);
 
164
        # Create a clock with wrapper
 
165
                $clock = (new XiboClock($this->getEntityProvider()))->create('Api Analogue clock', 20, 1, 1, 1, NULL, NULL, NULL, NULL, $region->playlists[0]['playlistId']);
 
166
                # Delete it
 
167
                $this->client->delete('/playlist/widget/' . $clock->widgetId);
 
168
        $response = json_decode($this->client->response->body());
 
169
        $this->assertSame(200, $response->status, $this->client->response->body());
 
170
    }
 
171
}