Skip to content

Commit 870e134

Browse files
authored
Merge pull request #761 from hydephp/deprecate-testing-helpers-file
Internal: Refactor testing helpers
2 parents 1451bee + af7ef71 commit 870e134

22 files changed

+465
-625
lines changed

packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,25 @@
44

55
namespace Hyde\Framework\Testing\Feature\Actions;
66

7+
use Hyde\Facades\Filesystem;
78
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
89
use Hyde\Framework\Exceptions\FileConflictException;
910
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
1011
use Hyde\Hyde;
1112
use Hyde\Pages\BladePage;
1213
use Hyde\Pages\DocumentationPage;
1314
use Hyde\Testing\TestCase;
14-
use Illuminate\Support\Facades\File;
1515

1616
/**
1717
* @covers \Hyde\Framework\Actions\CreatesNewPageSourceFile
1818
*/
1919
class CreatesNewPageSourceFileTest extends TestCase
2020
{
21-
protected function tearDown(): void
22-
{
23-
if (file_exists(Hyde::path('_pages/test-page.md'))) {
24-
unlink(Hyde::path('_pages/test-page.md'));
25-
}
26-
27-
if (file_exists(Hyde::path('_pages/test-page.blade.php'))) {
28-
unlink(Hyde::path('_pages/test-page.blade.php'));
29-
}
30-
31-
parent::tearDown();
32-
}
33-
3421
public function test_class_can_be_instantiated()
3522
{
3623
$this->assertInstanceOf(
3724
CreatesNewPageSourceFile::class,
38-
new CreatesNewPageSourceFile('Test Page')
25+
(new CreatesNewPageSourceFile('Test Page'))
3926
);
4027
}
4128

@@ -49,113 +36,112 @@ public function test_that_an_exception_is_thrown_for_invalid_page_type()
4936

5037
public function test_that_an_exception_is_thrown_if_file_already_exists_and_overwrite_is_false()
5138
{
52-
$path = Hyde::path('_pages/foo.md');
53-
file_put_contents($path, 'foo');
39+
$this->file('_pages/foo.md', 'foo');
5440

5541
$this->expectException(FileConflictException::class);
56-
$this->expectExceptionMessage("File already exists: $path");
42+
$this->expectExceptionMessage('File already exists: '.Hyde::path('_pages/foo.md'));
5743
$this->expectExceptionCode(409);
5844

59-
new CreatesNewPageSourceFile('foo');
60-
61-
unlink($path);
45+
(new CreatesNewPageSourceFile('foo'));
46+
$this->assertSame('foo', file_get_contents(Hyde::path('_pages/foo.md')));
47+
Filesystem::unlink('_pages/foo.md');
6248
}
6349

6450
public function test_that_can_save_file_returns_true_if_file_already_exists_and_overwrite_is_true()
6551
{
66-
$path = Hyde::path('_pages/foo.md');
67-
file_put_contents($path, 'foo');
68-
69-
new CreatesNewPageSourceFile('foo', force: true);
52+
$this->file('_pages/foo.md', 'foo');
7053

71-
$this->assertTrue(true);
72-
unlink($path);
54+
(new CreatesNewPageSourceFile('foo', force: true));
55+
$this->assertSame("---\ntitle: foo\n---\n\n# foo\n", file_get_contents(Hyde::path('_pages/foo.md')));
56+
Filesystem::unlink('_pages/foo.md');
7357
}
7458

7559
public function test_that_a_markdown_file_can_be_created_and_contains_expected_content()
7660
{
61+
Filesystem::unlink('_pages/test-page.md');
7762
(new CreatesNewPageSourceFile('Test Page'));
7863

79-
$this->assertFileExists(
80-
Hyde::path('_pages/test-page.md')
81-
);
64+
$this->assertFileExists(Hyde::path('_pages/test-page.md'));
8265

83-
$this->assertEquals(
66+
$this->assertSame(
8467
"---\ntitle: Test Page\n---\n\n# Test Page\n",
8568
file_get_contents(Hyde::path('_pages/test-page.md'))
8669
);
70+
Filesystem::unlink('_pages/test-page.md');
8771
}
8872

8973
public function test_that_a_blade_file_can_be_created_and_contains_expected_content()
9074
{
9175
(new CreatesNewPageSourceFile('Test Page', BladePage::class));
9276

93-
$this->assertFileExists(
94-
Hyde::path('_pages/test-page.blade.php')
95-
);
77+
$this->assertFileExists(Hyde::path('_pages/test-page.blade.php'));
78+
79+
$this->assertEquals(
80+
<<<'BLADE'
81+
@extends('hyde::layouts.app')
82+
@section('content')
83+
@php($title = "Test Page")
9684
97-
$fileContent = file_get_contents(Hyde::path('_pages/test-page.blade.php'));
98-
$this->assertEqualsIgnoringLineEndingType(
99-
'@extends(\'hyde::layouts.app\')
100-
@section(\'content\')
101-
@php($title = "Test Page")
85+
<main class="mx-auto max-w-7xl py-16 px-8">
86+
<h1 class="text-center text-3xl font-bold">Test Page</h1>
87+
</main>
10288
103-
<main class="mx-auto max-w-7xl py-16 px-8">
104-
<h1 class="text-center text-3xl font-bold">Test Page</h1>
105-
</main>
89+
@endsection
10690

107-
@endsection
108-
', $fileContent
91+
BLADE, file_get_contents(Hyde::path('_pages/test-page.blade.php'))
10992
);
93+
94+
Filesystem::unlink('_pages/test-page.blade.php');
11095
}
11196

11297
public function test_that_a_documentation_file_can_be_created_and_contains_expected_content()
11398
{
11499
(new CreatesNewPageSourceFile('Test Page', DocumentationPage::class));
115100

116-
$this->assertFileExists(
117-
Hyde::path('_docs/test-page.md')
118-
);
101+
$this->assertFileExists(Hyde::path('_docs/test-page.md'));
119102

120-
$this->assertEquals(
103+
$this->assertSame(
121104
"# Test Page\n",
122105
file_get_contents(Hyde::path('_docs/test-page.md'))
123106
);
124107

125-
Hyde::unlink('_docs/test-page.md');
108+
Filesystem::unlink('_docs/test-page.md');
126109
}
127110

128111
public function test_that_the_file_path_can_be_returned()
129112
{
130-
$this->assertEquals(
113+
$this->assertSame(
131114
Hyde::path('_pages/test-page.md'),
132115
(new CreatesNewPageSourceFile('Test Page'))->getOutputPath()
133116
);
134117

135-
$this->assertEquals(
118+
$this->assertSame(
136119
Hyde::path('_pages/test-page.blade.php'),
137120
(new CreatesNewPageSourceFile('Test Page', BladePage::class))->getOutputPath()
138121
);
122+
123+
Filesystem::unlink('_pages/test-page.md');
124+
Filesystem::unlink('_pages/test-page.blade.php');
139125
}
140126

141127
public function test_file_is_created_using_slug_generated_from_title()
142128
{
143-
new CreatesNewPageSourceFile('Foo Bar');
129+
(new CreatesNewPageSourceFile('Foo Bar'));
144130
$this->assertFileExists(Hyde::path('_pages/foo-bar.md'));
145-
Hyde::unlink('_pages/foo-bar.md');
131+
Filesystem::unlink('_pages/foo-bar.md');
146132
}
147133

148134
public function test_action_can_generate_nested_pages()
149135
{
150-
new CreatesNewPageSourceFile('foo/bar');
136+
(new CreatesNewPageSourceFile('foo/bar'));
151137
$this->assertFileExists(Hyde::path('_pages/foo/bar.md'));
152-
File::deleteDirectory(Hyde::path('_pages/foo'));
138+
Filesystem::deleteDirectory('_pages/foo');
153139
}
154140

155141
public function test_can_create_deeply_nested_pages()
156142
{
157-
new CreatesNewPageSourceFile('/foo/bar/Foo Bar');
143+
(new CreatesNewPageSourceFile('/foo/bar/Foo Bar'));
158144
$this->assertFileExists(Hyde::path('_pages/foo/bar/foo-bar.md'));
159-
File::deleteDirectory(Hyde::path('_pages/foo'));
145+
Filesystem::deleteDirectory('_pages/foo');
160146
}
161147
}

packages/framework/tests/Feature/Actions/PublishesHydeViewsTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@
77
use Hyde\Framework\Actions\PublishesHydeViews;
88
use Hyde\Hyde;
99
use Hyde\Testing\TestCase;
10+
use Illuminate\Support\Facades\File;
11+
use function is_dir;
1012

1113
/**
1214
* @covers \Hyde\Framework\Actions\PublishesHydeViews
1315
*/
1416
class PublishesHydeViewsTest extends TestCase
1517
{
16-
protected function setUp(): void
17-
{
18-
parent::setUp();
19-
20-
backupDirectory(Hyde::path('resources/views/vendor/hyde'));
21-
deleteDirectory(Hyde::path('resources/views/vendor/hyde'));
22-
}
23-
2418
protected function tearDown(): void
2519
{
26-
restoreDirectory(Hyde::path('resources/views/vendor/hyde'));
20+
if (is_dir(Hyde::path('resources/views/vendor/hyde'))) {
21+
File::deleteDirectory(Hyde::path('resources/views/vendor/hyde'));
22+
}
2723

2824
parent::tearDown();
2925
}
@@ -40,9 +36,16 @@ public function test_action_publishes_view_directories()
4036
$this->assertFileExists(Hyde::path('resources/views/vendor/hyde/layouts/app.blade.php'));
4137
}
4238

39+
public function test_action_publishes_view_components()
40+
{
41+
(new PublishesHydeViews('components'))->execute();
42+
$this->assertFileExists(Hyde::path('resources/views/vendor/hyde/components/link.blade.php'));
43+
}
44+
4345
public function test_action_publishes_view_files()
4446
{
45-
unlinkIfExists(Hyde::path('_pages/404.blade.php'));
47+
unlink(Hyde::path('_pages/404.blade.php'));
48+
4649
(new PublishesHydeViews('404'))->execute();
4750
$this->assertFileExists(Hyde::path('_pages/404.blade.php'));
4851
}

packages/framework/tests/Feature/Commands/BuildRssFeedCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function test_rss_feed_is_generated_when_conditions_are_met()
1919
config(['hyde.generate_rss_feed' => true]);
2020
$this->file('_posts/foo.md');
2121

22-
unlinkIfExists(Hyde::path('_site/feed.xml'));
22+
$this->assertFileDoesNotExist(Hyde::path('_site/feed.xml'));
2323
$this->artisan('build:rss')->assertExitCode(0);
2424

2525
$this->assertFileExists(Hyde::path('_site/feed.xml'));
@@ -33,8 +33,8 @@ public function test_rss_filename_can_be_changed()
3333
config(['hyde.rss_filename' => 'blog.xml']);
3434
$this->file('_posts/foo.md');
3535

36-
unlinkIfExists(Hyde::path('_site/feed.xml'));
37-
unlinkIfExists(Hyde::path('_site/blog.xml'));
36+
$this->assertFileDoesNotExist(Hyde::path('_site/feed.xml'));
37+
$this->assertFileDoesNotExist(Hyde::path('_site/blog.xml'));
3838

3939
$this->artisan('build:rss')->assertExitCode(0);
4040

0 commit comments

Comments
 (0)