Skip to content

Commit 3bb62f3

Browse files
committed
Add basic tests for ozu:configure-cms command
1 parent 5907ac7 commit 3bb62f3

File tree

7 files changed

+323
-57
lines changed

7 files changed

+323
-57
lines changed

phpunit.xml.dist

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
5-
backupGlobals="false"
6-
bootstrap="vendor/autoload.php"
7-
colors="true"
8-
processIsolation="false"
9-
stopOnFailure="false"
10-
executionOrder="random"
11-
failOnWarning="true"
12-
failOnRisky="true"
13-
failOnEmptyTestSuite="true"
14-
beStrictAboutOutputDuringTests="true"
15-
cacheDirectory=".phpunit.cache"
16-
backupStaticProperties="false"
17-
>
18-
<testsuites>
19-
<testsuite name="Code16 Test Suite">
20-
<directory>tests</directory>
21-
</testsuite>
22-
</testsuites>
23-
<coverage>
24-
<include>
25-
<directory suffix=".php">./src</directory>
26-
</include>
27-
<report>
28-
<html outputDirectory="build/coverage"/>
29-
<text outputFile="build/coverage.txt"/>
30-
<clover outputFile="build/logs/clover.xml"/>
31-
</report>
32-
</coverage>
33-
<logging>
34-
<junit outputFile="build/report.junit.xml"/>
35-
</logging>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<testsuites>
4+
<testsuite name="Code16 Test Suite">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<logging>
9+
<junit outputFile="build/report.junit.xml"/>
10+
</logging>
11+
<source>
12+
<include>
13+
<directory suffix=".php">./src</directory>
14+
</include>
15+
</source>
3616
</phpunit>

src/Console/ConfigureCmsCommand.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Code16\OzuClient\Console;
44

5+
use Closure;
56
use Code16\OzuClient\Client;
67
use Code16\OzuClient\OzuCms\Form\OzuField;
78
use Code16\OzuClient\OzuCms\OzuCollectionFormConfig;
@@ -16,19 +17,24 @@ class ConfigureCmsCommand extends Command
1617
protected $signature = 'ozu:configure-cms';
1718
protected $description = 'Send CMS configuration to Ozu.';
1819

19-
public function handle(Client $ozuClient): void
20+
public function handle(Client $ozuClient): int
2021
{
2122
if (empty(config('ozu-client.collections'))) {
2223
$this->info('No collections to configure.');
23-
return;
24+
return self::SUCCESS;
2425
}
2526

2627
collect(config('ozu-client.collections'))
27-
->map(function ($collectionClassName, $k) {
28-
$model = new $collectionClassName;
29-
$collection = $collectionClassName::configureOzuCollection(new OzuCollectionConfig());
30-
$list = $collectionClassName::configureOzuCollectionList(new OzuCollectionListConfig());
31-
$form = $collectionClassName::configureOzuCollectionForm(new OzuCollectionFormConfig());
28+
->map(function ($collection, $k) {
29+
$model = match (true) {
30+
is_string($collection) => app($collection),
31+
$collection instanceof Closure => $collection(),
32+
default => $collection,
33+
};
34+
35+
$collection = $model::configureOzuCollection(new OzuCollectionConfig());
36+
$list = $model::configureOzuCollectionList(new OzuCollectionListConfig());
37+
$form = $model::configureOzuCollectionForm(new OzuCollectionFormConfig());
3238

3339
return [
3440
'key' => $model->ozuCollectionKey(),
@@ -78,5 +84,7 @@ public function handle(Client $ozuClient): void
7884
});
7985

8086
$this->info('CMS configuration sent to Ozu.');
87+
88+
return self::SUCCESS;
8189
}
8290
}

src/OzuCms/OzuCollectionConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public function setIsDeletable(bool $isDeletable = true): self
4747

4848
public function label(): string
4949
{
50-
return $this->label;
50+
return $this->label ?? 'no label';
5151
}
5252

53-
public function icon(): string
53+
public function icon(): ?string
5454
{
55-
return $this->icon;
55+
return $this->icon ?? null;
5656
}
5757

5858
public function hasPublicationState(): bool

src/OzuServiceProvider.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Code16\OzuClient\View\Components\Content;
1212
use Code16\OzuClient\View\Components\File;
1313
use Code16\OzuClient\View\Components\Image;
14-
use Illuminate\Database\Eloquent\Relations\Relation;
1514
use Illuminate\Pagination\LengthAwarePaginator;
1615
use Illuminate\Pagination\Paginator;
1716
use Illuminate\Support\Facades\Blade;
@@ -75,12 +74,12 @@ public function boot()
7574
return request()->route()->parameter('page');
7675
});
7776

