Passed
Push — master ( 2a3d19...5e9e4e )
by Caen
05:36 queued 13s
created

PublicationServiceTest::createPublicationType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Framework\Exceptions\FileNotFoundException;
8
use Hyde\Hyde;
9
use Hyde\Publications\Models\PublicationType;
10
use Hyde\Publications\Pages\PublicationPage;
11
use Hyde\Publications\Publications;
12
use Hyde\Testing\TestCase;
13
use Illuminate\Support\Collection;
14
15
use function file_put_contents;
16
17
/**
18
 * @covers \Hyde\Publications\Publications
19
 * @covers \Hyde\Publications\PublicationsExtension
20
 */
21
class PublicationServiceTest extends TestCase
22
{
23
    protected function setUp(): void
24
    {
25
        parent::setUp();
26
27
        $this->directory('test-publication');
28
    }
29
30
    public function testGetPublicationTypes()
31
    {
32
        $this->assertEquals(new Collection(), Publications::getPublicationTypes());
33
    }
34
35
    public function testGetPublicationTypesWithTypes()
36
    {
37
        $this->createPublicationType();
38
        Hyde::boot();
39
40
        $this->assertEquals(new Collection([
0 ignored issues
show
Bug introduced by
array('test-publication'...et('test-publication')) of type array<string,Hyde\Public...Models\PublicationType> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->assertEquals(new Collection(/** @scrutinizer ignore-type */ [
Loading history...
41
            'test-publication' => PublicationType::get('test-publication'),
42
        ]), Publications::getPublicationTypes());
43
    }
44
45
    public function testGetPublicationTypesMethodReturnsTheSameInstances()
46
    {
47
        $this->createPublicationType();
48
49
        $this->assertSame(Publications::getPublicationTypes(), Publications::getPublicationTypes());
50
    }
51
52
    public function testGetPublicationsForPubType()
53
    {
54
        $this->createPublicationType();
55
56
        $this->assertEquals(
57
            new Collection(),
58
            Publications::getPublicationsForType(PublicationType::get('test-publication'))
59
        );
60
    }
61
62
    public function testGetPublicationsForPubTypeWithPublications()
63
    {
64
        $this->createPublicationType();
65
        $this->createPublication();
66
67
        $this->assertEquals(
68
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...test-publication/foo')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
69
                PublicationPage::parse('test-publication/foo'),
70
            ]),
71
            Publications::getPublicationsForType(PublicationType::get('test-publication'))
72
        );
73
    }
74
75
    public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationPage()
76
    {
77
        $this->createPublicationType();
78
        $this->createPublication();
79
80
        $this->assertContainsOnlyInstancesOf(
81
            PublicationPage::class,
82
            Publications::getPublicationsForType(PublicationType::get('test-publication'))
83
        );
84
    }
85
86
    public function testGetPublicationsForPubTypeSortsPublicationsBySortField()
87
    {
88
        (new PublicationType('test-publication', sortField: 'order'))->save();
89
90
        $this->markdown('test-publication/one.md', matter: ['order' => 1]);
91
        $this->markdown('test-publication/two.md', matter: ['order' => 2]);
92
        $this->markdown('test-publication/three.md', matter: ['order' => 3]);
93
94
        $this->assertEquals(
95
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...st-publication/three')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
96
                PublicationPage::parse('test-publication/one'),
97
                PublicationPage::parse('test-publication/two'),
98
                PublicationPage::parse('test-publication/three'),
99
            ]),
100
            Publications::getPublicationsForType(PublicationType::get('test-publication'))
101
        );
102
    }
103
104
    public function testGetPublicationsForPubTypeSortsPublicationsWithSpecifiedDirection()
