-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDataCollectionTest.php
209 lines (174 loc) · 7.44 KB
/
DataCollectionTest.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
<?php
namespace Hyde\Framework\Testing\Feature;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\MarkdownDocument;
use Hyde\Framework\Modules\DataCollections\DataCollection;
use Hyde\Framework\Modules\DataCollections\DataCollectionServiceProvider;
use Hyde\Framework\Modules\DataCollections\Facades\MarkdownCollection;
use Hyde\Testing\TestCase;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
/**
* @covers \Hyde\Framework\Modules\DataCollections\DataCollection
* @covers \Hyde\Framework\Modules\DataCollections\DataCollectionServiceProvider
* @covers \Hyde\Framework\Modules\DataCollections\Facades\MarkdownCollection
*/
class DataCollectionTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['hyde.features' => [Features::dataCollections()]]);
(new DataCollectionServiceProvider($this->app))->boot();
}
public function test_constructor_creates_new_data_collection_instance()
{
$class = new DataCollection('foo');
$this->assertInstanceOf(DataCollection::class, $class);
$this->assertInstanceOf(Collection::class, $class);
}
public function test_constructor_sets_key()
{
$class = new DataCollection('foo');
$this->assertEquals('foo', $class->key);
}
public function test_key_is_required()
{
$this->expectException(\ArgumentCountError::class);
new DataCollection();
}
public function test_get_collection_method_returns_the_collection_instance()
{
$class = new DataCollection('foo');
$this->assertSame($class, $class->getCollection());
}
public function test_get_collection_method_sets_parse_time_in_ms()
{
$class = new DataCollection('foo');
$class->getCollection();
$this->assertIsFloat($class->parseTimeInMs);
}
public function test_get_markdown_files_method_returns_empty_array_if_the_specified_directory_does_not_exist()
{
$class = new DataCollection('foo');
$this->assertIsArray($class->getMarkdownFiles());
$this->assertEmpty($class->getMarkdownFiles());
}
public function test_get_markdown_files_method_returns_empty_array_if_no_files_are_found_in_specified_directory()
{
mkdir(Hyde::path('_data/foo'));
$class = new DataCollection('foo');
$this->assertIsArray($class->getMarkdownFiles());
$this->assertEmpty($class->getMarkdownFiles());
rmdir(Hyde::path('_data/foo'));
}
public function test_get_markdown_files_method_returns_an_array_of_markdown_files_in_the_specified_directory()
{
mkdir(Hyde::path('_data/foo'));
Hyde::touch(('_data/foo/foo.md'));
Hyde::touch(('_data/foo/bar.md'));
$this->assertEquals([
Hyde::path('_data/foo/bar.md'),
Hyde::path('_data/foo/foo.md'),
], (new DataCollection('foo'))->getMarkdownFiles());
File::deleteDirectory(Hyde::path('_data/foo'));
}
public function test_get_markdown_files_method_does_not_include_files_in_subdirectories()
{
mkdir(Hyde::path('_data/foo'));
mkdir(Hyde::path('_data/foo/bar'));
Hyde::touch(('_data/foo/foo.md'));
Hyde::touch(('_data/foo/bar/bar.md'));
$this->assertEquals([
Hyde::path('_data/foo/foo.md'),
], (new DataCollection('foo'))->getMarkdownFiles());
File::deleteDirectory(Hyde::path('_data/foo'));
}
public function test_get_markdown_files_method_does_not_include_files_with_extensions_other_than_md()
{
mkdir(Hyde::path('_data/foo'));
Hyde::touch(('_data/foo/foo.md'));
Hyde::touch(('_data/foo/bar.txt'));
$this->assertEquals([
Hyde::path('_data/foo/foo.md'),
], (new DataCollection('foo'))->getMarkdownFiles());
File::deleteDirectory(Hyde::path('_data/foo'));
}
// test get markdown files method does not remove files starting with an underscore
public function test_get_markdown_files_method_does_not_remove_files_starting_with_an_underscore()
{
mkdir(Hyde::path('_data/foo'));
Hyde::touch(('_data/foo/_foo.md'));
$this->assertEquals([
Hyde::path('_data/foo/_foo.md'),
], (new DataCollection('foo'))->getMarkdownFiles());
File::deleteDirectory(Hyde::path('_data/foo'));
}
public function test_static_markdown_helper_returns_new_data_collection_instance()
{
$this->assertInstanceOf(DataCollection::class, DataCollection::markdown('foo'));
}
public function test_static_markdown_helper_discovers_and_parses_markdown_files_in_the_specified_directory()
{
mkdir(Hyde::path('_data/foo'));
Hyde::touch(('_data/foo/foo.md'));
Hyde::touch(('_data/foo/bar.md'));
$collection = DataCollection::markdown('foo');
$this->assertContainsOnlyInstancesOf(MarkdownDocument::class, $collection);
File::deleteDirectory(Hyde::path('_data/foo'));
}
public function test_static_markdown_helper_doest_not_ignore_files_starting_with_an_underscore()
{
mkdir(Hyde::path('_data/foo'));
Hyde::touch(('_data/foo/foo.md'));
Hyde::touch(('_data/foo/_bar.md'));
$this->assertCount(2, DataCollection::markdown('foo'));
File::deleteDirectory(Hyde::path('_data/foo'));
}
public function test_markdown_facade_returns_same_result_as_static_markdown_helper()
{
$expected = DataCollection::markdown('foo');
$actual = MarkdownCollection::get('foo');
unset($expected->parseTimeInMs);
unset($actual->parseTimeInMs);
$this->assertEquals($expected, $actual);
}
public function test_data_collection_service_provider_registers_the_facade_as_an_alias()
{
$this->assertArrayHasKey('MarkdownCollection', AliasLoader::getInstance()->getAliases());
$this->assertContains(MarkdownCollection::class, AliasLoader::getInstance()->getAliases());
}
public function test_data_collection_service_provider_creates_the__data_directory_if_it_does_not_exist_and_feature_is_enabled()
{
config(['hyde.features' => [Features::dataCollections()]]);
File::deleteDirectory(Hyde::path('_data'));
$this->assertFileDoesNotExist(Hyde::path('_data'));
(new DataCollectionServiceProvider($this->app))->boot();
$this->assertFileExists(Hyde::path('_data'));
}
public function test_data_collection_service_provider_does_not_create_the__data_directory_feature_is_disabled()
{
File::deleteDirectory(Hyde::path('_data'));
$this->assertFileDoesNotExist(Hyde::path('_data'));
config(['hyde.features' => []]);
$this->app['config']->set('hyde.data_collection.enabled', false);
(new DataCollectionServiceProvider($this->app))->boot();
$this->assertFileDoesNotExist(Hyde::path('_data'));
}
public function test_class_has_static_source_directory_property()
{
$this->assertEquals('_data', DataCollection::$sourceDirectory);
}
public function test_source_directory_can_be_changed()
{
DataCollection::$sourceDirectory = 'foo';
mkdir(Hyde::path('foo/bar'), recursive: true);
Hyde::touch(('foo/bar/foo.md'));
$this->assertEquals([
Hyde::path('foo/bar/foo.md'),
], (new DataCollection('bar'))->getMarkdownFiles());
File::deleteDirectory(Hyde::path('foo'));
}
}