|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Brainbits\FunctionalTestHelpers\Tests\Uuid; |
| 6 | + |
| 7 | +use Brainbits\FunctionalTestHelpers\Uuid\UuidTrait; |
| 8 | +use PHPUnit\Framework\ExpectationFailedException; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Uid\Uuid; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +use function Safe\json_encode; |
| 14 | + |
| 15 | +/** @covers \Brainbits\FunctionalTestHelpers\Uuid\UuidTrait */ |
| 16 | +final class UuidTraitTest extends TestCase |
| 17 | +{ |
| 18 | + use UuidTrait; |
| 19 | + |
| 20 | + public function testNextUuid(): void |
| 21 | + { |
| 22 | + self::assertEquals('00000000-0000-0000-0000-000000000001', $this->nextUuid()); |
| 23 | + self::assertEquals('00000000-0000-0000-0000-000000000002', $this->nextUuid()); |
| 24 | + self::assertEquals('00000000-0000-0000-0000-000000000003', $this->nextUuid()); |
| 25 | + self::assertEquals('00000000-0000-0000-0000-000000000004', $this->nextUuid()); |
| 26 | + self::assertEquals('00000000-0000-0000-0000-000000000005', $this->nextUuid()); |
| 27 | + self::assertEquals('00000000-0000-0000-0000-000000000006', $this->nextUuid()); |
| 28 | + self::assertEquals('00000000-0000-0000-0000-000000000007', $this->nextUuid()); |
| 29 | + self::assertEquals('00000000-0000-0000-0000-000000000008', $this->nextUuid()); |
| 30 | + self::assertEquals('00000000-0000-0000-0000-000000000009', $this->nextUuid()); |
| 31 | + self::assertEquals('00000000-0000-0000-0000-00000000000a', $this->nextUuid()); |
| 32 | + self::assertEquals('00000000-0000-0000-0000-00000000000b', $this->nextUuid()); |
| 33 | + self::assertEquals('00000000-0000-0000-0000-00000000000c', $this->nextUuid()); |
| 34 | + self::assertEquals('00000000-0000-0000-0000-00000000000d', $this->nextUuid()); |
| 35 | + self::assertEquals('00000000-0000-0000-0000-00000000000e', $this->nextUuid()); |
| 36 | + self::assertEquals('00000000-0000-0000-0000-00000000000f', $this->nextUuid()); |
| 37 | + self::assertEquals('00000000-0000-0000-0000-000000000010', $this->nextUuid()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAssertIsUuid(): void |
| 41 | + { |
| 42 | + $uuid = (string) Uuid::v4(); |
| 43 | + $noUuid = 'foo'; |
| 44 | + |
| 45 | + self::assertIsUuid($uuid); |
| 46 | + |
| 47 | + try { |
| 48 | + self::assertIsUuid($noUuid); |
| 49 | + |
| 50 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 51 | + } catch (ExpectationFailedException) { |
| 52 | + // @ignoreException |
| 53 | + } catch (Throwable) { |
| 54 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function testAssertAndReplaceUuidInJson(): void |
| 59 | + { |
| 60 | + $json = json_encode([ |
| 61 | + 'foo' => (string) Uuid::v4(), |
| 62 | + 'bar' => 'baz', |
| 63 | + ]); |
| 64 | + |
| 65 | + $replacedJson = self::assertAndReplaceUuidInJson($json, 'foo'); |
| 66 | + |
| 67 | + self::assertJson($replacedJson); |
| 68 | + self::assertJsonStringEqualsJsonString( |
| 69 | + '{"foo":"00000000-0000-0000-0000-000000000000","bar":"baz"}', |
| 70 | + $replacedJson, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testAssertAndReplaceUuidInJsonWithNullUuidValueDoesNotReplaceUuid(): void |
| 75 | + { |
| 76 | + $json = json_encode([ |
| 77 | + 'foo' => null, |
| 78 | + 'bar' => 'baz', |
| 79 | + ]); |
| 80 | + |
| 81 | + $replacedJson = self::assertAndReplaceUuidInJson($json, 'foo'); |
| 82 | + |
| 83 | + self::assertJson($replacedJson); |
| 84 | + self::assertJsonStringEqualsJsonString( |
| 85 | + '{"foo":null,"bar":"baz"}', |
| 86 | + $replacedJson, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public function testAssertAndReplaceUuidInJsonFailsOnInvalidJson(): void |
| 91 | + { |
| 92 | + $json = 'foo'; |
| 93 | + |
| 94 | + try { |
| 95 | + self::assertAndReplaceUuidInJson($json, 'foo'); |
| 96 | + |
| 97 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 98 | + } catch (ExpectationFailedException $e) { |
| 99 | + self::assertSame( |
| 100 | + 'Failed asserting that \'foo\' is valid JSON (Syntax error, malformed JSON).', |
| 101 | + $e->getMessage(), |
| 102 | + ); |
| 103 | + } catch (Throwable) { |
| 104 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public function testAssertAndReplaceUuidInJsonFailsOnInvalidUuid(): void |
| 109 | + { |
| 110 | + $json = json_encode([ |
| 111 | + 'foo' => 'xxx', |
| 112 | + 'bar' => 'baz', |
| 113 | + ]); |
| 114 | + |
| 115 | + try { |
| 116 | + self::assertAndReplaceUuidInJson($json, 'foo'); |
| 117 | + |
| 118 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 119 | + } catch (ExpectationFailedException $e) { |
| 120 | + self::assertSame( |
| 121 | + 'Failed asserting that false is true.', |
| 122 | + $e->getMessage(), |
| 123 | + ); |
| 124 | + } catch (Throwable) { |
| 125 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public function testAssertAndReplaceUuidInArray(): void |
| 130 | + { |
| 131 | + $data = [ |
| 132 | + 'foo' => (string) Uuid::v4(), |
| 133 | + 'bar' => 'baz', |
| 134 | + ]; |
| 135 | + |
| 136 | + $replacedData = self::assertAndReplaceUuidInArray($data, 'foo'); |
| 137 | + |
| 138 | + self::assertIsArray($replacedData); |
| 139 | + self::assertSame( |
| 140 | + ['foo' => '00000000-0000-0000-0000-000000000000', 'bar' => 'baz'], |
| 141 | + $replacedData, |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + public function testAssertAndReplaceUuidInArrayWithNullUuidValueDoesNotReplaceUuid(): void |
| 146 | + { |
| 147 | + $data = [ |
| 148 | + 'foo' => null, |
| 149 | + 'bar' => 'baz', |
| 150 | + ]; |
| 151 | + |
| 152 | + $replacedData = self::assertAndReplaceUuidInArray($data, 'foo'); |
| 153 | + |
| 154 | + self::assertIsArray($replacedData); |
| 155 | + self::assertSame( |
| 156 | + ['foo' => null, 'bar' => 'baz'], |
| 157 | + $replacedData, |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + public function testAssertAndReplaceUuidInArrayFailsOnInvalidJson(): void |
| 162 | + { |
| 163 | + $data = 'foo'; |
| 164 | + |
| 165 | + try { |
| 166 | + self::assertAndReplaceUuidInArray($data, 'foo'); |
| 167 | + |
| 168 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 169 | + } catch (ExpectationFailedException $e) { |
| 170 | + self::assertSame('Failed asserting that \'foo\' is of type array.', $e->getMessage()); |
| 171 | + } catch (Throwable) { |
| 172 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public function testAssertAndReplaceUuidInArrayFailsOnInvalidUuid(): void |
| 177 | + { |
| 178 | + $data = [ |
| 179 | + 'foo' => 'xxx', |
| 180 | + 'bar' => 'baz', |
| 181 | + ]; |
| 182 | + |
| 183 | + try { |
| 184 | + self::assertAndReplaceUuidInArray($data, 'foo'); |
| 185 | + |
| 186 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 187 | + } catch (ExpectationFailedException $e) { |
| 188 | + self::assertSame( |
| 189 | + 'Failed asserting that false is true.', |
| 190 | + $e->getMessage(), |
| 191 | + ); |
| 192 | + } catch (Throwable) { |
| 193 | + self::fail('Expected ExpectationFailedException exception was not thrown'); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments