1
0

AbstractStringCheckTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 AbstractStringCheckTest extends \Test\TestCase {
  23. protected function getCheckMock() {
  24. $l = $this->getMockBuilder('OCP\IL10N')
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $l->expects($this->any())
  28. ->method('t')
  29. ->willReturnCallback(function($string, $args) {
  30. return sprintf($string, $args);
  31. });
  32. $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
  33. ->setConstructorArgs([
  34. $l,
  35. ])
  36. ->setMethods([
  37. 'setPath',
  38. 'executeCheck',
  39. 'getActualValue',
  40. ])
  41. ->getMock();
  42. return $check;
  43. }
  44. public function dataExecuteStringCheck() {
  45. return [
  46. ['is', 'same', 'same', true],
  47. ['is', 'different', 'not the same', false],
  48. ['!is', 'same', 'same', false],
  49. ['!is', 'different', 'not the same', true],
  50. ['matches', '/match/', 'match', true],
  51. ['matches', '/different/', 'not the same', false],
  52. ['!matches', '/match/', 'match', false],
  53. ['!matches', '/different/', 'not the same', true],
  54. ];
  55. }
  56. /**
  57. * @dataProvider dataExecuteStringCheck
  58. * @param string $operation
  59. * @param string $checkValue
  60. * @param string $actualValue
  61. * @param bool $expected
  62. */
  63. public function testExecuteStringCheck($operation, $checkValue, $actualValue, $expected) {
  64. $check = $this->getCheckMock();
  65. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  66. $this->assertEquals($expected, $this->invokePrivate($check, 'executeStringCheck', [$operation, $checkValue, $actualValue]));
  67. }
  68. public function dataValidateCheck() {
  69. return [
  70. ['is', '/Invalid(Regex/'],
  71. ['!is', '/Invalid(Regex/'],
  72. ['matches', '/Valid(Regex)/'],
  73. ['!matches', '/Valid(Regex)/'],
  74. ];
  75. }
  76. /**
  77. * @dataProvider dataValidateCheck
  78. * @param string $operator
  79. * @param string $value
  80. */
  81. public function testValidateCheck($operator, $value) {
  82. $check = $this->getCheckMock();
  83. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  84. $check->validateCheck($operator, $value);
  85. }
  86. public function dataValidateCheckInvalid() {
  87. return [
  88. ['!!is', '', 1, 'The given operator is invalid'],
  89. ['less', '', 1, 'The given operator is invalid'],
  90. ['matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  91. ['!matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  92. ];
  93. }
  94. /**
  95. * @dataProvider dataValidateCheckInvalid
  96. * @param $operator
  97. * @param $value
  98. * @param $exceptionCode
  99. * @param $exceptionMessage
  100. */
  101. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
  102. $check = $this->getCheckMock();
  103. try {
  104. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  105. $check->validateCheck($operator, $value);
  106. } catch (\UnexpectedValueException $e) {
  107. $this->assertEquals($exceptionCode, $e->getCode());
  108. $this->assertEquals($exceptionMessage, $e->getMessage());
  109. }
  110. }
  111. public function dataMatch() {
  112. return [
  113. ['/valid/', 'valid', [], true],
  114. ['/valid/', 'valid', [md5('/valid/') => [md5('valid') => false]], false], // Cache hit
  115. ];
  116. }
  117. /**
  118. * @dataProvider dataMatch
  119. * @param string $pattern
  120. * @param string $subject
  121. * @param array[] $matches
  122. * @param bool $expected
  123. */
  124. public function testMatch($pattern, $subject, $matches, $expected) {
  125. $check = $this->getCheckMock();
  126. $this->invokePrivate($check, 'matches', [$matches]);
  127. $this->assertEquals($expected, $this->invokePrivate($check, 'match', [$pattern, $subject]));
  128. }
  129. }