105
    {
106
        (new PublicationType('test-publication', sortField: 'order', sortAscending: false))->save();
107
108
        $this->markdown('test-publication/one.md', matter: ['order' => 1]);
109
        $this->markdown('test-publication/two.md', matter: ['order' => 2]);
110
        $this->markdown('test-publication/three.md', matter: ['order' => 3]);
111
112
        $this->assertEquals(
113
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...test-publication/one')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
114
                PublicationPage::parse('test-publication/three'),
115
                PublicationPage::parse('test-publication/two'),
116
                PublicationPage::parse('test-publication/one'),
117
            ]),
118
            Publications::getPublicationsForType(PublicationType::get('test-publication'))
119
        );
120
    }
121
122
    public function testGetPublicationsForPubTypeSortsPublicationsByNewSortField()
123
    {
124
        (new PublicationType('test-publication'))->save();
125
126
        $this->markdown('test-publication/one.md', matter: ['readCount' => 1]);
127
        $this->markdown('test-publication/two.md', matter: ['readCount' => 2]);
128
        $this->markdown('test-publication/three.md', matter: ['readCount' => 3]);
129
130
        $this->assertEquals(
131
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...st-publication/three')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
132
                PublicationPage::parse('test-publication/one'),
133
                PublicationPage::parse('test-publication/two'),
134
                PublicationPage::parse('test-publication/three'),
135
            ]),
136
            Publications::getPublicationsForType(PublicationType::get('test-publication'), 'readCount')
137
        );
138
    }
139
140
    public function testGetPublicationsForPubTypeSortsPublicationsByNewSortFieldDescending()
141
    {
142
        (new PublicationType('test-publication', sortField: 'order'))->save();
143
144
        $this->markdown('test-publication/one.md', matter: ['readCount' => 1]);
145
        $this->markdown('test-publication/two.md', matter: ['readCount' => 2]);
146
        $this->markdown('test-publication/three.md', matter: ['readCount' => 3]);
147
148
        $this->assertEquals(
149
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...test-publication/one')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
150
                PublicationPage::parse('test-publication/three'),
151
                PublicationPage::parse('test-publication/two'),
152
                PublicationPage::parse('test-publication/one'),
153
            ]),
154
            Publications::getPublicationsForType(PublicationType::get('test-publication'), 'readCount', false)
155
        );
156
    }
157
158
    public function testGetPublicationsForPubTypeWithInvalidSortField()
