AbstractStringCheckTest.php 3.6 KB

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