|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Container; |
| 4 | + |
| 5 | +use Illuminate\Container\Attributes\Config; |
| 6 | +use Illuminate\Contracts\Container\Buildable; |
| 7 | +use Illuminate\Support\Facades\Validator; |
| 8 | +use Illuminate\Validation\ValidationException; |
| 9 | +use Orchestra\Testbench\TestCase; |
| 10 | + |
| 11 | +class BuildableIntegrationTest extends TestCase |
| 12 | +{ |
| 13 | + public function test_example(): void |
| 14 | + { |
| 15 | + config([ |
| 16 | + 'aim' => [ |
| 17 | + 'api_key' => 'api-key', |
| 18 | + 'user_name' => 'cosmastech', |
| 19 | + 'away_message' => [ |
| 20 | + 'duration' => 500, |
| 21 | + 'body' => 'sad emo lyrics', |
| 22 | + ], |
| 23 | + ], |
| 24 | + ]); |
| 25 | + |
| 26 | + $config = $this->app->make(AolInstantMessengerConfig::class); |
| 27 | + |
| 28 | + $this->assertEquals(500, $config->awayMessageDuration); |
| 29 | + $this->assertEquals('sad emo lyrics', $config->awayMessage); |
| 30 | + $this->assertEquals('api-key', $config->apiKey); |
| 31 | + $this->assertEquals('cosmastech', $config->userName); |
| 32 | + |
| 33 | + config(['aim.away_message.duration' => 5]); |
| 34 | + |
| 35 | + try { |
| 36 | + $this->app->make(AolInstantMessengerConfig::class); |
| 37 | + } catch (ValidationException $exception) { |
| 38 | + $this->assertArrayHasKey('away_message.duration', $exception->errors()); |
| 39 | + $this->assertStringContainsString('60', $exception->errors()['away_message.duration'][0]); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class AolInstantMessengerConfig implements Buildable |
| 45 | +{ |
| 46 | + public function __construct( |
| 47 | + #[Config('aim.api_key')] |
| 48 | + public string $apiKey, |
| 49 | + #[Config('aim.user_name')] |
| 50 | + public string $userName, |
| 51 | + #[Config('aim.away_message.duration')] |
| 52 | + public int $awayMessageDuration, |
| 53 | + #[Config('aim.away_message.body')] |
| 54 | + public string $awayMessage |
| 55 | + ) { |
| 56 | + } |
| 57 | + |
| 58 | + public static function build() |
| 59 | + { |
| 60 | + Validator::make(config('aim'), [ |
| 61 | + 'api-key' => 'string', |
| 62 | + 'user_name' => 'string', |
| 63 | + 'away_message' => 'array', |
| 64 | + 'away_message.duration' => ['integer', 'min:60', 'max:3600'], |
| 65 | + 'away_message.body' => ['string', 'min:1'], |
| 66 | + ])->validate(); |
| 67 | + |
| 68 | + return app()->build(static::class); |
| 69 | + } |
| 70 | +} |
0 commit comments