/
PageTest.php
216 lines (173 loc) · 7.67 KB
/
PageTest.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Astrotomic\Stancy\Tests\Models;
use Astrotomic\Stancy\Contracts\Page as PageContract;
use Astrotomic\Stancy\Models\Page;
use Astrotomic\Stancy\Tests\PageDatas\BlogPostPageData;
use Astrotomic\Stancy\Tests\PageDatas\FeedablePageData;
use Astrotomic\Stancy\Tests\PageDatas\HomePageData;
use Astrotomic\Stancy\Tests\TestCase;
use Exception;
use Illuminate\Contracts\View\View;
use Illuminate\Support\HtmlString;
use RuntimeException;
use Spatie\DataTransferObject\DataTransferObjectError;
use Spatie\Feed\FeedItem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
final class PageTest extends TestCase
{
/** @test */
public function it_can_resolve_instance(): void
{
$page = $this->app->make(PageContract::class);
static::assertInstanceOf(Page::class, $page);
}
/** @test */
public function it_can_render_from_sheet(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'home')->view('pages.home');
static::assertInstanceOf(Page::class, $page);
static::assertInstanceOf(View::class, $page->render());
static::assertEquals('<h1>hello world</h1>', trim($page->toHtml()));
static::assertInstanceOf(Response::class, $page->toResponse($this->getRequest()));
}
/** @test */
public function it_can_render_from_sheet_with_yaml_front_matter_predefined_variables(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'yamlFrontMatterPredefined');
static::assertInstanceOf(Page::class, $page);
static::assertInstanceOf(View::class, $page->render());
static::assertEquals('<h1>hello world</h1>', trim($page->toHtml()));
static::assertInstanceOf(Response::class, $page->toResponse($this->getRequest()));
}
/** @test */
public function it_can_render_from_sheet_with_validated_data(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'home')->view('pages.home')->page(HomePageData::class);
static::assertInstanceOf(Page::class, $page);
static::assertInstanceOf(View::class, $page->render());
static::assertEquals('<h1>hello world</h1>', trim($page->toHtml()));
static::assertInstanceOf(Response::class, $page->toResponse($this->getRequest()));
}
/** @test */
public function it_can_return_data_as_array(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'home')->page(HomePageData::class);
static::assertEquals([
'contents' => new HtmlString("<h1>hello world</h1>\n"),
'slug' => 'home',
], $page->toArray());
}
/** @test */
public function it_can_return_data_as_json(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'home')->page(HomePageData::class);
static::assertEquals('{"contents":"<h1>hello world<\/h1>\n","slug":"home"}', $page->toJson());
}
/** @test */
public function it_responses_data_as_json_if_wanted(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'home')->page(HomePageData::class);
$response = $page->toResponse($this->getRequest('/', ['Accept' => 'application/json']));
static::assertInstanceOf(JsonResponse::class, $response);
static::assertEquals('{"contents":"<h1>hello world<\/h1>\n","slug":"home"}', $response->getContent());
}
/** @test */
public function it_can_load_child_sheets(): void
{
$page = $this->getPageFactory()->makeFromSheetName('content', 'blog');
static::assertEquals([
'contents' => new HtmlString(''),
'slug' => 'blog',
'home' => HomePageData::make([
'contents' => new HtmlString("<h1>hello world</h1>\n"),
'slug' => 'home',
]),
'posts' => [
BlogPostPageData::make([
'contents' => new HtmlString("<h1>first post</h1>\n"),
'slug' => 'first-post',
]),
BlogPostPageData::make([
'contents' => new HtmlString("<h1>second post</h1>\n"),
'slug' => 'second-post',
]),
],
], $page->toArray());
}
/** @test */
public function it_throws_exception_if_page_data_class_does_not_exist(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage('The page data class [Strange\Unexisting\Class] has to extend Astrotomic\Stancy\Models\PageData.');
$this->getPageFactory()->make([], 'Strange\Unexisting\Class');
}
/** @test */
public function it_throws_exception_if_page_data_class_does_not_extend_page_data(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage('The page data class [Astrotomic\Stancy\Models\Page] has to extend Astrotomic\Stancy\Models\PageData.');
$this->getPageFactory()->make([], Page::class);
}
/** @test */
public function it_throws_exception_if_rendered_without_view(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage('You have to define a view before the page can render.');
$page = $this->getPageFactory()->make();
static::assertInstanceOf(Page::class, $page);
$page->render();
}
/** @test */
public function it_throws_exception_if_data_is_not_valid(): void
{
static::expectException(DataTransferObjectError::class);
$this->getPageFactory()->make()->page(HomePageData::class);
}
/** @test */
public function it_throws_exception_if_data_is_not_valid_with_yaml_front_matter_predefined_variables(): void
{
static::expectException(DataTransferObjectError::class);
$this->getPageFactory()->makeFromSheetName('content', 'yamlFrontMatterPredefinedInvalid');
}
/** @test */
public function it_throws_exception_on_feed_item_transformation_if_data_is_not_page_data_class(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage('The page data has to extend Astrotomic\Stancy\Models\PageData to allow transformation to Spatie\Feed\FeedItem.');
$this->getPageFactory()->make()->toFeedItem();
}
/** @test */
public function it_throws_exception_on_sitemap_item_transformation_if_data_is_not_page_data_class(): void
{
static::expectException(Exception::class);
static::expectExceptionMessage('The page data has to extend Astrotomic\Stancy\Models\PageData to allow transformation to Spatie\Sitemap\Tags\Tag.');
$this->getPageFactory()->make()->toSitemapItem();
}
/** @test */
public function it_throws_exception_if_sheets_array_is_not_associative(): void
{
static::expectException(RuntimeException::class);
static::expectExceptionMessage('The [_sheets] data has to be an associative array.');
$this->getPageFactory()->makeFromSheetName('content', 'yamlFrontMatterChildSheetsInvalid');
}
/** @test */
public function it_returns_feed_item_if_page_data_supports_it(): void
{
$feedItem = $this->getPageFactory()->make()->page(FeedablePageData::class)->toFeedItem();
static::assertInstanceOf(FeedItem::class, $feedItem);
$feedItem->validate();
}
/** @test */
public function it_is_macroable()
{
$called = false;
Page::macro('testMacro', function () use (&$called) {
$called = true;
return $called;
});
static::assertTrue(Page::hasMacro('testMacro'));
static::assertTrue(Page::testMacro());
static::assertTrue($called);
}
}