Skip to content

Commit 7cdd135

Browse files
Merge pull request #387 from TobiasMie/master
Added test for the JsonService
2 parents 770611f + 51ac3a8 commit 7cdd135

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

.github/actions/run-tests/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The folder will be populated later during the build. There will be a few 100MB n
3232

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

3838
This process will take some time as a few dependencies need to be compiled into the image. Just wait for it to succeed.

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
[#375](https://github.com./nextcloud/cookbook/pull/375/) @seyfeb
2626
- Service to handle schema.org JSON data in strings easier
2727
[#383](https://github.com./nextcloud/cookbook/pull/383/) @christianlupus
28+
- Unit tests for JSON object service
29+
[#387](https://github.com./nextcloud/cookbook/pull/387) @TobiasMie
2830

2931
### Changed
3032
- Switch of project ownership to neextcloud organization in GitHub

lib/Service/JsonService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace OCA\Cookbook;
3+
namespace OCA\Cookbook\Service;
44

55
/**
66
* A collection of useful functions to handle schema.org JSON data
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)