From ebfb423da272d61dc0e4ba21d7f116d728a1c2b2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Fri, 23 Aug 2024 11:07:43 +0300 Subject: [PATCH] Added the ability to set custom macro names --- README.md | 49 +++++++++++++++++ config/http.php | 3 + src/ServiceProvider.php | 16 ++---- tests/Fixtures/{ => Data}/ConstructorData.php | 2 +- tests/Fixtures/{ => Data}/FromMethodData.php | 2 +- .../{ => Data}/SpatieConstructorData.php | 2 +- .../{ => Data}/SpatiePropertiesData.php | 2 +- tests/TestCase.php | 15 +++++ .../Unit/ToDataCollectionConstructorTest.php | 2 +- tests/Unit/ToDataCollectionFromMethodTest.php | 2 +- .../ToDataCollectionSpatieConstructorTest.php | 2 +- .../ToDataCollectionSpatieFromMethodTest.php | 2 +- tests/Unit/ToDataConstructorTest.php | 2 +- tests/Unit/ToDataFromMethodTest.php | 2 +- tests/Unit/ToDataSpatieConstructor.php | 2 +- tests/Unit/ToDataSpatieFromMethodTest.php | 2 +- tests/Unit/ToFooTest.php | 55 +++++++++++++++++++ 17 files changed, 140 insertions(+), 22 deletions(-) rename tests/Fixtures/{ => Data}/ConstructorData.php (83%) rename tests/Fixtures/{ => Data}/FromMethodData.php (89%) rename tests/Fixtures/{ => Data}/SpatieConstructorData.php (86%) rename tests/Fixtures/{ => Data}/SpatiePropertiesData.php (83%) create mode 100644 tests/Unit/ToFooTest.php diff --git a/README.md b/README.md index e15c6da..77aaf54 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,55 @@ To get the latest version of `HTTP Macros`, simply require the project using [Co composer require dragon-code/laravel-http-macros ``` +## Configuration + +If desired, you can publish the configuration file using the console command: + +```bash +php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider" +``` + +If your application already has a `config/http.php` file, then you can simply add a new `macros` key from the +[configuration](config/http.php) file to it. + +Here you can specify a list of your classes for registering macros. +Macro classes must inherit from the abstract class `DragonCode\LaravelHttpMacros\Macros\Macro`. + +You can also redefine macro names using an associative array. For example: + +```php +// Config +return [ + 'macros' => [ + 'response' => [ + ToDataMacro::class, + ], + ], +]; + +// Macro +Http::get()->toData(...); +``` + +```php +// Config +return [ + 'macros' => [ + 'response' => [ + 'qwerty' => ToDataMacro::class, + ], + ], +]; + +// Macro +Http::get()->qwerty(...); +Http::get()->toData(...); // will be method not found exception +``` + +> Note +> +> Please note that IDE hints will not work in this case. + ## Usage ### As Class diff --git a/config/http.php b/config/http.php index f80a51f..ff81af3 100644 --- a/config/http.php +++ b/config/http.php @@ -10,6 +10,9 @@ 'response' => [ ToDataMacro::class, ToDataCollectionMacro::class, + + // CustomMacro::class, + // 'toFoo' => CustomMacro::class, ], ], ]; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 2e02537..3aeae18 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,7 +4,6 @@ namespace DragonCode\LaravelHttpMacros; -use DragonCode\LaravelHttpMacros\Macros\Macro; use Illuminate\Http\Client\Response; use Illuminate\Support\ServiceProvider as BaseServiceProvider; @@ -23,20 +22,17 @@ public function boot(): void protected function bootMacros(): void { - foreach ($this->macros() as $macros) { - Response::macro($macros::name(), $macros::callback()); + foreach ($this->macros() as $name => $macro) { + Response::macro( + name : is_string($name) ? $name : $macro::name(), + macro: $macro::callback() + ); } } - /** - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface - * - * @return array - */ protected function macros(): array { - return $this->app['config']->get('http.macros.response', []); + return config('http.macros.response', []); } protected function registerConfig(): void diff --git a/tests/Fixtures/ConstructorData.php b/tests/Fixtures/Data/ConstructorData.php similarity index 83% rename from tests/Fixtures/ConstructorData.php rename to tests/Fixtures/Data/ConstructorData.php index a77c699..a24b61d 100644 --- a/tests/Fixtures/ConstructorData.php +++ b/tests/Fixtures/Data/ConstructorData.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Data; class ConstructorData { diff --git a/tests/Fixtures/FromMethodData.php b/tests/Fixtures/Data/FromMethodData.php similarity index 89% rename from tests/Fixtures/FromMethodData.php rename to tests/Fixtures/Data/FromMethodData.php index a26dc67..cac73a4 100644 --- a/tests/Fixtures/FromMethodData.php +++ b/tests/Fixtures/Data/FromMethodData.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Data; class FromMethodData { diff --git a/tests/Fixtures/SpatieConstructorData.php b/tests/Fixtures/Data/SpatieConstructorData.php similarity index 86% rename from tests/Fixtures/SpatieConstructorData.php rename to tests/Fixtures/Data/SpatieConstructorData.php index 730b4ac..d341550 100644 --- a/tests/Fixtures/SpatieConstructorData.php +++ b/tests/Fixtures/Data/SpatieConstructorData.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Data; use Spatie\LaravelData\Data; diff --git a/tests/Fixtures/SpatiePropertiesData.php b/tests/Fixtures/Data/SpatiePropertiesData.php similarity index 83% rename from tests/Fixtures/SpatiePropertiesData.php rename to tests/Fixtures/Data/SpatiePropertiesData.php index a29baa3..a6922b9 100644 --- a/tests/Fixtures/SpatiePropertiesData.php +++ b/tests/Fixtures/Data/SpatiePropertiesData.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Data; use Spatie\LaravelData\Data; diff --git a/tests/TestCase.php b/tests/TestCase.php index 86b4a7e..ac51341 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,10 @@ namespace Tests; +use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro; +use DragonCode\LaravelHttpMacros\Macros\ToDataMacro; use DragonCode\LaravelHttpMacros\ServiceProvider; +use Illuminate\Config\Repository; use Orchestra\Testbench\TestCase as BaseTestCase; use Spatie\LaravelData\LaravelDataServiceProvider; @@ -15,4 +18,16 @@ protected function getPackageProviders($app): array ServiceProvider::class, ]; } + + protected function defineEnvironment($app): void + { + tap($app['config'], function (Repository $config) { + $config->set('http.macros.response', [ + ToDataMacro::class, + ToDataCollectionMacro::class, + + 'toFoo' => ToDataMacro::class, + ]); + }); + } } diff --git a/tests/Unit/ToDataCollectionConstructorTest.php b/tests/Unit/ToDataCollectionConstructorTest.php index 403958c..a20f703 100644 --- a/tests/Unit/ToDataCollectionConstructorTest.php +++ b/tests/Unit/ToDataCollectionConstructorTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Collection; -use Tests\Fixtures\ConstructorData; +use Tests\Fixtures\Data\ConstructorData; test('many', function () { $response = fakeRequest()->get('many'); diff --git a/tests/Unit/ToDataCollectionFromMethodTest.php b/tests/Unit/ToDataCollectionFromMethodTest.php index 0c17fd7..6f87667 100644 --- a/tests/Unit/ToDataCollectionFromMethodTest.php +++ b/tests/Unit/ToDataCollectionFromMethodTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Collection; -use Tests\Fixtures\FromMethodData; +use Tests\Fixtures\Data\FromMethodData; test('many', function () { $response = fakeRequest()->get('many'); diff --git a/tests/Unit/ToDataCollectionSpatieConstructorTest.php b/tests/Unit/ToDataCollectionSpatieConstructorTest.php index c08b75e..1ecba4a 100644 --- a/tests/Unit/ToDataCollectionSpatieConstructorTest.php +++ b/tests/Unit/ToDataCollectionSpatieConstructorTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Collection; -use Tests\Fixtures\SpatieConstructorData; +use Tests\Fixtures\Data\SpatieConstructorData; test('many', function () { $response = fakeRequest()->get('many'); diff --git a/tests/Unit/ToDataCollectionSpatieFromMethodTest.php b/tests/Unit/ToDataCollectionSpatieFromMethodTest.php index 3450895..12bd04c 100644 --- a/tests/Unit/ToDataCollectionSpatieFromMethodTest.php +++ b/tests/Unit/ToDataCollectionSpatieFromMethodTest.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Illuminate\Support\Collection; -use Tests\Fixtures\SpatiePropertiesData; +use Tests\Fixtures\Data\SpatiePropertiesData; test('many', function () { $response = fakeRequest()->get('many'); diff --git a/tests/Unit/ToDataConstructorTest.php b/tests/Unit/ToDataConstructorTest.php index ea5ea38..91dd8bd 100644 --- a/tests/Unit/ToDataConstructorTest.php +++ b/tests/Unit/ToDataConstructorTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use Tests\Fixtures\ConstructorData; +use Tests\Fixtures\Data\ConstructorData; test('simple', function () { $response = fakeRequest()->get('simple'); diff --git a/tests/Unit/ToDataFromMethodTest.php b/tests/Unit/ToDataFromMethodTest.php index cbbfa9d..619edf5 100644 --- a/tests/Unit/ToDataFromMethodTest.php +++ b/tests/Unit/ToDataFromMethodTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use Tests\Fixtures\FromMethodData; +use Tests\Fixtures\Data\FromMethodData; test('simple', function () { $response = fakeRequest()->get('simple'); diff --git a/tests/Unit/ToDataSpatieConstructor.php b/tests/Unit/ToDataSpatieConstructor.php index ecfa54c..fb75eea 100644 --- a/tests/Unit/ToDataSpatieConstructor.php +++ b/tests/Unit/ToDataSpatieConstructor.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use Tests\Fixtures\SpatieConstructorData; +use Tests\Fixtures\Data\SpatieConstructorData; test('simple', function () { $response = fakeRequest()->get('simple'); diff --git a/tests/Unit/ToDataSpatieFromMethodTest.php b/tests/Unit/ToDataSpatieFromMethodTest.php index 0ab2c06..ffdf4ff 100644 --- a/tests/Unit/ToDataSpatieFromMethodTest.php +++ b/tests/Unit/ToDataSpatieFromMethodTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use Tests\Fixtures\SpatiePropertiesData; +use Tests\Fixtures\Data\SpatiePropertiesData; test('simple', function () { $response = fakeRequest()->get('simple'); diff --git a/tests/Unit/ToFooTest.php b/tests/Unit/ToFooTest.php new file mode 100644 index 0000000..cca0e01 --- /dev/null +++ b/tests/Unit/ToFooTest.php @@ -0,0 +1,55 @@ +get('simple'); + + expect($response->toFoo(ConstructorData::class)) + ->toBeInstanceOf(ConstructorData::class) + ->id->toBe(1) + ->title->toBe('Qwerty 1'); +}); + +test('single', function () { + $response = fakeRequest()->get('single'); + + expect($response->toFoo(ConstructorData::class, 'result.item')) + ->toBeInstanceOf(ConstructorData::class) + ->id->toBe(1) + ->title->toBe('Qwerty 1'); +}); + +test('many', function () { + $response = fakeRequest()->get('many'); + + expect($response->toFoo(ConstructorData::class, 'result.items.1')) + ->toBeInstanceOf(ConstructorData::class) + ->id->toBe(3) + ->title->toBe('Qwerty 3'); +}); + +test('callback', function () { + $response = fakeRequest()->get('many'); + + expect( + $response->toFoo( + fn (array $items) => new ConstructorData( + $items[0]['id'], + $items[1]['title'], + ), + 'result.items' + ) + ) + ->toBeInstanceOf(ConstructorData::class) + ->id->toBe(2) + ->title->toBe('Qwerty 3'); +}); + +test('missing key', function () { + $response = fakeRequest()->get('simple'); + + expect($response->toFoo(ConstructorData::class, 'missing_key'))->toBeNull(); +});