-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from TobiasMie/master
Added test for the JsonService
- Loading branch information
Showing
4 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace OCA\Cookbook; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use OCA\Cookbook\Service\JsonService; | ||
|
||
class JsonServiceTest extends TestCase{ | ||
|
||
/** | ||
* @var JsonService | ||
*/ | ||
private $service; | ||
|
||
public function setUp(): void{ | ||
$this->service = new JsonService(); | ||
} | ||
|
||
public function testIsSchemaObject(){ | ||
// Objects must be encoded as arrays in JSON | ||
$testData = "notAnArray"; | ||
$result = $this->service->isSchemaObject($testData); | ||
self::assertFalse($result, 'The object must be an array'); | ||
|
||
// Objects must have a property @type | ||
$testData = [ | ||
"@context" => "https://schema.org/", | ||
"name" => "Schema.org Ontology", | ||
"subjectOf" => [ | ||
"@type" => "Book", | ||
"name" => "The Complete History of Schema.org" | ||
] | ||
]; | ||
$result = $this->service->isSchemaObject($testData); | ||
self::assertFalse($result, 'The object must have the property @type'); | ||
|
||
// No typecheck will be requested | ||
$testData = [ | ||
"@context" => "https://schema.org/", | ||
"@type" => "Thing", | ||
"name" => "Schema.org Ontology", | ||
"subjectOf" => [ | ||
"@type" => "Book", | ||
"name" => "The Complete History of Schema.org" | ||
] | ||
]; | ||
$result = $this->service->isSchemaObject($testData); | ||
self::assertTrue($result); | ||
$result = $this->service->isSchemaObject($testData, ''); | ||
self::assertTrue($result); | ||
|
||
// Check if type matches | ||
$testData = [ | ||
"@context" => "https://schema.org/", | ||
"@type" => "Thing", | ||
"name" => "Schema.org Ontology", | ||
"subjectOf" => [ | ||
"@type" => "Book", | ||
"name" => "The Complete History of Schema.org" | ||
] | ||
]; | ||
$result = $this->service->isSchemaObject($testData, 'Thing'); | ||
self::assertTrue($result, 'The type match but it returned false'); | ||
$result = $this->service->isSchemaObject($testData, 'Foo'); | ||
self::assertFalse($result, 'The type does not match bat it returned true'); | ||
} | ||
|
||
public function testHasProperty(){ | ||
// The method isSchemaObject() is tested in another test and assumed as working properly | ||
$testData = [ | ||
"@context" => "https://schema.org/", | ||
"@type" => "Thing", | ||
"name" => "Schema.org Ontology", | ||
"subjectOf" => [ | ||
"@type" => "Book", | ||
"name" => "The Complete History of Schema.org" | ||
] | ||
]; | ||
$result = $this->service->hasProperty($testData, 'name'); | ||
self::assertTrue($result, 'Property name was not found.'); | ||
$result = $this->service->hasProperty($testData, 'Bar'); | ||
self::assertFalse($result, 'Property Bar was falsely found.'); | ||
|
||
$result = $this->service->hasProperty(['foo' => 'bar'], 'foo'); | ||
self::assertFalse($result, 'Property of a non-object must not be returned.'); | ||
} | ||
} |