|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> |
| 4 | + * |
| 5 | + * @license GNU AGPL version 3 or any later version |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +namespace OCA\WorkflowEngine\Tests\Check; |
| 23 | + |
| 24 | + |
| 25 | +class RequestTimeTest extends \Test\TestCase { |
| 26 | + |
| 27 | + /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 28 | + protected $timeFactory; |
| 29 | + |
| 30 | + protected function setUp() { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory') |
| 34 | + ->getMock(); |
| 35 | + } |
| 36 | + |
| 37 | + public function dataExecuteCheck() { |
| 38 | + return [ |
| 39 | + [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467870105, false], // 2016-07-07T07:41:45+02:00 |
| 40 | + [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467873705, true], // 2016-07-07T08:41:45+02:00 |
| 41 | + [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467902505, true], // 2016-07-07T16:41:45+02:00 |
| 42 | + [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467906105, false], // 2016-07-07T17:41:45+02:00 |
| 43 | + [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467870105, true], // 2016-07-07T07:41:45+02:00 |
| 44 | + [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467873705, false], // 2016-07-07T08:41:45+02:00 |
| 45 | + [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467902505, false], // 2016-07-07T16:41:45+02:00 |
| 46 | + [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467906105, true], // 2016-07-07T17:41:45+02:00 |
| 47 | + |
| 48 | + [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467843105, false], // 2016-07-07T07:41:45+09:30 |
| 49 | + [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467846705, true], // 2016-07-07T08:41:45+09:30 |
| 50 | + [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467875505, true], // 2016-07-07T16:41:45+09:30 |
| 51 | + [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467879105, false], // 2016-07-07T17:41:45+09:30 |
| 52 | + [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467843105, true], // 2016-07-07T07:41:45+09:30 |
| 53 | + [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467846705, false], // 2016-07-07T08:41:45+09:30 |
| 54 | + [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467875505, false], // 2016-07-07T16:41:45+09:30 |
| 55 | + [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467879105, true], // 2016-07-07T17:41:45+09:30 |
| 56 | + |
| 57 | + [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467916905, false], // 2016-07-07T07:41:45-11:00 |
| 58 | + [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467920505, true], // 2016-07-07T08:41:45-11:00 |
| 59 | + [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467949305, true], // 2016-07-07T16:41:45-11:00 |
| 60 | + [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467952905, false], // 2016-07-07T17:41:45-11:00 |
| 61 | + [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467916905, true], // 2016-07-07T07:41:45-11:00 |
| 62 | + [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467920505, false], // 2016-07-07T08:41:45-11:00 |
| 63 | + [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467949305, false], // 2016-07-07T16:41:45-11:00 |
| 64 | + [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467952905, true], // 2016-07-07T17:41:45-11:00 |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider dataExecuteCheck |
| 70 | + * @param string $value |
| 71 | + * @param int $timestamp |
| 72 | + * @param bool $expected |
| 73 | + */ |
| 74 | + public function testExecuteCheckIn($value, $timestamp, $expected) { |
| 75 | + $check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory); |
| 76 | + |
| 77 | + $this->timeFactory->expects($this->once()) |
| 78 | + ->method('getTime') |
| 79 | + ->willReturn($timestamp); |
| 80 | + |
| 81 | + $this->assertEquals($expected, $check->executeCheck('in', $value)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dataProvider dataExecuteCheck |
| 86 | + * @param string $value |
| 87 | + * @param int $timestamp |
| 88 | + * @param bool $expected |
| 89 | + */ |
| 90 | + public function testExecuteCheckNotIn($value, $timestamp, $expected) { |
| 91 | + $check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory); |
| 92 | + |
| 93 | + $this->timeFactory->expects($this->once()) |
| 94 | + ->method('getTime') |
| 95 | + ->willReturn($timestamp); |
| 96 | + |
| 97 | + $this->assertEquals(!$expected, $check->executeCheck('!in', $value)); |
| 98 | + } |
| 99 | + |
| 100 | + public function dataValidateCheck() { |
| 101 | + return [ |
| 102 | + ['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin'])], |
| 103 | + ['!in', json_encode(['08:00 Europe/Berlin', '17:00 America/North_Dakota/Beulah'])], |
| 104 | + ['in', json_encode(['08:00 America/Port-au-Prince', '17:00 America/Argentina/San_Luis'])], |
| 105 | + ]; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @dataProvider dataValidateCheck |
| 110 | + * @param string $operator |
| 111 | + * @param string $value |
| 112 | + */ |
| 113 | + public function testValidateCheck($operator, $value) { |
| 114 | + $check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory); |
| 115 | + $check->validateCheck($operator, $value); |
| 116 | + } |
| 117 | + |
| 118 | + public function dataValidateCheckInvalid() { |
| 119 | + return [ |
| 120 | + ['!!in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1, 'Invalid operator'], |
| 121 | + ['in', json_encode(['28:00 Europe/Berlin', '17:00 Europe/Berlin']), 2, 'Invalid time limits'], |
| 122 | + ['in', json_encode(['08:00 Europa/Berlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'], |
| 123 | + ['in', json_encode(['08:00 Europe/Berlin', '17:00 Europa/Berlin']), 3, 'Invalid timezone2'], |
| 124 | + ['in', json_encode(['08:00 Europe/Bearlin', '17:00 Europe/Berlin']), 3, 'Invalid timezone1'], |
| 125 | + ['in', json_encode(['08:00 Europe/Berlin', '17:00 Europe/Bearlin']), 3, 'Invalid timezone2'], |
| 126 | + ]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @dataProvider dataValidateCheckInvalid |
| 131 | + * @param string $operator |
| 132 | + * @param string $value |
| 133 | + * @param int $exceptionCode |
| 134 | + * @param string $exceptionMessage |
| 135 | + */ |
| 136 | + public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) { |
| 137 | + $check = new \OCA\WorkflowEngine\Check\RequestTime($this->timeFactory); |
| 138 | + |
| 139 | + try { |
| 140 | + $check->validateCheck($operator, $value); |
| 141 | + } catch (\UnexpectedValueException $e) { |
| 142 | + $this->assertEquals($exceptionCode, $e->getCode()); |
| 143 | + $this->assertEquals($exceptionMessage, $e->getMessage()); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments