|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Cookbook; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use OCA\Cookbook\Service\JsonService; |
| 7 | + |
| 8 | +class JsonServiceTest extends TestCase{ |
| 9 | + |
| 10 | + /** |
| 11 | + * @var JsonService |
| 12 | + */ |
| 13 | + private $service; |
| 14 | + |
| 15 | + public function setUp(): void{ |
| 16 | + $this->service = new JsonService(); |
| 17 | + } |
| 18 | + |
| 19 | + public function testIsSchemaObject(){ |
| 20 | + // Objects must be encoded as arrays in JSON |
| 21 | + $testData = "notAnArray"; |
| 22 | + $result = $this->service->isSchemaObject($testData); |
| 23 | + self::assertFalse($result, 'The object must be an array'); |
| 24 | + |
| 25 | + // Objects must have a property @type |
| 26 | + $testData = [ |
| 27 | + "@context" => "https://schema.org/", |
| 28 | + "name" => "Schema.org Ontology", |
| 29 | + "subjectOf" => [ |
| 30 | + "@type" => "Book", |
| 31 | + "name" => "The Complete History of Schema.org" |
| 32 | + ] |
| 33 | + ]; |
| 34 | + $result = $this->service->isSchemaObject($testData); |
| 35 | + self::assertFalse($result, 'The object must have the property @type'); |
| 36 | + |
| 37 | + // No typecheck will be requested |
| 38 | + $testData = [ |
| 39 | + "@context" => "https://schema.org/", |
| 40 | + "@type" => "Thing", |
| 41 | + "name" => "Schema.org Ontology", |
| 42 | + "subjectOf" => [ |
| 43 | + "@type" => "Book", |
| 44 | + "name" => "The Complete History of Schema.org" |
| 45 | + ] |
| 46 | + ]; |
| 47 | + $result = $this->service->isSchemaObject($testData); |
| 48 | + self::assertTrue($result); |
| 49 | + $result = $this->service->isSchemaObject($testData, ''); |
| 50 | + self::assertTrue($result); |
| 51 | + |
| 52 | + // Check if type matches |
| 53 | + $testData = [ |
| 54 | + "@context" => "https://schema.org/", |
| 55 | + "@type" => "Thing", |
| 56 | + "name" => "Schema.org Ontology", |
| 57 | + "subjectOf" => [ |
| 58 | + "@type" => "Book", |
| 59 | + "name" => "The Complete History of Schema.org" |
| 60 | + ] |
| 61 | + ]; |
| 62 | + $result = $this->service->isSchemaObject($testData, 'Thing'); |
| 63 | + self::assertTrue($result, 'The type match but it returned false'); |
| 64 | + $result = $this->service->isSchemaObject($testData, 'Foo'); |
| 65 | + self::assertFalse($result, 'The type does not match bat it returned true'); |
| 66 | + } |
| 67 | + |
| 68 | + public function testHasProperty(){ |
| 69 | + // The method isSchemaObject() is tested in another test and assumed as working properly |
| 70 | + $testData = [ |
| 71 | + "@context" => "https://schema.org/", |
| 72 | + "@type" => "Thing", |
| 73 | + "name" => "Schema.org Ontology", |
| 74 | + "subjectOf" => [ |
| 75 | + "@type" => "Book", |
| 76 | + "name" => "The Complete History of Schema.org" |
| 77 | + ] |
| 78 | + ]; |
| 79 | + $result = $this->service->hasProperty($testData, 'name'); |
| 80 | + self::assertTrue($result, 'Property name was not found.'); |
| 81 | + $result = $this->service->hasProperty($testData, 'Bar'); |
| 82 | + self::assertFalse($result, 'Property Bar was falsely found.'); |
| 83 | + |
| 84 | + $result = $this->service->hasProperty(['foo' => 'bar'], 'foo'); |
| 85 | + self::assertFalse($result, 'Property of a non-object must not be returned.'); |
| 86 | + } |
| 87 | +} |
0 commit comments