Skip to content

Commit ba01ec0

Browse files
committed
Test coverage for jsonSerialize
1 parent 2276637 commit ba01ec0

File tree

14 files changed

+548
-23
lines changed

14 files changed

+548
-23
lines changed

src/Analytics/AnalyticsData.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use JsonSerializable;
66
use SilverStripe\Core\Injector\Injectable;
77
use SilverStripe\Model\ModelData;
8-
use stdClass;
98

109
class AnalyticsData extends ModelData implements JsonSerializable
1110
{
@@ -83,16 +82,14 @@ public function forTemplate(): string
8382
return http_build_query($query);
8483
}
8584

86-
public function jsonSerialize(): array|stdClass
85+
public function jsonSerialize(): array
8786
{
88-
$data = $this->getDataArray();
89-
90-
if (!$data) {
91-
// Return an empty stdClass, so that json_encode provides an empty object (rather than an empty array)
92-
return new stdClass();
93-
}
94-
95-
return $data;
87+
return [
88+
'indexName' => $this->getIndexName(),
89+
'queryString' => $this->getQueryString(),
90+
'documentId' => $this->getDocumentId(),
91+
'requestId' => $this->getRequestId(),
92+
];
9693
}
9794

9895
private function getDataArray(): array

src/Service/Results/FacetData.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
class FacetData extends ModelData implements JsonSerializable
99
{
1010

11-
private int $count;
11+
private ?int $count = null;
1212

13-
private string|int|float $from;
13+
private string|int|float|null $from = null;
1414

15-
private string|int|float $to;
15+
private string|int|float|null $to = null;
1616

17-
private string $name;
17+
private ?string $name = null;
1818

19-
private string|int|float $value;
19+
private string|int|float|null $value = null;
2020

21-
public function getCount(): int
21+
public function getCount(): ?int
2222
{
2323
return $this->count;
2424
}
@@ -30,7 +30,7 @@ public function setCount(int $count): static
3030
return $this;
3131
}
3232

33-
public function getFrom(): float|int|string
33+
public function getFrom(): float|int|string|null
3434
{
3535
return $this->from;
3636
}
@@ -42,7 +42,7 @@ public function setFrom(float|int|string $from): static
4242
return $this;
4343
}
4444

45-
public function getTo(): float|int|string
45+
public function getTo(): float|int|string|null
4646
{
4747
return $this->to;
4848
}
@@ -54,7 +54,7 @@ public function setTo(float|int|string $to): static
5454
return $this;
5555
}
5656

57-
public function getName(): string
57+
public function getName(): ?string
5858
{
5959
return $this->name;
6060
}
@@ -66,7 +66,7 @@ public function setName(string $name): static
6666
return $this;
6767
}
6868

69-
public function getValue(): string|int|float
69+
public function getValue(): string|int|float|null
7070
{
7171
return $this->value;
7272
}

src/Service/Results/Facets.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public function forTemplate(): string
1818

1919
public function jsonSerialize(): array
2020
{
21-
return $this->toArray();
21+
$facets = [];
22+
23+
foreach ($this->items as $item) {
24+
$facets[] = $item->jsonSerialize();
25+
}
26+
27+
return $facets;
2228
}
2329

2430
}

src/Service/Results/Records.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ public function forTemplate(): string
1919

2020
public function jsonSerialize(): array
2121
{
22-
return $this->toArray();
22+
$records = [];
23+
24+
foreach ($this->toArray() as $record) {
25+
$records[] = $record->jsonSerialize();
26+
}
27+
28+
return $records;
2329
}
2430

2531
}

src/Service/Results/Results.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Results extends Response
1717

1818
private ?string $indexName = null;
1919

20-
public function __construct(private readonly Query $query, int $statusCode)
20+
public function __construct(int $statusCode, private readonly Query $query)
2121
{
2222
parent::__construct($statusCode);
2323

tests/Analytics/AnalyticsDataTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,36 @@ public function testForTemplateEmptyData(): void
3737
$this->assertEquals($data->forTemplate(), '');
3838
}
3939