159
    {
160
        (new PublicationType('test-publication', sortField: 'order'))->save();
161
162
        $this->markdown('test-publication/one.md', matter: ['readCount' => 1]);
163
        $this->markdown('test-publication/two.md', matter: ['readCount' => 2]);
164
        $this->markdown('test-publication/three.md', matter: ['readCount' => 3]);
165
166
        $this->assertEquals(
167
            new Collection([
0 ignored issues
show
Bug introduced by
array(Hyde\Publications\...test-publication/two')) of type array<integer,Hyde\Publi...\Pages\PublicationPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
168
                PublicationPage::parse('test-publication/one'),
169
                PublicationPage::parse('test-publication/three'),
170
                PublicationPage::parse('test-publication/two'),
171
            ]),
172
            Publications::getPublicationsForType(PublicationType::get('test-publication'), 'invalid')
173
        );
174
    }
175
176
    public function testGetMediaForPubType()
177
    {
178
        $this->createPublicationType();
179
180
        $this->assertEquals(
181
            new Collection(),
182
            Publications::getMediaForType(PublicationType::get('test-publication'))
183
        );
184
    }
185
186
    public function testGetMediaForPubTypeWithMedia()
187
    {
188
        $this->createPublicationType();
189
        $this->directory('_media/test-publication');
190
        file_put_contents(Hyde::path('_media/test-publication/image.png'), '');
191
192
        $this->assertEquals(
193
            new Collection([
0 ignored issues
show
Bug introduced by
array('test-publication/image.png') of type array<integer,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

193
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
194
                'test-publication/image.png',
195
            ]),
196
            Publications::getMediaForType(PublicationType::get('test-publication'))
197
        );
198
    }
199
200
    public function testGetMediaForPubTypeWithCustomMediaDirectory()
201
    {
202
        Hyde::setMediaDirectory('_assets');
203
        $this->createPublicationType();
204
        $this->directory('_assets/test-publication');
205
        file_put_contents(Hyde::path('_assets/test-publication/image.png'), '');
206
207
        $this->assertEquals(
208
            new Collection([
0 ignored issues
show
Bug introduced by
array('test-publication/image.png') of type array<integer,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

208
            new Collection(/** @scrutinizer ignore-type */ [
Loading history...
209
                'test-publication/image.png',
210
            ]),
211
            Publications::getMediaForType(PublicationType::get('test-publication'))
212
        );
213
    }
214
215
    public function testParsePublicationFile()
216
    {
217
        $this->createPublicationType();
218
        $this->createPublication();
219
220
        $file = PublicationPage::parse('test-publication/foo');
221
        $this->assertInstanceOf(PublicationPage::class, $file);
222
        $this->assertEquals('test-publication/foo', $file->getIdentifier());
223
    }
224
225
    public function testParsePublicationFileWithNonExistentFile()
226
    {
227
        $this->createPublicationType();
228
229
        $this->expectException(FileNotFoundException::class);
230
        $this->expectExceptionMessage('File [test-publication/foo.md] not found.');
231
232
        PublicationPage::parse('test-publication/foo');
233
    }
234
235
    public function testPublicationTypeExists()
236
    {
237
        $this->createPublicationType();
238
239
        $this->assertTrue(Publications::publicationTypeExists('test-publication'));
240
        $this->assertFalse(Publications::publicationTypeExists('foo'));
241
    }
242
243
    public function testCanGetTagsUsedInPublications()
244
    {
245
        $type = new PublicationType('test-publication', fields: [[
246
            'name' => 'tag',
247
            'type' => 'tag',
248
        ]]);
249
250
        $page = new PublicationPage(matter: [
251
            'tag' => ['foo', 'bar'],
252
        ], type: $type);
253
254
        Hyde::kernel()->pages()->addPage($page);
255
256
        $this->assertSame(['foo', 'bar'], Publications::getPublicationTags());
257
    }
258
259
    public function testMultipleOccurringTagsAreAggregatedUniquely()
260
    {
261
        $type = new PublicationType('test-publication', fields: [[
262
            'name' => 'tag',
263
            'type' => 'tag',
264
        ]]);
265
266
        Hyde::kernel()->pages()->addPage(new PublicationPage('1', [
267
            'tag' => ['foo', 'bar'],
268
        ], type: $type));
269
270
        Hyde::kernel()->pages()->addPage(new PublicationPage('2', [
271
            'tag' => ['foo', 'baz'],
272
        ], type: $type));
273
274
        $this->assertSame(['foo', 'bar', 'baz'], Publications::getPublicationTags());
275
    }
276
277
    public function testAllTagsMethodFindsBothArrayAndSingleTagValues()
278
    {
279
        $type = new PublicationType('test-publication', fields: [[
280
            'name' => 'tag',
281
            'type' => 'tag',
282
        ]]);
283
284
        Hyde::kernel()->pages()->addPage(new PublicationPage('1', [
285
            'tag' => 'foo',
286
        ], type: $type));
287
288
        Hyde::kernel()->pages()->addPage(new PublicationPage('2', [
289
            'tag' => ['bar', 'baz'],
290
        ], type: $type));
291
292
        $this->assertSame(['foo', 'bar', 'baz'], Publications::getPublicationTags());
293
    }
294
295
    public function testAllTagsMethodReturnsEmptyArrayWhenThereAreNoTagsUsed()
296
    {
297
        $this->assertSame([], Publications::getPublicationTags());
298
    }
299
300
    protected function createPublicationType(): void
301
    {
302
        (new PublicationType('test-publication'))->save();
303
    }
304
305
    protected function createPublication(): void
306
    {
307
        file_put_contents(
308
            Hyde::path('test-publication/foo.md'),
309
            "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\n---\n\nHello World!\n"
310
        );
311
    }
312
}
313