183 lines
6.9 KiB
PHP
183 lines
6.9 KiB
PHP
<?php
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
namespace CloudObjects\SDK\JSON;
|
|
|
|
use InvalidArgumentException;
|
|
use ML\JsonLD\JsonLD;
|
|
use CloudObjects\SDK\ObjectRetriever;
|
|
|
|
class SchemaValidatorTest extends \PHPUnit\Framework\TestCase {
|
|
|
|
private $schemaValidator;
|
|
private $graph;
|
|
|
|
protected function setUp(): void {
|
|
$this->schemaValidator = new SchemaValidator(new ObjectRetriever);
|
|
$this->graph = JsonLD::getDocument('{}')->getGraph();
|
|
}
|
|
|
|
public function testString() {
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
$this->schemaValidator->validateAgainstNode("Test", $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testNotString() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
$this->schemaValidator->validateAgainstNode(9, $node);
|
|
}
|
|
|
|
public function testNumber() {
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Number'));
|
|
$this->schemaValidator->validateAgainstNode(3.5, $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testNotNumber() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Number'));
|
|
$this->schemaValidator->validateAgainstNode("ABC", $node);
|
|
}
|
|
|
|
public function testInteger() {
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Integer'));
|
|
$this->schemaValidator->validateAgainstNode(12, $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testNotInteger() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Integer'));
|
|
$this->schemaValidator->validateAgainstNode(1.4, $node);
|
|
}
|
|
|
|
public function testArray() {
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Array'));
|
|
$this->schemaValidator->validateAgainstNode([ 1, 2, "foo" ], $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testNotArray() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Array'));
|
|
$this->schemaValidator->validateAgainstNode("NANANA", $node);
|
|
}
|
|
|
|
public function testObject() {
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'a' => 'A',
|
|
'b' => 'B'
|
|
], $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testNotObject() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$this->schemaValidator->validateAgainstNode(5, $node);
|
|
}
|
|
|
|
public function testObjectWithProperty() {
|
|
$stringNode = $this->graph->createNode();
|
|
$stringNode->setProperty('coid://json.co-n.net/hasKey', 'a');
|
|
$stringNode->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$node->setProperty('coid://json.co-n.net/hasProperty', $stringNode);
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'a' => 'A',
|
|
'b' => 'B'
|
|
], $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testObjectWithPropertyTypeError() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$stringNode = $this->graph->createNode();
|
|
$stringNode->setProperty('coid://json.co-n.net/hasKey', 'a');
|
|
$stringNode->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$node->setProperty('coid://json.co-n.net/hasProperty', $stringNode);
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'a' => 0,
|
|
'b' => 'B'
|
|
], $node);
|
|
}
|
|
|
|
public function testObjectWithRequiredProperty() {
|
|
$stringNode = $this->graph->createNode();
|
|
$stringNode->setProperty('coid://json.co-n.net/hasKey', 'a');
|
|
$stringNode->setProperty('coid://json.co-n.net/isRequired', 'true');
|
|
$stringNode->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$node->setProperty('coid://json.co-n.net/hasProperty', $stringNode);
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'a' => 'A',
|
|
'b' => 'B'
|
|
], $node);
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testObjectWithRequiredPropertyTypeError() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$stringNode = $this->graph->createNode();
|
|
$stringNode->setProperty('coid://json.co-n.net/hasKey', 'a');
|
|
$stringNode->setProperty('coid://json.co-n.net/isRequired', 'true');
|
|
$stringNode->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$node->setProperty('coid://json.co-n.net/hasProperty', $stringNode);
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'a' => 0,
|
|
'b' => 'B'
|
|
], $node);
|
|
}
|
|
|
|
public function testObjectWithRequiredPropertyMissing() {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$stringNode = $this->graph->createNode();
|
|
$stringNode->setProperty('coid://json.co-n.net/hasKey', 'a');
|
|
$stringNode->setProperty('coid://json.co-n.net/isRequired', 'true');
|
|
$stringNode->setType($this->graph->createNode('coid://json.co-n.net/String'));
|
|
|
|
$node = $this->graph->createNode();
|
|
$node->setType($this->graph->createNode('coid://json.co-n.net/Object'));
|
|
$node->setProperty('coid://json.co-n.net/hasProperty', $stringNode);
|
|
$this->schemaValidator->validateAgainstNode([
|
|
'b' => 'B',
|
|
'c' => 'C'
|
|
], $node);
|
|
}
|
|
|
|
}
|