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); } }