|
| 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