AbstractStringCheckTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Tests\Check;
  7. use OCP\IL10N;
  8. class AbstractStringCheckTest extends \Test\TestCase {
  9. protected function getCheckMock() {
  10. $l = $this->getMockBuilder(IL10N::class)
  11. ->disableOriginalConstructor()
  12. ->getMock();
  13. $l->expects($this->any())
  14. ->method('t')
  15. ->willReturnCallback(function ($string, $args) {
  16. return sprintf($string, $args);
  17. });
  18. $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
  19. ->setConstructorArgs([
  20. $l,
  21. ])
  22. ->setMethods([
  23. 'setPath',
  24. 'executeCheck',
  25. 'getActualValue',
  26. ])
  27. ->getMock();
  28. return $check;
  29. }
  30. public function dataExecuteStringCheck() {
  31. return [
  32. ['is', 'same', 'same', true],
  33. ['is', 'different', 'not the same', false],
  34. ['!is', 'same', 'same', false],
  35. ['!is', 'different', 'not the same', true],
  36. ['matches', '/match/', 'match', true],
  37. ['matches', '/different/', 'not the same', false],
  38. ['!matches', '/match/', 'match', false],
  39. ['!matches', '/different/', 'not the same', true],
  40. ];
  41. }
  42. /**
  43. * @dataProvider dataExecuteStringCheck
  44. * @param string $operation
  45. * @param string $checkValue
  46. * @param string $actualValue
  47. * @param bool $expected
  48. */
  49. public function testExecuteStringCheck($operation, $checkValue, $actualValue, $expected): void {
  50. $check = $this->getCheckMock();
  51. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  52. $this->assertEquals($expected, $this->invokePrivate($check, 'executeStringCheck', [$operation, $checkValue, $actualValue]));
  53. }
  54. public function dataValidateCheck() {
  55. return [
  56. ['is', '/Invalid(Regex/'],
  57. ['!is', '/Invalid(Regex/'],
  58. ['matches', '/Valid(Regex)/'],
  59. ['!matches', '/Valid(Regex)/'],
  60. ];
  61. }
  62. /**
  63. * @dataProvider dataValidateCheck
  64. * @param string $operator
  65. * @param string $value
  66. */
  67. public function testValidateCheck($operator, $value): void {
  68. $check = $this->getCheckMock();
  69. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  70. $check->validateCheck($operator, $value);
  71. $this->addToAssertionCount(1);
  72. }
  73. public function dataValidateCheckInvalid() {
  74. return [
  75. ['!!is', '', 1, 'The given operator is invalid'],
  76. ['less', '', 1, 'The given operator is invalid'],
  77. ['matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  78. ['!matches', '/Invalid(Regex/', 2, 'The given regular expression is invalid'],
  79. ];
  80. }
  81. /**
  82. * @dataProvider dataValidateCheckInvalid
  83. * @param $operator
  84. * @param $value
  85. * @param $exceptionCode
  86. * @param $exceptionMessage
  87. */
  88. public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage): void {
  89. $check = $this->getCheckMock();
  90. try {
  91. /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
  92. $check->validateCheck($operator, $value);
  93. } catch (\UnexpectedValueException $e) {
  94. $this->assertEquals($exceptionCode, $e->getCode());
  95. $this->assertEquals($exceptionMessage, $e->getMessage());
  96. }
  97. }
  98. public function dataMatch() {
  99. return [
  100. ['/valid/', 'valid', [], true],
  101. ['/valid/', 'valid', [md5('/valid/') => [md5('valid') => false]], false], // Cache hit
  102. ];
  103. }
  104. /**
  105. * @dataProvider dataMatch
  106. * @param string $pattern
  107. * @param string $subject
  108. * @param array[] $matches
  109. * @param bool $expected
  110. */
  111. public function testMatch($pattern, $subject, $matches, $expected): void {
  112. $check = $this->getCheckMock();
  113. $this->invokePrivate($check, 'matches', [$matches]);
  114. $this->assertEquals($expected, $this->invokePrivate($check, 'match', [$pattern, $subject]));
  115. }
  116. }