40+
public function testJsonSerialize(): void
41+
{
42+
$data = AnalyticsData::create();
43+
$data->setIndexName('our-index-main');
44+
$data->setQueryString('search string');
45+
$data->setDocumentId('abc123');
46+
$data->setRequestId('123abc');
47+
48+
$expected = [
49+
'indexName' => 'our-index-main',
50+
'queryString' => 'search string',
51+
'documentId' => 'abc123',
52+
'requestId' => '123abc',
53+
];
54+
55+
$this->assertEqualsCanonicalizing($expected, $data->jsonSerialize());
56+
}
57+
58+
public function testJsonSerializeEmpty(): void
59+
{
60+
$data = AnalyticsData::create();
61+
62+
$expected = [
63+
'indexName' => null,
64+
'queryString' => null,
65+
'documentId' => null,
66+
'requestId' => null,
67+
];
68+
69+
$this->assertEqualsCanonicalizing($expected, $data->jsonSerialize());
70+
}
71+
4072
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace SilverStripe\Discoverer\Tests\Service\Results;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use SilverStripe\Discoverer\Service\Results\FacetData;
7+
8+
class FacetDataTest extends SapphireTest
9+
{
10+
11+
public function testJsonSerialize(): void
12+
{
13+
$facetData = FacetData::create();
14+
$facetData->setCount(1);
15+
$facetData->setFrom(111);
16+
$facetData->setTo(222);
17+
$facetData->setName('test');
18+
$facetData->setValue('testing');
19+
20+
$expected = [
21+
'count' => 1,
22+
'from' => 111,
23+
'to' => 222,
24+
'name' => 'test',
25+
'value' => 'testing',
26+
];
27+
28+
$this->assertEqualsCanonicalizing($expected, $facetData->jsonSerialize());
29+
}
30+
31+
public function testJsonSerializeEmpty(): void
32+
{
33+
$expected = [
34+
'count' => null,
35+
'from' => null,
36+
'to' => null,
37+
'name' => null,
38+
'value' => null,
39+
];
40+
41+
$facetData = FacetData::create();
42+
43+
$this->assertEqualsCanonicalizing($expected, $facetData->jsonSerialize());
44+
}
45+
46+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace SilverStripe\Discoverer\Tests\Service\Results;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use SilverStripe\Discoverer\Service\Results\Facet;
7+
use SilverStripe\Discoverer\Service\Results\FacetData;
8+
9+
class FacetTest extends SapphireTest
10+
{
11+
12+
public function testJsonSerialize(): void
13+
{
14+
$facetDataOne = FacetData::create();
15+
$facetDataOne->setCount(1);
16+
$facetDataOne->setFrom(111);
17+
$facetDataOne->setTo(222);
18+
$facetDataOne->setName('test');
19+
$facetDataOne->setValue('testing');
20+
21+
$facetDataTwo = FacetData::create();
22+
$facetDataTwo->setCount(2);
23+
$facetDataTwo->setFrom(222);
24+
$facetDataTwo->setTo(333);
25+
$facetDataTwo->setName('test2');
26+
$facetDataTwo->setValue('testing2');
27+
28+
$facet = Facet::create();
29+
$facet->setName('test');
30+
$facet->setFieldName('testing');
31+
$facet->setType('testType');
32+
$facet->addData($facetDataOne);
33+
$facet->addData($facetDataTwo);
34+
35+
$expected = [
36+
'type' => 'testType',
37+
'name' => 'test',
38+
'value' => 'testing',
39+
'data' => [
40+
[
41+
'count' => 1,
42+
'from' => 111,
43+
'to' => 222,
44+
'name' => 'test',
45+
'value' => 'testing',
46+
],
47+
[
48+
'count' => 2,
49+
'from' => 222,
50+
'to' => 333,
51+
'name' => 'test2',
52+
'value' => 'testing2',
53+
],
54+
],
55+
];
56+
57+
$this->assertEqualsCanonicalizing($expected, $facet->jsonSerialize());
58+
}
59+
60+
public function testJsonSerializeEmpty(): void
61+
{
62+
$facet = Facet::create();
63+
64+
$expected = [
65+
'data' => [],
66+
'type' => null,
67+
'name' => null,
68+
'value' => null,
69+
];
70+
71+
$this->assertEqualsCanonicalizing($expected, $facet->jsonSerialize());
72+
}
73+
74+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace SilverStripe\Discoverer\Tests\Service\Results;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use SilverStripe\Discoverer\Service\Results\Facet;
7+
use SilverStripe\Discoverer\Service\Results\FacetData;
8+
use SilverStripe\Discoverer\Service\Results\Facets;
9+
10+
class FacetsTest extends SapphireTest
11+
{
12+
13+
public function testJsonSerialize(): void
14+
{
15+
$facetDataOne = FacetData::create();
16+
$facetDataOne->setCount(1);
17+
$facetDataOne->setFrom(111);
18+
$facetDataOne->setTo(222);
19+
$facetDataOne->setName('test');
20+
$facetDataOne->setValue('testing');
21+
22+
$facetOne = Facet::create();
23+
$facetOne->setName('test');
24+
$facetOne->setFieldName('testing');
25+
$facetOne->setType('testType');
26+
$facetOne->addData($facetDataOne);
27+
28+
$facetDataTwo = FacetData::create();
29+
$facetDataTwo->setCount(2);
30+
$facetDataTwo->setFrom(222);
31+
$facetDataTwo->setTo(333);
32+
$facetDataTwo->setName('test2');
33+
$facetDataTwo->setValue('testing2');
34+
35+
$facetTwo = Facet::create();
36+
$facetTwo->setName('test2');
37+
$facetTwo->setFieldName('testing2');
38+
$facetTwo->setType('testType2');
39+
$facetTwo->addData($facetDataTwo);
40+
41+
$facets = Facets::create();
42+
$facets->add($facetOne);
43+
$facets->add($facetTwo);
44+
45+
$expected = [
46+
[
47+
'type' => 'testType',
48+
'name' => 'test',
49+
'value' => 'testing',
50+
'data' => [
51+
[
52+
'count' => 1,
53+
'from' => 111,
54+
'to' => 222,
55+
'name' => 'test',
56+
'value' => 'testing',
57+
],
58+
],
59+
],
60+
[
61+
'type' => 'testType2',
62+
'name' => 'test2',
63+
'value' => 'testing2',
64+
'data' => [
65+
[
66+
'count' => 2,
67+
'from' => 222,
68+
'to' => 333,
69+
'name' => 'test2',
70+
'value' => 'testing2',
71+
],
72+
],
73+
],
74+
];
75+
76+
$this->assertEqualsCanonicalizing($expected, $facets->jsonSerialize());
77+
}
78+
79+
public function testJsonSerializeEmpty(): void
80+
{
81+
$facets = Facets::create();
82+
83+
$this->assertEqualsCanonicalizing([], $facets->jsonSerialize());
84+
}
85+
86+
}

0 commit comments

Comments
 (0)