|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Support\Tests\Conditions; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Tempest\Support\Conditions\HasConditions; |
| 9 | + |
| 10 | +/** |
| 11 | + * @internal |
| 12 | + */ |
| 13 | +final class HasConditionsTest extends TestCase |
| 14 | +{ |
| 15 | + public function test_when(): void |
| 16 | + { |
| 17 | + $class = new class () { |
| 18 | + use HasConditions; |
| 19 | + |
| 20 | + public bool $value = false; |
| 21 | + }; |
| 22 | + |
| 23 | + $class->when(true, fn ($c) => $c->value = true); // @phpstan-ignore-line |
| 24 | + |
| 25 | + $this->assertTrue($class->value); |
| 26 | + } |
| 27 | + |
| 28 | + public function test_when_with_callback(): void |
| 29 | + { |
| 30 | + $class = new class () { |
| 31 | + use HasConditions; |
| 32 | + |
| 33 | + public bool $value = false; |
| 34 | + }; |
| 35 | + |
| 36 | + $class->when(fn () => true, fn ($c) => $c->value = true); // @phpstan-ignore-line |
| 37 | + |
| 38 | + $this->assertTrue($class->value); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_unless(): void |
| 42 | + { |
| 43 | + $class = new class () { |
| 44 | + use HasConditions; |
| 45 | + |
| 46 | + public bool $value = false; |
| 47 | + }; |
| 48 | + |
| 49 | + $class->unless(true, fn ($c) => $c->value = true); // @phpstan-ignore-line |
| 50 | + |
| 51 | + $this->assertFalse($class->value); |
| 52 | + } |
| 53 | + |
| 54 | + public function test_unless_with_callback(): void |
| 55 | + { |
| 56 | + $class = new class () { |
| 57 | + use HasConditions; |
| 58 | + |
| 59 | + public bool $value = false; |
| 60 | + }; |
| 61 | + |
| 62 | + $class->unless(fn () => true, fn ($c) => $c->value = true); // @phpstan-ignore-line |
| 63 | + |
| 64 | + $this->assertFalse($class->value); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_returns_same_instance(): void |
| 68 | + { |
| 69 | + $class = new class () { |
| 70 | + use HasConditions; |
| 71 | + |
| 72 | + public string $string = 'foo'; |
| 73 | + |
| 74 | + public function append(string $string): static |
| 75 | + { |
| 76 | + $self = new self(); |
| 77 | + $self->string = $this->string . $string; |
| 78 | + |
| 79 | + return $self; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + $class->when(true, function ($c): void { // @phpstan-ignore-line |
| 84 | + $c->append('bar'); |
| 85 | + }); |
| 86 | + |
| 87 | + $this->assertSame('foo', $class->string); |
| 88 | + } |
| 89 | +} |
0 commit comments