RequestTimeTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. namespace OCA\WorkflowEngine\Tests\Check;
  22. class RequestTimeTest extends \Test\TestCase {
  23. /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $timeFactory;
  25. /**
  26. * @return \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected function getL10NMock() {
  29. $l = $this->getMockBuilder('OCP\IL10N')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $l->expects($this->any())
  33. ->method('t')
  34. ->willReturnCallback(function ($string, $args) {
  35. return sprintf($string, $args);
  36. });
  37. return $l;
  38. }
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
  42. ->getMock();
  43. }
  44. public function dataExecuteCheck() {
  45. return [
  46. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467870105, false], // 2016-07-07T07:41:45+02:00
  47. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467873705, true], // 2016-07-07T08:41:45+02:00
  48. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467902505, true], // 2016-07-07T16:41:45+02:00
  49. [json_encode(['08:00 Europe/Berlin', '17:00 Europe/Berlin']), 1467906105, false], // 2016-07-07T17:41:45+02:00
  50. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467870105, true], // 2016-07-07T07:41:45+02:00
  51. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467873705, false], // 2016-07-07T08:41:45+02:00
  52. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467902505, false], // 2016-07-07T16:41:45+02:00
  53. [json_encode(['17:00 Europe/Berlin', '08:00 Europe/Berlin']), 1467906105, true], // 2016-07-07T17:41:45+02:00
  54. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467843105, false], // 2016-07-07T07:41:45+09:30
  55. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467846705, true], // 2016-07-07T08:41:45+09:30
  56. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467875505, true], // 2016-07-07T16:41:45+09:30
  57. [json_encode(['08:00 Australia/Adelaide', '17:00 Australia/Adelaide']), 1467879105, false], // 2016-07-07T17:41:45+09:30
  58. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467843105, true], // 2016-07-07T07:41:45+09:30
  59. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467846705, false], // 2016-07-07T08:41:45+09:30
  60. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467875505, false], // 2016-07-07T16:41:45+09:30
  61. [json_encode(['17:00 Australia/Adelaide', '08:00 Australia/Adelaide']), 1467879105, true], // 2016-07-07T17:41:45+09:30
  62. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467916905, false], // 2016-07-07T07:41:45-11:00
  63. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467920505, true], // 2016-07-07T08:41:45-11:00
  64. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467949305, true], // 2016-07-07T16:41:45-11:00
  65. [json_encode(['08:00 Pacific/Niue', '17:00 Pacific/Niue']), 1467952905, false], // 2016-07-07T17:41:45-11:00
  66. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467916905, true], // 2016-07-07T07:41:45-11:00
  67. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467920505, false], // 2016-07-07T08:41:45-11:00
  68. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467949305, false], // 2016-07-07T16:41:45-11:00
  69. [json_encode(['17:00 Pacific/Niue', '08:00 Pacific/Niue']), 1467952905, true], // 2016-07-07T17:41:45-11:00
  70. ];
  71. }
  72. /**
  73. * @dataProvider dataExecuteCheck
  74. * @param string $value
  75. * @param int $timestamp
  76. * @param bool $expected
  77. */
  78. public function testExecuteCheckIn($value, $timestamp, $expected) {
  79. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  80. $this->timeFactory->expects($this->once())
  81. ->method('getTime')
  82. ->willReturn($timestamp);
  83. $this->assertEquals($expected, $check->executeCheck('in', $value));
  84. }
  85. /**
  86. * @dataProvider dataExecuteCheck
  87. * @param string $value
  88. * @param int $timestamp
  89. * @param bool $expected
  90. */
  91. public function testExecuteCheckNotIn($value, $timestamp, $expected) {
  92. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  93. $this->timeFactory->expects($this->once())
  94. ->method('getTime')
  95. ->willReturn($timestamp);
  96. $this->assertEquals(!$expected, $check->executeCheck('!in', $value));
  97. }
  98. public function dataValidateCheck() {
  99. return [
  100. ['in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]'],
  101. ['!in', '["08:00 Europe/Berlin","17:00 America/North_Dakota/Beulah"]'],
  102. ['in', '["08:00 America/Port-au-Prince","17:00 America/Argentina/San_Luis"]'],
  103. ];
  104. }
  105. /**
  106. * @dataProvider dataValidateCheck
  107. * @param string $operator
  108. * @param string $value
  109. */
  110. public function testValidateCheck($operator, $value) {
  111. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  112. $check->validateCheck($operator, $value);
  113. }
  114. public function dataValidateCheckInvalid() {
  115. return [
  116. ['!!in', '["08:00 Europe/Berlin","17:00 Europe/Berlin"]', 1, 'The given operator is invalid'],
  117. ['in', '["28:00 Europe/Berlin","17:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  118. ['in', '["08:00 Europe/Berlin","27:00 Europe/Berlin"]', 2, 'The given time span is invalid'],
  119. ['in', '["08:00 Europa/Berlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  120. ['in', '["08:00 Europe/Berlin","17:00 Europa/Berlin"]', 4, 'The given end time is invalid'],
  121. ['in', '["08:00 Europe/Bearlin","17:00 Europe/Berlin"]', 3, 'The given start time is invalid'],
  122. ['in', '["08:00 Europe/Berlin","17:00 Europe/Bearlin"]', 4, 'The given end time is invalid'],
  123. ];
  124. }
  125. /**
  126. * @dataProvider dataValidateCheckInvalid
  127. * @param string $operator
  128. * @param string $value
  129. * @param int $exceptionCode
  130. * @param string $exceptionMessage
  131. */
  132. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
  133. $check = new \OCA\WorkflowEngine\Check\RequestTime($this->getL10NMock(), $this->timeFactory);
  134. try {
  135. $check->validateCheck($operator, $value);
  136. } catch (\UnexpectedValueException $e) {
  137. $this->assertEquals($exceptionCode, $e->getCode());
  138. $this->assertEquals($exceptionMessage, $e->getMessage());
  139. }
  140. }
  141. }