AbstractStringCheckTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\WorkflowEngine\Tests\Check;
  26. use OCP\IL10N;
  27. class AbstractStringCheckTest extends \Test\TestCase {
  28. protected function getCheckMock() {
  29. $l = $this->getMockBuilder(IL10N::class)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $l->expects($this->any())
  33. ->method('t')
  34. ->willReturnCallback(function ($string, $args) {
  35. return sprintf($string, $args);
  36. });
  37. $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
  38. ->setConstructorArgs([
  39. $l,
  40. ])
  41. ->setMethods([
  42. 'setPath',
  43. 'executeCheck',
  44. 'getActualValue',
  45. ])
  46. ->getMock();
  47. return $check;
  48. }
  49. public function dataExecuteStringCheck() {
  50. return [
  51. ['is', 'same', 'same', true],
  52. ['is', 'different', 'not the same', false],
  53. ['!is', 'same', 'same', false],
  54. ['!is', 'different', 'not the same', true],
  55. ['matches', '/match/', 'match', true],
  56. ['matches', '/different/', 'not the same', false],
  57. ['!matches', '/match/', 'match', false],
  58. ['!matches', '/different/', 'not the same', true],
  59. ];
  60. }
  61. /**
  62. * @dataProvider dataExecuteStringCheck
  63. * @param string $operation
  64. * @param string $checkValue
  65. * @param string $actualValue
  66. * @param bool $expected
  67. */
  68. public function testExecuteStringCheck($operation, $checkValue, $actualValue, $expected) {
  69. $check = $this->getCheckMock();
  70. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  71. $this->assertEquals($expected, $this->invokePrivate($check, 'executeStringCheck', [$operation, $checkValue, $actualValue]));
  72. }
  73. public function dataValidateCheck() {
  74. return [
  75. ['is', '/Invalid(Regex/'],
  76. ['!is', '/Invalid(Regex/'],
  77. ['matches', '/Valid(Regex)/'],
  78. ['!matches', '/Valid(Regex)/'],
  79. ];
  80. }
  81. /**
  82. * @dataProvider dataValidateCheck
  83. * @param string $operator
  84. * @param string $value
  85. */
  86. public function testValidateCheck($operator, $value) {
  87. $check = $this->getCheckMock();
  88. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  89. $check->validateCheck($operator, $value);
  90. $this->addToAssertionCount(1);
  91. }
  92. public function dataValidateCheckInvalid() {
  93. return [
  94. ['!!is', '', 1, 'The given operator is invalid'],
  95. ['less', '', 1, 'The given operator is invalid'],
  96. ['matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  97. ['!matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  98. ];
  99. }
  100. /**
  101. * @dataProvider dataValidateCheckInvalid
  102. * @param $operator
  103. * @param $value
  104. * @param $exceptionCode
  105. * @param $exceptionMessage
  106. */
  107. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
  108. $check = $this->getCheckMock();
  109. try {
  110. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  111. $check->validateCheck($operator, $value);
  112. } catch (\UnexpectedValueException $e) {
  113. $this->assertEquals($exceptionCode, $e->getCode());
  114. $this->assertEquals($exceptionMessage, $e->getMessage());
  115. }
  116. }
  117. public function dataMatch() {
  118. return [
  119. ['/valid/', 'valid', [], true],
  120. ['/valid/', 'valid', [md5('/valid/') => [md5('valid') => false]], false], // Cache hit
  121. ];
  122. }
  123. /**
  124. * @dataProvider dataMatch
  125. * @param string $pattern
  126. * @param string $subject
  127. * @param array[] $matches
  128. * @param bool $expected
  129. */
  130. public function testMatch($pattern, $subject, $matches, $expected) {
  131. $check = $this->getCheckMock();
  132. $this->invokePrivate($check, 'matches', [$matches]);
  133. $this->assertEquals($expected, $this->invokePrivate($check, 'match', [$pattern, $subject]));
  134. }
  135. }