1
0

AbstractStringCheckTest.php 4.3 KB

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