Skip to content

Commit

Permalink
Merge pull request #4 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Added the ability to set custom macro names
  • Loading branch information
andrey-helldar authored Aug 23, 2024
2 parents f257a8b + ebfb423 commit 31c7850
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 22 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
'response' => [
ToDataMacro::class,
ToDataCollectionMacro::class,

// CustomMacro::class,
// 'toFoo' => CustomMacro::class,
],
],
];
16 changes: 6 additions & 10 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace DragonCode\LaravelHttpMacros;

use DragonCode\LaravelHttpMacros\Macros\Macro;
use Illuminate\Http\Client\Response;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

Expand All @@ -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<string|Macro>
*/
protected function macros(): array
{
return $this->app['config']->get('http.macros.response', []);
return config('http.macros.response', []);
}

protected function registerConfig(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Data;

class ConstructorData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Data;

class FromMethodData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Data;

use Spatie\LaravelData\Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Data;

use Spatie\LaravelData\Data;

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
]);
});
}
}
2 changes: 1 addition & 1 deletion tests/Unit/ToDataCollectionConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataCollectionFromMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataCollectionSpatieConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataCollectionSpatieFromMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Tests\Fixtures\ConstructorData;
use Tests\Fixtures\Data\ConstructorData;

test('simple', function () {
$response = fakeRequest()->get('simple');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataFromMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Tests\Fixtures\FromMethodData;
use Tests\Fixtures\Data\FromMethodData;

test('simple', function () {
$response = fakeRequest()->get('simple');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataSpatieConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Tests\Fixtures\SpatieConstructorData;
use Tests\Fixtures\Data\SpatieConstructorData;

test('simple', function () {
$response = fakeRequest()->get('simple');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ToDataSpatieFromMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Tests\Fixtures\SpatiePropertiesData;
use Tests\Fixtures\Data\SpatiePropertiesData;

test('simple', function () {
$response = fakeRequest()->get('simple');
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/ToFooTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

use Tests\Fixtures\Data\ConstructorData;

test('simple', function () {
$response = fakeRequest()->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();
});

0 comments on commit 31c7850

Please sign in to comment.