-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueryBlockTest.php
More file actions
139 lines (119 loc) · 5.18 KB
/
QueryBlockTest.php
File metadata and controls
139 lines (119 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
use HiFolks\DataType\Block;
use HiFolks\DataType\Enums\Operator;
test("Query Block", function (): void {
$jsonString = file_get_contents("./tests/data/story.json");
$composerContent = Block::fromJsonString($jsonString);
$banners = $composerContent
->getBlock("story.content.body")
->where("component", "==", "banner");
expect($banners)->toHaveCount(2);
expect($banners->get("0.headline"))->toBe("New banner");
expect($banners->get("4.headline"))->toBe(
"Top Five Discoveries, Curiosity Rover at Mars",
);
$composerContent = Block::fromJsonString($jsonString);
$banners = $composerContent
->getBlock("story.content.body")
->where("component", Operator::NOT_EQUAL, "banner", false);
expect($banners)->toHaveCount(8);
expect($banners->get("0.component"))->toBe("hero-section");
expect($banners->get("4.component"))->toBe("grid-section");
});
test("Query and select Block", function (): void {
$jsonString = file_get_contents("./tests/data/story.json");
$composerContent = Block::fromJsonString($jsonString);
$banners = $composerContent
->getBlock("story.content.body")
->where("component", Operator::EQUAL, "banner")
->select("headline");
expect($banners)->toHaveCount(2);
expect($banners->get("0"))->toHaveCount(1);
expect($banners->get("0.headline"))->toBe("New banner");
expect($banners->get("1"))->toHaveCount(1);
expect($banners->get("1.headline"))->toBe(
"Top Five Discoveries, Curiosity Rover at Mars",
);
$composerContent = Block::fromJsonString($jsonString);
$banners = $composerContent
->getBlock("story.content.body")
->where("component", Operator::NOT_EQUAL, "banner", false);
expect($banners)->toHaveCount(8);
expect($banners->get("0.component"))->toBe("hero-section");
expect($banners->get("4.component"))->toBe("grid-section");
});
test("Order Block", function (): void {
$jsonString = file_get_contents("./tests/data/story.json");
$composerContent = Block::fromJsonString($jsonString);
$bodyComponents = $composerContent
->getBlock("story.content.body")
->orderBy("component", "asc");
expect($bodyComponents)->toHaveCount(10);
expect($bodyComponents->get("0.component"))->toBe("banner");
expect($bodyComponents->get("9.component"))->toBe("text-section");
$bodyComponents = $composerContent
->getBlock("story.content.body")
->orderBy("component", "desc");
expect($bodyComponents)->toHaveCount(10);
expect($bodyComponents->get("9.component"))->toBe("banner");
expect($bodyComponents->get("0.component"))->toBe("text-section");
});
it("local dummyjson post", function (): void {
$response = Block::fromJsonFile("./tests/data/dummy-posts-30.json");
expect($response)->toBeInstanceOf(Block::class);
expect($response)->toHaveCount(4);
$posts = $response->getBlock("posts");
expect($posts)->toBeInstanceOf(Block::class);
expect($posts)->toHaveCount(30);
$lovePosts = $posts->where("tags", Operator::HAS, "love");
expect($lovePosts)->toHaveCount(9);
$mostViewedPosts = $posts->orderBy("views", "desc");
//$mostViewedPosts->dumpJson();
expect($mostViewedPosts)->toHaveCount(30);
expect($mostViewedPosts->get("0.id"))->toBe(2);
expect($mostViewedPosts->get("0.views"))->toBe(4884);
$lessViewedPosts = $posts->orderBy("views"); //by default ascending
//$lessViewedPosts->dumpJson();
expect($lessViewedPosts)->toHaveCount(30);
expect($lessViewedPosts->get("0.id"))->toBe(6);
expect($lessViewedPosts->get("0.views"))->toBe(38);
$mostLikedPosts = $posts->orderBy("reactions.likes", "desc");
//$mostLikedPosts->dumpJson();
expect($mostLikedPosts)->toHaveCount(30);
expect($mostLikedPosts->get("0.id"))->toBe(3);
expect($mostLikedPosts->get("0.reactions.likes"))->toBe(1448);
expect($posts)->toHaveCount(30);
expect($posts->get("0.id"))->toBe(1);
expect($posts->get("0.reactions.likes"))->toBe(192);
});
test("Query Block with has", function (): void {
$jsonString = file_get_contents("./tests/data/story.json");
$composerContent = Block::fromJsonString($jsonString);
$has = $composerContent
->getBlock("story.content.body")
->where("component", Operator::EQUAL, "banner")
->exists();
expect($has)->toBeTrue();
$has = $composerContent
->getBlock("story.content.body")
->where("component", Operator::NOT_EQUAL, "banner")
->exists();
expect($has)->toBeTrue();
$has = $composerContent
->getBlock("story.content.body")
->where("component", Operator::EQUAL, "bannerXXX")
->exists();
expect($has)->toBeFalse();
$has = $composerContent
->getBlock("story.content.body")
->where("component", "banner")
->exists();
expect($has)->toBeTrue();
});
test("Query Block extractWhere", function (): void {
$jsonString = file_get_contents("./tests/data/story.json");
$story = Block::fromJsonString($jsonString);
$assets = $story->extractWhere("fieldtype", "asset");
expect($assets)->toHaveCount(16);
expect($assets->get("3.filename"))->toStartWith("https://a.story");
});