|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Hashing; |
| 4 | + |
| 5 | +use ArrayIterator; |
| 6 | +use Illuminate\Foundation\Auth\User; |
| 7 | +use Illuminate\Hashing\Fingerprint; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class FingerprintTest extends TestCase |
| 12 | +{ |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + Fingerprint::$with = 'xxh3'; |
| 16 | + } |
| 17 | + |
| 18 | + public function test_hashes_string(): void |
| 19 | + { |
| 20 | + $fingerprint = Fingerprint::of('test'); |
| 21 | + |
| 22 | + $this->assertSame('nsn3kY19/EA=', $fingerprint->hash()); |
| 23 | + } |
| 24 | + |
| 25 | + public function test_hashes_stringable(): void |
| 26 | + { |
| 27 | + $fingerprint = Fingerprint::of(Str::of('test')); |
| 28 | + |
| 29 | + $this->assertSame('nsn3kY19/EA=', $fingerprint->hash()); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_hashes_array(): void |
| 33 | + { |
| 34 | + $fingerprint = Fingerprint::of(str_split('test')); |
| 35 | + |
| 36 | + $this->assertSame('TEO9OatCBX8=', $fingerprint->hash()); |
| 37 | + } |
| 38 | + |
| 39 | + public function test_hashes_resource(): void |
| 40 | + { |
| 41 | + $resource = fopen(__DIR__ . '/fixtures/fingerprintable.txt', 'r'); |
| 42 | + |
| 43 | + $fingerprint = Fingerprint::of($resource); |
| 44 | + |
| 45 | + $this->assertSame('6Eq/UI1GMuc=', $fingerprint->hash()); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_hashes_iterable(): void |
| 49 | + { |
| 50 | + $fingerprint = Fingerprint::of(new ArrayIterator(str_split('test'))); |
| 51 | + |
| 52 | + $this->assertSame('TEO9OatCBX8=', $fingerprint->hash()); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_hashes_model(): void |
| 56 | + { |
| 57 | + $fingerprint = Fingerprint::of((new User())->forceFill(['name' => 'test'])); |
| 58 | + |
| 59 | + $this->assertSame('ANVzoYpsoh0=', $fingerprint->hash()); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_hash_is_cached(): void |
| 63 | + { |
| 64 | + $changes = (new User())->forceFill(['name' => 'test']); |
| 65 | + |
| 66 | + $fingerprint = Fingerprint::of($changes); |
| 67 | + |
| 68 | + $this->assertSame('ANVzoYpsoh0=', $fingerprint->hash()); |
| 69 | + |
| 70 | + $changes->forceFill(['name' => 'other-test']); |
| 71 | + |
| 72 | + $this->assertSame('ANVzoYpsoh0=', $fingerprint->hash()); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_raw_hash(): void |
| 76 | + { |
| 77 | + $fingerprint = Fingerprint::of('test'); |
| 78 | + |
| 79 | + $this->assertSame(Str::fromBase64($fingerprint->hash()), $fingerprint->raw()); |
| 80 | + } |
| 81 | + |
| 82 | + public function test_serializes_into_string_as_hash(): void |
| 83 | + { |
| 84 | + $fingerprint = Fingerprint::of('test'); |
| 85 | + |
| 86 | + $this->assertSame('nsn3kY19/EA=', (string) $fingerprint); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_rehashes_the_same_value(): void |
| 90 | + { |
| 91 | + $changes = (new User())->forceFill(['name' => 'test']); |
| 92 | + |
| 93 | + $fingerprint = Fingerprint::of($changes); |
| 94 | + |
| 95 | + $this->assertSame('ANVzoYpsoh0=', $fingerprint->hash()); |
| 96 | + |
| 97 | + $changes->forceFill(['name' => 'other-test']); |
| 98 | + |
| 99 | + $this->assertSame('4Ginwb2rBAs=', $fingerprint->rehash()); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_uses_non_default_hashing_algorithm(): void |
| 103 | + { |
| 104 | + $fingerprint = Fingerprint::of('test', 'sha256'); |
| 105 | + |
| 106 | + $this->assertSame('n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=', $fingerprint->hash()); |
| 107 | + } |
| 108 | + |
| 109 | + public function test_uses_hashing_options(): void |
| 110 | + { |
| 111 | + $fingerprint = Fingerprint::of('test', 'xxh3', [ |
| 112 | + 'seed' => 'test' |
| 113 | + ]); |
| 114 | + |
| 115 | + $this->assertSame('nsn3kY19/EA=', $fingerprint->hash()); |
| 116 | + } |
| 117 | + |
| 118 | + public function test_changes_default_algorithm(): void |
| 119 | + { |
| 120 | + Fingerprint::$with = 'sha256'; |
| 121 | + |
| 122 | + $fingerprint = Fingerprint::of('test'); |
| 123 | + |
| 124 | + $this->assertSame('n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=', $fingerprint->hash()); |
| 125 | + } |
| 126 | + |
| 127 | + public function test_compares_an_equal_hash(): void |
| 128 | + { |
| 129 | + $fingerprint = Fingerprint::of('test'); |
| 130 | + |
| 131 | + $equal = $fingerprint->hash(); |
| 132 | + |
| 133 | + $this->assertTrue($fingerprint->is($equal)); |
| 134 | + $this->assertFalse($fingerprint->isNot($equal)); |
| 135 | + |
| 136 | + $this->assertTrue($fingerprint->is(Str::fromBase64($equal), false)); |
| 137 | + $this->assertFalse($fingerprint->isNot(Str::fromBase64($equal), false)); |
| 138 | + } |
| 139 | + |
| 140 | + public function test_compares_a_different_hash(): void |
| 141 | + { |
| 142 | + $fingerprint = Fingerprint::of('test'); |
| 143 | + |
| 144 | + $different = 'different'; |
| 145 | + |
| 146 | + $this->assertFalse($fingerprint->is($different)); |
| 147 | + $this->assertTrue($fingerprint->isNot($different)); |
| 148 | + |
| 149 | + $this->assertFalse($fingerprint->is(Str::fromBase64($different), false)); |
| 150 | + $this->assertTrue($fingerprint->isNot(Str::fromBase64($different), false)); |
| 151 | + } |
| 152 | + |
| 153 | + public function test_compares_an_equal_fingerprint_instance(): void |
| 154 | + { |
| 155 | + $fingerprint = Fingerprint::of('test'); |
| 156 | + |
| 157 | + $equal = Fingerprint::of('test'); |
| 158 | + |
| 159 | + $this->assertTrue($fingerprint->is($equal)); |
| 160 | + $this->assertFalse($fingerprint->isNot($equal)); |
| 161 | + |
| 162 | + $this->assertTrue($fingerprint->is(Str::fromBase64($equal), false)); |
| 163 | + $this->assertFalse($fingerprint->isNot(Str::fromBase64($equal), false)); |
| 164 | + } |
| 165 | + |
| 166 | + public function test_compares_a_different_fingerprint_instance(): void |
| 167 | + { |
| 168 | + $fingerprint = Fingerprint::of('test'); |
| 169 | + |
| 170 | + $different = Fingerprint::of('different'); |
| 171 | + |
| 172 | + $this->assertFalse($fingerprint->is($different)); |
| 173 | + $this->assertTrue($fingerprint->isNot($different)); |
| 174 | + |
| 175 | + $this->assertFalse($fingerprint->is(Str::fromBase64($different), false)); |
| 176 | + $this->assertTrue($fingerprint->isNot(Str::fromBase64($different), false)); |
| 177 | + } |
| 178 | +} |
0 commit comments