Skip to content

Commit 7218cb6

Browse files
committed
Add TaxonomyBlueprintsController::edit test
1 parent a26b0b6 commit 7218cb6

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"Tdwesten\\StatamicBuilder\\": "src"
88
}
99
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"Tests\\": "tests"
13+
}
14+
},
1015
"extra": {
1116
"statamic": {
1217
"name": "Statamic Builder",

tests/TestCase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Tests;
44

5-
use Orchestra\Testbench\TestCase as Orchestra;
5+
use Statamic\Testing\AddonTestCase;
6+
use Tdwesten\StatamicBuilder\ServiceProvider;
67

7-
abstract class TestCase extends Orchestra
8+
abstract class TestCase extends AddonTestCase
89
{
9-
//
10+
protected string $addonServiceProvider = ServiceProvider::class;
1011
}

tests/Unit/BlueprintTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3+
use Statamic\Facades\Taxonomy;
34
use Tdwesten\StatamicBuilder\Exceptions\BlueprintRenderException;
45
use Tdwesten\StatamicBuilder\FieldTypes\Tab;
56
use Tdwesten\StatamicBuilder\FieldTypes\Text;
7+
use Tdwesten\StatamicBuilder\Http\Controllers\TaxonomyBlueprintsController;
68
use Tests\Helpers\TestBlueprint;
9+
use Tests\TestCase;
710

811
test('Has a title', function (): void {
912
$blueprint = TestBlueprint::make('test_blueprint');
@@ -81,3 +84,77 @@
8184

8285
expect($blueprint->getHandle())->toBe('test_blueprint');
8386
});
87+
88+
pest()->extend(TestCase::class);
89+
90+
const TEST_FILES_DIRECTORY = __DIR__.'/../__fixtures__';
91+
const TAX_DIRECTORY = TEST_FILES_DIRECTORY.'/content/taxonomies';
92+
const TEST_TAX_BLUEPRINTS_DIRECTORY = TEST_FILES_DIRECTORY.'/resources/blueprints/taxonomies/test_tax';
93+
94+
test('taxonomy blueprints can be edited', function (): void {
95+
96+
// Prepare fixtures directory
97+
if (! (is_dir(TEST_FILES_DIRECTORY) && is_dir(TAX_DIRECTORY) && is_dir(TEST_TAX_BLUEPRINTS_DIRECTORY))) {
98+
$created = mkdir(TAX_DIRECTORY, recursive: true)
99+
&& mkdir(TEST_TAX_BLUEPRINTS_DIRECTORY, recursive: true);
100+
101+
if (! $created) {
102+
$this->fail('Could not create test files directory (in '.TEST_FILES_DIRECTORY.')');
103+
}
104+
}
105+
106+
/**
107+
* The issue we're testing for regards normal Statamic Taxonomy Blueprints
108+
* and their interaction with Statamic-Builder's controller.
109+
*
110+
* So, we'll need to make files for Statamic to load in during the test.
111+
* These will be removed in the ->after callback.
112+
*/
113+
$test_tax_blueprint = <<<'BLUEPRINT'
114+
title: Test_Taxonomy_Blueprint
115+
tabs:
116+
main:
117+
display: Main
118+
sections:
119+
-
120+
fields:
121+
-
122+
handle: title
123+
field:
124+
type: text
125+
required: true
126+
validate:
127+
- required
128+
BLUEPRINT;
129+
130+
$test_tax = <<<'TAXONOMY'
131+
title: Test_Tax
132+
TAXONOMY;
133+
134+
file_put_contents(TAX_DIRECTORY.'/test_tax.yaml', $test_tax);
135+
file_put_contents(TEST_TAX_BLUEPRINTS_DIRECTORY.'/test_tax.yaml', $test_tax_blueprint);
136+
137+
// Create a new controller and attempt to edit the taxonomy blueprint with it
138+
(new TaxonomyBlueprintsController)->edit(Taxonomy::findByHandle('test_tax'), 'test_tax');
139+
})
140+
->throwsNoExceptions()
141+
->after(function () {
142+
function removeDir(string $dir): void
143+
{
144+
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
145+
$files = new RecursiveIteratorIterator(
146+
$it,
147+
RecursiveIteratorIterator::CHILD_FIRST
148+
);
149+
foreach ($files as $file) {
150+
if ($file->isDir()) {
151+
rmdir($file->getPathname());
152+
} else {
153+
unlink($file->getPathname());
154+
}
155+
}
156+
rmdir($dir);
157+
}
158+
159+
removeDir(TEST_FILES_DIRECTORY);
160+
});

0 commit comments

Comments
 (0)