|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Validator; |
| 6 | +use Illuminate\Support\Stringable; |
| 7 | +use Illuminate\Validation\ValidationException; |
| 8 | +use MichaelRubel\ValueObjects\Collection\Complex\Url; |
| 9 | + |
| 10 | +test('can instantiate valid url', function () { |
| 11 | + $url = new Url('test-url'); |
| 12 | + $this->assertSame('http://localhost/test-url', $url->value()); |
| 13 | +}); |
| 14 | + |
| 15 | +test('can url accepts query string', function () { |
| 16 | + $url = new Url('test-url?query=test&string=test2'); |
| 17 | + $this->assertSame('http://localhost/test-url?query=test&string=test2', $url->value()); |
| 18 | +}); |
| 19 | + |
| 20 | +test('can url accepts full url', function () { |
| 21 | + $url = new Url('https://example.com/test-url?query=test&string=test2'); |
| 22 | + $this->assertSame('https://example.com/test-url?query=test&string=test2', $url->value()); |
| 23 | +}); |
| 24 | + |
| 25 | +test('cannot instantiate invalid url', function () { |
| 26 | + $this->expectException(ValidationException::class); |
| 27 | + |
| 28 | + new Url(' Test Url '); |
| 29 | +}); |
| 30 | + |
| 31 | +test('cannot instantiate invalid url with try/catch', function () { |
| 32 | + try { |
| 33 | + new Url(' Test Url '); |
| 34 | + } catch (ValidationException $exception) { |
| 35 | + $this->assertSame('Your URL is invalid.', $exception->getMessage()); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +test('can cast url to string', function () { |
| 40 | + $url = new Url('test-url'); |
| 41 | + $this->assertSame('http://localhost/test-url', (string) $url); |
| 42 | +}); |
| 43 | + |
| 44 | +test('url cannot accept null', function () { |
| 45 | + $this->expectException(\TypeError::class); |
| 46 | + |
| 47 | + new Url(null); |
| 48 | +}); |
| 49 | + |
| 50 | +test('url fails when no argument passed', function () { |
| 51 | + $this->expectException(\TypeError::class); |
| 52 | + |
| 53 | + new Url(); |
| 54 | +}); |
| 55 | + |
| 56 | +test('url fails when empty string passed', function () { |
| 57 | + $this->expectException(\InvalidArgumentException::class); |
| 58 | + |
| 59 | + new Url(''); |
| 60 | +}); |
| 61 | + |
| 62 | +test('url is makeable', function () { |
| 63 | + $valueObject = Url::make('1'); |
| 64 | + $this->assertSame('http://localhost/1', $valueObject->value()); |
| 65 | +}); |
| 66 | + |
| 67 | +test('url is macroable', function () { |
| 68 | + Url::macro('str', function () { |
| 69 | + return str($this->value()); |
| 70 | + }); |
| 71 | + |
| 72 | + $valueObject = new Url('test-url'); |
| 73 | + |
| 74 | + $this->assertTrue($valueObject->str()->is('http://localhost/test-url')); |
| 75 | +}); |
| 76 | + |
| 77 | +test('url is conditionable', function () { |
| 78 | + $valueObject = new Url('1'); |
| 79 | + $this->assertSame('http://localhost/1', $valueObject->when(true)->value()); |
| 80 | + $this->assertSame($valueObject, $valueObject->when(false)->value()); |
| 81 | +}); |
| 82 | + |
| 83 | +test('url is arrayable', function () { |
| 84 | + $array = (new Url('test-url'))->toArray(); |
| 85 | + $this->assertSame(['http://localhost/test-url'], $array); |
| 86 | +}); |
| 87 | + |
| 88 | +test('url is stringable', function () { |
| 89 | + $valueObject = new Url('test-url'); |
| 90 | + $this->assertSame('http://localhost/test-url', (string) $valueObject); |
| 91 | + |
| 92 | + $valueObject = new Url('test-url'); |
| 93 | + $this->assertSame('http://localhost/test-url', $valueObject->toString()); |
| 94 | +}); |
| 95 | + |
| 96 | +test('url has immutable properties', function () { |
| 97 | + $this->expectException(\InvalidArgumentException::class); |
| 98 | + $valueObject = new Url('lorem-ipsum'); |
| 99 | + $this->assertSame('http://localhost/lorem-ipsum', $valueObject->value); |
| 100 | + $valueObject->value = 'immutable'; |
| 101 | +}); |
| 102 | + |
| 103 | +test('url has immutable constructor', function () { |
| 104 | + $this->expectException(\InvalidArgumentException::class); |
| 105 | + $valueObject = new Url('test-url'); |
| 106 | + $valueObject->__construct(' Lorem ipsum '); |
| 107 | +}); |
| 108 | + |
| 109 | +test('can extend protected methods in url', function () { |
| 110 | + $email = new TestUrl('test-url'); |
| 111 | + $this->assertSame(['required', 'url'], $email->validationRules()); |
| 112 | +}); |
| 113 | + |
| 114 | +class TestUrl extends Url |
| 115 | +{ |
| 116 | + /** |
| 117 | + * Create a new instance of the value object. |
| 118 | + * |
| 119 | + * @param string|Stringable $value |
| 120 | + */ |
| 121 | + public function __construct(string|Stringable $value) |
| 122 | + { |
| 123 | + parent::__construct($value); |
| 124 | + |
| 125 | + $this->value = url($value); |
| 126 | + |
| 127 | + $validator = Validator::make( |
| 128 | + ['url' => $this->value()], |
| 129 | + ['url' => $this->validationRules()], |
| 130 | + ); |
| 131 | + |
| 132 | + if ($validator->fails()) { |
| 133 | + throw ValidationException::withMessages([__('Your URL is invalid.')]); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public function validationRules(): array |
| 138 | + { |
| 139 | + return parent::validationRules(); |
| 140 | + } |
| 141 | +} |
0 commit comments