Open Data Catalog v2.0.0
ProperJsonValidatorTest.php
Go to the documentation of this file.
1 <?php
2 
4 use Symfony\Component\Validator\Constraints\Count;
5 use Symfony\Component\Validator\Context\ExecutionContext;
6 use PHPUnit\Framework\TestCase;
7 
11 class ProperJsonValidatorTest extends TestCase {
12 
16  public function testValidationSuccess() {
17  $validator = $this->getMockBuilder(ProperJsonValidator::class)
18  ->setMethods(["isProper"])
19  ->getMock();
20 
21  $validator->expects($this->once())->method("isProper")->willReturn(['valid' => TRUE]);
22 
23  $context = $this->getMockBuilder(ExecutionContext::class)
24  ->setMethods(["addViolation"])
25  ->disableOriginalConstructor()
26  ->getMock();
27 
28  $context->expects($this->never())->method("addViolation");
29 
30  $validator->initialize($context);
31 
32  $validator->validate([(object) ['value' => "{}"]], new Count(['min' => 1, 'max' => 2]));
33  }
34 
38  public function testValidationFailure() {
39  $validator = $this->getMockBuilder(ProperJsonValidator::class)
40  ->setMethods(["isProper"])
41  ->getMock();
42 
43  $validator->expects($this->once())
44  ->method("isProper")
45  ->willReturn(
46  [
47  'valid' => FALSE,
48  'errors' => [
49  ['message' => "yep"],
50  ],
51  ]
52  );
53 
54  $context = $this->getMockBuilder(ExecutionContext::class)
55  ->setMethods(["addViolation"])
56  ->disableOriginalConstructor()
57  ->getMock();
58 
59  $context->expects($this->once())->method("addViolation");
60 
61  $validator->initialize($context);
62 
63  $validator->validate([(object) ['value' => "{}"]], new Count(['min' => 1, 'max' => 2]));
64  }
65 
66 }
ProperJsonValidatorTest\testValidationSuccess
testValidationSuccess()
Definition: ProperJsonValidatorTest.php:16
ProperJsonValidatorTest
Definition: ProperJsonValidatorTest.php:11
ProperJsonValidatorTest\testValidationFailure
testValidationFailure()
Definition: ProperJsonValidatorTest.php:38
Drupal\dkan_data\Plugin\Validation\Constraint\ProperJsonValidator
Definition: ProperJsonValidator.php:11