Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for the JsonService #387

Merged
merged 5 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/run-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The folder will be populated later during the build. There will be a few 100MB n

In order to run the tests, a docker image accoring to your settings needs to be generated. This is done by calling
```
docker-compose built dut
docker-compose build dut
```

This process will take some time as a few dependencies need to be compiled into the image. Just wait for it to succeed.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
[#375](https://github.com/nextcloud/cookbook/pull/375/) @seyfeb
- Service to handle schema.org JSON data in strings easier
[#383](https://github.com/nextcloud/cookbook/pull/383/) @christianlupus
- Unit tests for JSON object service
[#387](https://github.com/nextcloud/cookbook/pull/387) @TobiasMie

### Changed
- Switch of project ownership to neextcloud organization in GitHub
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/JsonService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OCA\Cookbook;
namespace OCA\Cookbook\Service;

/**
* A collection of useful functions to handle schema.org JSON data
Expand Down
87 changes: 87 additions & 0 deletions tests/Unit/Service/JsonServiceTest.php
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.');
}
}