|
| 1 | +<?php |
| 2 | + |
| 3 | +use MightFail\Either; |
| 4 | + |
| 5 | +test('class must exist', function () { |
| 6 | + expect(class_exists(Either::class))->toBeTrue(); |
| 7 | +}); |
| 8 | + |
| 9 | +test('should extend the IteratorIterator class', function () { |
| 10 | + $either = new Either('foo'); |
| 11 | + |
| 12 | + expect($either)->toBeInstanceOf(IteratorIterator::class); |
| 13 | +}); |
| 14 | + |
| 15 | +test('should implement the ArrayAccess interface', function () { |
| 16 | + $either = new Either('foo'); |
| 17 | + |
| 18 | + expect($either)->toBeInstanceOf(ArrayAccess::class); |
| 19 | +}); |
| 20 | + |
| 21 | +test('should have a result property', function () { |
| 22 | + $either = new Either('foo'); |
| 23 | + |
| 24 | + expect($either->result)->toBe('foo'); |
| 25 | +}); |
| 26 | + |
| 27 | +test('should have an error property', function () { |
| 28 | + $either = new Either(null, new Exception('foo')); |
| 29 | + |
| 30 | + expect($either->error)->toBeInstanceOf(Exception::class); |
| 31 | +}); |
| 32 | + |
| 33 | +test('should throw an error if result is an exception', function () { |
| 34 | + expect(fn () => new Either(new Exception('foo')))->toThrow(InvalidArgumentException::class, 'Result cannot be a Throwable.'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('should throw an error if both result and error are null', function () { |
| 38 | + expect(fn () => new Either(null, null))->toThrow(InvalidArgumentException::class, 'Either result or error must be set.'); |
| 39 | +}); |
| 40 | + |
| 41 | +test('should have the correct methods', function () { |
| 42 | + expect(method_exists(Either::class, 'getIterator'))->toBeTrue() |
| 43 | + ->and(method_exists(Either::class, 'offsetSet'))->toBeTrue() |
| 44 | + ->and(method_exists(Either::class, 'offsetExists'))->toBeTrue() |
| 45 | + ->and(method_exists(Either::class, 'offsetUnset'))->toBeTrue() |
| 46 | + ->and(method_exists(Either::class, 'offsetGet'))->toBeTrue() |
| 47 | + ->and(method_exists(Either::class, 'create'))->toBeTrue(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('can be instantiated with new', function () { |
| 51 | + $either = new Either('foo'); |
| 52 | + |
| 53 | + expect($either)->toBeInstanceOf(Either::class) |
| 54 | + ->and($either->result)->toBe('foo') |
| 55 | + ->and($either->error)->toBeNull(); |
| 56 | + |
| 57 | + [$error, $result] = $either; |
| 58 | + |
| 59 | + expect($error)->toBeNull() |
| 60 | + ->and($result)->toBe('foo'); |
| 61 | + |
| 62 | + $either = new Either(null, new Exception('foo')); |
| 63 | + |
| 64 | + expect($either)->toBeInstanceOf(Either::class) |
| 65 | + ->and($either->result)->toBeNull() |
| 66 | + ->and($either->error)->toBeInstanceOf(Exception::class); |
| 67 | + |
| 68 | + [$error, $result] = $either; |
| 69 | + |
| 70 | + expect($error)->toBeInstanceOf(Exception::class) |
| 71 | + ->and($result)->toBeNull(); |
| 72 | +}); |
| 73 | + |
| 74 | +test('can be instantiated with from', function () { |
| 75 | + $either = Either::create('foo'); |
| 76 | + |
| 77 | + expect($either)->toBeInstanceOf(Either::class) |
| 78 | + ->and($either->result)->toBe('foo') |
| 79 | + ->and($either->error)->toBeNull(); |
| 80 | + |
| 81 | + [$error, $result] = $either; |
| 82 | + |
| 83 | + expect($error)->toBeNull() |
| 84 | + ->and($result)->toBe('foo'); |
| 85 | + |
| 86 | + $either = Either::create(null, new Exception('foo')); |
| 87 | + |
| 88 | + expect($either)->toBeInstanceOf(Either::class) |
| 89 | + ->and($either->result)->toBeNull() |
| 90 | + ->and($either->error)->toBeInstanceOf(Exception::class); |
| 91 | + |
| 92 | + [$error, $result] = $either; |
| 93 | + |
| 94 | + expect($error)->toBeInstanceOf(Exception::class) |
| 95 | + ->and($result)->toBeNull(); |
| 96 | +}); |
0 commit comments