Skip to content

Commit

Permalink
Merge pull request #387 from TobiasMie/master
Browse files Browse the repository at this point in the history
Added test for the JsonService
  • Loading branch information
christianlupus committed Nov 15, 2020
2 parents 770611f + 51ac3a8 commit 7cdd135
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
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.');
}
}

0 comments on commit 7cdd135

Please sign in to comment.