78-
Relation::enforceMorphMap(
79-
collect(config('ozu-client.collections'))
80-
->mapWithKeys(fn ($className) => [
81-
(new $className)->ozuCollectionKey() => $className
82-
])
83-
->toArray()
84-
);
77+
// Relation::enforceMorphMap(
78+
// collect(config('ozu-client.collections'))
79+
// ->mapWithKeys(fn ($className) => [
80+
// (new $className)->ozuCollectionKey() => $className
81+
// ])
82+
// ->toArray()
83+
// );
8584
}
8685
}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
<?php
2+
3+
use Code16\OzuClient\OzuCms\Form\OzuField;
4+
use Code16\OzuClient\OzuCms\List\OzuColumn;
5+
use Code16\OzuClient\OzuCms\OzuCollectionConfig;
6+
use Code16\OzuClient\OzuCms\OzuCollectionFormConfig;
7+
use Code16\OzuClient\OzuCms\OzuCollectionListConfig;
8+
use Code16\OzuClient\Tests\Fixtures\DummyTestModel;
9+
use Illuminate\Console\Command;
10+
use \Illuminate\Http\Client\Request;
11+
12+
beforeEach(function () {
13+
config(['ozu-client.website_key' => 'test']);
14+
Http::fake();
15+
});
16+
17+
it('sends cms configuration to Ozu for each configured collection', function () {
18+
config(['ozu-client.collections' => [
19+
new class extends DummyTestModel {
20+
public function ozuCollectionKey(): string
21+
{
22+
return 'dummy1';
23+
}
24+
},
25+
new class extends DummyTestModel {
26+
public function ozuCollectionKey(): string
27+
{
28+
return 'dummy2';
29+
}
30+
}
31+
]]);
32+
33+
$this->artisan('ozu:configure-cms')
34+
->expectsOutput('CMS configuration sent to Ozu.')
35+
->assertExitCode(Command::SUCCESS);
36+
37+
Http::assertSent(function (Request $request) {
38+
return $request->url() == sprintf(
39+
'%s/api/%s/%s/collections/%s/configure',
40+
rtrim(config('ozu-client.api_host'), '/'),
41+
config('ozu-client.api_version'),
42+
'test',
43+
'dummy1'
44+
);
45+
});
46+
47+
Http::assertSent(function (Request $request) {
48+
return $request->url() == sprintf(
49+
'%s/api/%s/%s/collections/%s/configure',
50+
rtrim(config('ozu-client.api_host'), '/'),
51+
config('ozu-client.api_version'),
52+
'test',
53+
'dummy2'
54+
);
55+
});
56+
});
57+
58+
it('sends general cms configuration to Ozu', function () {
59+
Http::fake();
60+
61+
config(['ozu-client.collections' => [
62+
new class extends DummyTestModel {
63+
public function ozuCollectionKey(): string
64+
{
65+
return 'dummy';
66+
}
67+
68+
public static function configureOzuCollection(OzuCollectionConfig $config
69+
): OzuCollectionConfig {
70+
return $config
71+
->setLabel('Dummy label')
72+
->setIcon('dummy-icon')
73+
->setHasPublicationState()
74+
->setIsCreatable()
75+
->setIsDeletable();
76+
}
77+
}
78+
]]);
79+
80+
$this->artisan('ozu:configure-cms')
81+
->expectsOutput('CMS configuration sent to Ozu.')
82+
->assertExitCode(Command::SUCCESS);
83+
84+
Http::assertSent(function (Request $request) {
85+
return $request->url() == sprintf(
86+
'%s/api/%s/%s/collections/%s/configure',
87+
rtrim(config('ozu-client.api_host'), '/'),
88+
config('ozu-client.api_version'),
89+
'test',
90+
'dummy'
91+
)
92+
&& $request['label'] == 'Dummy label'
93+
&& $request['icon'] == 'dummy-icon'
94+
&& $request['hasPublicationState'] == true
95+
&& $request['isCreatable'] == true
96+
&& $request['isDeletable'] == true;
97+
});
98+
});
99+
100+
it('sends list cms configuration to Ozu', function () {
101+
Http::fake();
102+
103+
config(['ozu-client.collections' => [
104+
new class extends DummyTestModel {
105+
public function ozuCollectionKey(): string
106+
{
107+
return 'dummy';
108+
}
109+
110+
public static function configureOzuCollectionList(OzuCollectionListConfig $config): OzuCollectionListConfig
111+
{
112+
return $config
113+
->setIsPaginated()
114+
->setIsReorderable()
115+
->setIsSearchable()
116+
->addColumn(OzuColumn::makeText('dummy-text', 1)->setLabel('Dummy text'))
117+
->addColumn(OzuColumn::makeCheck('dummy-check', 2)->setLabel('Dummy check'))
118+
->addColumn(OzuColumn::makeImage('dummy-image', 3)->setLabel('Dummy image'))
119+
->addColumn(OzuColumn::makeDate('dummy-date', 3)->setLabel('Dummy date'));
120+
}
121+
}
122+
]]);
123+
124+
$this->artisan('ozu:configure-cms')
125+
->expectsOutput('CMS configuration sent to Ozu.')
126+
->assertExitCode(Command::SUCCESS);
127+
128+
Http::assertSent(function (Request $request) {
129+
return $request['list']['isReorderable'] == true
130+
&& $request['list']['isSearchable'] == true
131+
&& $request['list']['isPaginated'] == true
132+
&& $request['list']['columns'] == collect([
133+
[
134+
'type' => 'text',
135+
'key' => 'dummy-text',
136+
'label' => 'Dummy text',
137+
'size' => 1
138+
],
139+
[
140+
'type' => 'check',
141+
'key' => 'dummy-check',
142+
'label' => 'Dummy check',
143+
'size' => 2
144+
],
145+
[
146+
'type' => 'image',
147+
'key' => 'dummy-image',
148+
'label' => 'Dummy image',
149+
'size' => 3
150+
],
151+
[
152+
'type' => 'date',
153+
'key' => 'dummy-date',
154+
'label' => 'Dummy date',
155+
'size' => 3
156+
]
157+
]);
158+
});
159+
});
160+
161+
it('sends form cms configuration to Ozu', function () {
162+
Http::fake();
163+
164+
config(['ozu-client.collections' => [
165+
new class extends DummyTestModel {
166+
public function ozuCollectionKey(): string
167+
{
168+
return 'dummy';
169+
}
170+
171+
public static function configureOzuCollectionForm(OzuCollectionFormConfig $config): OzuCollectionFormConfig
172+
{
173+
return $config
174+
->addCustomField(
175+
OzuField::makeText('dummy-text')
176+
->setLabel('Dummy text')
177+
->setValidationRules(['required'])
178+
)
179+
->addCustomField(
180+
OzuField::makeSelect('dummy-select')
181+
->setDisplayAsDropdown()
182+
->setOptions([1 =>'option1', 2 => 'option2'])
183+
->setLabel('Dummy select')
184+
->setHelpMessage('Select an option')
185+
);
186+
}
187+
}
188+
]]);
189+
190+
$this->artisan('ozu:configure-cms')
191+
->expectsOutput('CMS configuration sent to Ozu.')
192+
->assertExitCode(Command::SUCCESS);
193+
194+
Http::assertSent(function (Request $request) {
195+
return $request['form']['fields'] == collect([
196+
[
197+
'type' => 'text',
198+
'key' => 'dummy-text',
199+
'label' => 'Dummy text',
200+
'validationRules' => ['required'],
201+
'helpMessage' => null,
202+
'isUpdatable' => true,
203+
],
204+
[
205+
'type' => 'select',
206+
'key' => 'dummy-select',
207+
'label' => 'Dummy select',
208+
'options' => [1 =>'option1', 2 => 'option2'],
209+
'multiple' => false,
210+
'display' => 'dropdown',
211+
'clearable' => false,
212+
'validationRules' => [],
213+
'helpMessage' => 'Select an option',
214+
'isUpdatable' => true,
215+
],
216+
]);
217+
});
218+
});
219+
220+
it('sends custom fields configuration to Ozu', function () {
221+
Http::fake();
222+
223+
Schema::partialMock()
224+
->shouldReceive(
225+
'getColumnListing',
226+
'getColumnType'
227+
)
228+
->andReturn(
229+
[
230+
...DummyTestModel::$ozuColumns,
231+
'dummy_text',
232+
],
233+
'text'
234+
);
235+
236+
config(['ozu-client.collections' => [
237+
new class extends DummyTestModel {
238+
public function ozuCollectionKey(): string
239+
{
240+
return 'dummy';
241+
}
242+
}
243+
]]);
244+
245+
$this->artisan('ozu:configure-cms')
246+
->expectsOutput('CMS configuration sent to Ozu.')
247+
->assertExitCode(Command::SUCCESS);
248+
249+
Http::assertSent(function (Request $request) {
250+
return $request['customFields'] == collect([
251+
'dummy_text' => 'string',
252+
]);
253+
});
254+
});
255+

0 commit comments

Comments
 (0)