|
3 | 3 | use HiFolks\DataType\Block; |
4 | 4 | use HiFolks\DataType\Enums\Operator; |
5 | 5 |
|
6 | | -test('Query Block', function (): void { |
| 6 | +test("Query Block", function (): void { |
7 | 7 | $jsonString = file_get_contents("./tests/data/story.json"); |
8 | 8 |
|
9 | 9 | $composerContent = Block::fromJsonString($jsonString); |
10 | | - $banners = $composerContent->getBlock("story.content.body")->where( |
11 | | - "component", |
12 | | - "==", |
13 | | - "banner", |
14 | | - ); |
| 10 | + $banners = $composerContent |
| 11 | + ->getBlock("story.content.body") |
| 12 | + ->where("component", "==", "banner"); |
15 | 13 | expect($banners)->toHaveCount(2); |
16 | 14 | expect($banners->get("0.headline"))->toBe("New banner"); |
17 | | - expect($banners->get("4.headline"))->toBe("Top Five Discoveries, Curiosity Rover at Mars"); |
| 15 | + expect($banners->get("4.headline"))->toBe( |
| 16 | + "Top Five Discoveries, Curiosity Rover at Mars", |
| 17 | + ); |
18 | 18 |
|
19 | 19 | $composerContent = Block::fromJsonString($jsonString); |
20 | | - $banners = $composerContent->getBlock("story.content.body")->where( |
21 | | - "component", |
22 | | - Operator::NOT_EQUAL, |
23 | | - "banner", |
24 | | - false, |
25 | | - ); |
| 20 | + $banners = $composerContent |
| 21 | + ->getBlock("story.content.body") |
| 22 | + ->where("component", Operator::NOT_EQUAL, "banner", false); |
26 | 23 | expect($banners)->toHaveCount(8); |
27 | 24 | expect($banners->get("0.component"))->toBe("hero-section"); |
28 | 25 | expect($banners->get("4.component"))->toBe("grid-section"); |
29 | | - |
30 | 26 | }); |
31 | 27 |
|
32 | | -test('Query and select Block', function (): void { |
| 28 | +test("Query and select Block", function (): void { |
33 | 29 | $jsonString = file_get_contents("./tests/data/story.json"); |
34 | 30 |
|
35 | 31 | $composerContent = Block::fromJsonString($jsonString); |
36 | | - $banners = $composerContent->getBlock("story.content.body")->where( |
37 | | - "component", |
38 | | - Operator::EQUAL, |
39 | | - "banner", |
40 | | - )->select("headline"); |
| 32 | + $banners = $composerContent |
| 33 | + ->getBlock("story.content.body") |
| 34 | + ->where("component", Operator::EQUAL, "banner") |
| 35 | + ->select("headline"); |
41 | 36 | expect($banners)->toHaveCount(2); |
42 | 37 | expect($banners->get("0"))->toHaveCount(1); |
43 | 38 | expect($banners->get("0.headline"))->toBe("New banner"); |
44 | 39 | expect($banners->get("1"))->toHaveCount(1); |
45 | | - expect($banners->get("1.headline"))->toBe("Top Five Discoveries, Curiosity Rover at Mars"); |
| 40 | + expect($banners->get("1.headline"))->toBe( |
| 41 | + "Top Five Discoveries, Curiosity Rover at Mars", |
| 42 | + ); |
46 | 43 |
|
47 | 44 | $composerContent = Block::fromJsonString($jsonString); |
48 | | - $banners = $composerContent->getBlock("story.content.body")->where( |
49 | | - "component", |
50 | | - Operator::NOT_EQUAL, |
51 | | - "banner", |
52 | | - false, |
53 | | - ); |
| 45 | + $banners = $composerContent |
| 46 | + ->getBlock("story.content.body") |
| 47 | + ->where("component", Operator::NOT_EQUAL, "banner", false); |
54 | 48 | expect($banners)->toHaveCount(8); |
55 | 49 | expect($banners->get("0.component"))->toBe("hero-section"); |
56 | 50 | expect($banners->get("4.component"))->toBe("grid-section"); |
57 | 51 | }); |
58 | 52 |
|
59 | | -test('Order Block', function (): void { |
| 53 | +test("Order Block", function (): void { |
60 | 54 | $jsonString = file_get_contents("./tests/data/story.json"); |
61 | 55 |
|
62 | 56 | $composerContent = Block::fromJsonString($jsonString); |
63 | | - $bodyComponents = $composerContent->getBlock("story.content.body")->orderBy( |
64 | | - "component", |
65 | | - "asc", |
66 | | - ); |
| 57 | + $bodyComponents = $composerContent |
| 58 | + ->getBlock("story.content.body") |
| 59 | + ->orderBy("component", "asc"); |
67 | 60 | expect($bodyComponents)->toHaveCount(10); |
68 | 61 | expect($bodyComponents->get("0.component"))->toBe("banner"); |
69 | 62 | expect($bodyComponents->get("9.component"))->toBe("text-section"); |
70 | 63 |
|
71 | | - $bodyComponents = $composerContent->getBlock("story.content.body")->orderBy( |
72 | | - "component", |
73 | | - "desc", |
74 | | - ); |
| 64 | + $bodyComponents = $composerContent |
| 65 | + ->getBlock("story.content.body") |
| 66 | + ->orderBy("component", "desc"); |
75 | 67 | expect($bodyComponents)->toHaveCount(10); |
76 | 68 | expect($bodyComponents->get("9.component"))->toBe("banner"); |
77 | 69 | expect($bodyComponents->get("0.component"))->toBe("text-section"); |
78 | | - |
79 | 70 | }); |
80 | 71 |
|
81 | | - |
82 | | -it('local dummyjson post', function (): void { |
83 | | - |
| 72 | +it("local dummyjson post", function (): void { |
84 | 73 | $response = Block::fromJsonFile("./tests/data/dummy-posts-30.json"); |
85 | 74 | expect($response)->toBeInstanceOf(Block::class); |
86 | 75 | expect($response)->toHaveCount(4); |
|
115 | 104 | expect($posts->get("0.reactions.likes"))->toBe(192); |
116 | 105 | }); |
117 | 106 |
|
118 | | -test('Query Block with has', function (): void { |
| 107 | +test("Query Block with has", function (): void { |
119 | 108 | $jsonString = file_get_contents("./tests/data/story.json"); |
120 | 109 | $composerContent = Block::fromJsonString($jsonString); |
121 | | - $has = $composerContent->getBlock("story.content.body")->where( |
122 | | - "component", |
123 | | - Operator::EQUAL, |
124 | | - "banner", |
125 | | - )->exists(); |
| 110 | + $has = $composerContent |
| 111 | + ->getBlock("story.content.body") |
| 112 | + ->where("component", Operator::EQUAL, "banner") |
| 113 | + ->exists(); |
126 | 114 | expect($has)->toBeTrue(); |
127 | | - $has = $composerContent->getBlock("story.content.body")->where( |
128 | | - "component", |
129 | | - Operator::NOT_EQUAL, |
130 | | - "banner", |
131 | | - )->exists(); |
| 115 | + $has = $composerContent |
| 116 | + ->getBlock("story.content.body") |
| 117 | + ->where("component", Operator::NOT_EQUAL, "banner") |
| 118 | + ->exists(); |
132 | 119 | expect($has)->toBeTrue(); |
133 | | - $has = $composerContent->getBlock("story.content.body")->where( |
134 | | - "component", |
135 | | - Operator::EQUAL, |
136 | | - "bannerXXX", |
137 | | - )->exists(); |
| 120 | + $has = $composerContent |
| 121 | + ->getBlock("story.content.body") |
| 122 | + ->where("component", Operator::EQUAL, "bannerXXX") |
| 123 | + ->exists(); |
138 | 124 | expect($has)->toBeFalse(); |
139 | | - $has = $composerContent->getBlock("story.content.body")->where( |
140 | | - "component", |
141 | | - "banner", |
142 | | - )->exists(); |
| 125 | + $has = $composerContent |
| 126 | + ->getBlock("story.content.body") |
| 127 | + ->where("component", "banner") |
| 128 | + ->exists(); |
143 | 129 | expect($has)->toBeTrue(); |
144 | 130 | }); |
| 131 | + |
| 132 | +test("Query Block extractWhere", function (): void { |
| 133 | + $jsonString = file_get_contents("./tests/data/story.json"); |
| 134 | + |
| 135 | + $story = Block::fromJsonString($jsonString); |
| 136 | + $assets = $story->extractWhere("fieldtype", "asset"); |
| 137 | + expect($assets)->toHaveCount(16); |
| 138 | + expect($assets->get("3.filename"))->toStartWith("https://a.story"); |
| 139 | +}); |
0 commit comments