|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Craftzing\TestBench\Laravel\Constraint\Bus; |
| 6 | + |
| 7 | +use Craftzing\TestBench\PHPUnit\Doubles\SpyCallable; |
| 8 | +use Illuminate\Support\Facades\Bus; |
| 9 | +use InvalidArgumentException; |
| 10 | +use Orchestra\Testbench\TestCase; |
| 11 | +use PHPUnit\Framework\Attributes\Test; |
| 12 | +use PHPUnit\Framework\Attributes\TestWith; |
| 13 | +use PHPUnit\Framework\ExpectationFailedException; |
| 14 | +use stdClass; |
| 15 | + |
| 16 | +/** |
| 17 | + * @codeCoverageIgnore |
| 18 | + */ |
| 19 | +final class HasHandlerTest extends TestCase |
| 20 | +{ |
| 21 | + #[Test] |
| 22 | + #[TestWith([true], 'Boolean')] |
| 23 | + #[TestWith([1], 'Integers')] |
| 24 | + #[TestWith([['event']], 'Array')] |
| 25 | + public function itCannotEvaluateUnsupportedValueTypes(mixed $value): void |
| 26 | + { |
| 27 | + $this->expectException(InvalidArgumentException::class); |
| 28 | + $this->expectExceptionMessage(HasHandler::class . ' can only be evaluated for strings'); |
| 29 | + |
| 30 | + $this->assertThat($value, new HasHandler('SomeHandlerClassFCN')); |
| 31 | + } |
| 32 | + |
| 33 | + #[Test] |
| 34 | + public function itCannotEvaluateStringThatAreNotExistingClasses(): void |
| 35 | + { |
| 36 | + $value = 'NotAClass'; |
| 37 | + |
| 38 | + $this->expectException(InvalidArgumentException::class); |
| 39 | + $this->expectExceptionMessage(HasHandler::class . " can only be evaluated for existing classes, got $value."); |
| 40 | + |
| 41 | + $this->assertThat($value, new HasHandler('SomeHandlerClassFCN')); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function itFailsWhenNoHandlerIsMapped(): void |
| 46 | + { |
| 47 | + $messageClassFCN = stdClass::class; |
| 48 | + |
| 49 | + $this->expectException(ExpectationFailedException::class); |
| 50 | + $this->expectExceptionMessage('has handler'); |
| 51 | + $this->expectExceptionMessage('stdClass has no handler mapped to it'); |
| 52 | + |
| 53 | + $this->assertThat($messageClassFCN, new HasHandler('SomeHandlerClassFCN')); |
| 54 | + } |
| 55 | + |
| 56 | + #[Test] |
| 57 | + public function itFailsWhenDifferentHandlerIsMapped(): void |
| 58 | + { |
| 59 | + $messageClassFCN = stdClass::class; |
| 60 | + Bus::map([$messageClassFCN => SpyCallable::class]); |
| 61 | + |
| 62 | + $this->expectException(ExpectationFailedException::class); |
| 63 | + $this->expectExceptionMessage('has handler'); |
| 64 | + $this->expectExceptionMessage('stdClass has a different handler mapped to it'); |
| 65 | + |
| 66 | + $this->assertThat($messageClassFCN, new HasHandler('SomeHandlerClassFCN')); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function itPassesWhenGivenHandlerIsMapped(): void |
| 71 | + { |
| 72 | + $messageClassFCN = stdClass::class; |
| 73 | + Bus::map([$messageClassFCN => SpyCallable::class]); |
| 74 | + |
| 75 | + $this->assertThat($messageClassFCN, new HasHandler(SpyCallable::class)); |
| 76 | + } |
| 77 | +} |
0 commit comments