|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Swis\Laravel\Encrypted\Tests\Casts; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Swis\Laravel\Encrypted\Casts\AsEncryptedBoolean; |
| 7 | +use Swis\Laravel\Encrypted\Tests\_mocks\DummyEncrypter; |
| 8 | +use Swis\Laravel\Encrypted\Tests\_mocks\Model; |
| 9 | + |
| 10 | +class AsEncryptedBooleanTest extends TestCase |
| 11 | +{ |
| 12 | + protected function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + Model::$encrypter = new DummyEncrypter(); |
| 16 | + } |
| 17 | + |
| 18 | + public function testGetReturnsNullForNull(): void |
| 19 | + { |
| 20 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 21 | + $model = new Model(); |
| 22 | + |
| 23 | + $result = $cast->get($model, 'flag', null, []); |
| 24 | + |
| 25 | + $this->assertNull($result); |
| 26 | + } |
| 27 | + |
| 28 | + public function testGetReturnsBoolean(): void |
| 29 | + { |
| 30 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 31 | + $model = new Model(); |
| 32 | + |
| 33 | + $encryptedTrue = Model::$encrypter->encrypt('1', false); |
| 34 | + $encryptedFalse = Model::$encrypter->encrypt('0', false); |
| 35 | + |
| 36 | + $this->assertTrue($cast->get($model, 'flag', $encryptedTrue, [])); |
| 37 | + $this->assertFalse($cast->get($model, 'flag', $encryptedFalse, [])); |
| 38 | + } |
| 39 | + |
| 40 | + public function testSetReturnsNullForNull(): void |
| 41 | + { |
| 42 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 43 | + $model = new Model(); |
| 44 | + |
| 45 | + $result = $cast->set($model, 'flag', null, []); |
| 46 | + |
| 47 | + $this->assertNull($result); |
| 48 | + } |
| 49 | + |
| 50 | + public function testSetReturnsEncryptedString(): void |
| 51 | + { |
| 52 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 53 | + $model = new Model(); |
| 54 | + |
| 55 | + $encrypted = $cast->set($model, 'flag', true, []); |
| 56 | + $this->assertEquals(Model::$encrypter->encrypt('1', false), $encrypted); |
| 57 | + |
| 58 | + $encrypted = $cast->set($model, 'flag', false, []); |
| 59 | + $this->assertEquals(Model::$encrypter->encrypt('0', false), $encrypted); |
| 60 | + } |
| 61 | + |
| 62 | + public function testCompareReturnsTrueForSameDecryptedValue(): void |
| 63 | + { |
| 64 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 65 | + $model = new Model(); |
| 66 | + |
| 67 | + $encryptedTrue = Model::$encrypter->encrypt('1', false); |
| 68 | + $encryptedTrue2 = Model::$encrypter->encrypt('1', false); |
| 69 | + |
| 70 | + $this->assertTrue($cast->compare($model, 'flag', $encryptedTrue, $encryptedTrue2)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testCompareReturnsFalseForDifferentDecryptedValue(): void |
| 74 | + { |
| 75 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 76 | + $model = new Model(); |
| 77 | + |
| 78 | + $encryptedTrue = Model::$encrypter->encrypt('1', false); |
| 79 | + $encryptedFalse = Model::$encrypter->encrypt('0', false); |
| 80 | + |
| 81 | + $this->assertFalse($cast->compare($model, 'flag', $encryptedTrue, $encryptedFalse)); |
| 82 | + } |
| 83 | + |
| 84 | + public function testCompareReturnsFalseIfPreviousKeysExist(): void |
| 85 | + { |
| 86 | + $cast = AsEncryptedBoolean::castUsing([]); |
| 87 | + $model = new Model(); |
| 88 | + |
| 89 | + // Simulate previous keys |
| 90 | + /* @noinspection PhpUndefinedMethodInspection */ |
| 91 | + $model::$encrypter->setPreviousKeys(['dummy-key']); |
| 92 | + |
| 93 | + $encrypted = Model::$encrypter->encrypt('1', false); |
| 94 | + |
| 95 | + $this->assertFalse($cast->compare($model, 'flag', $encrypted, $encrypted)); |
| 96 | + } |
| 97 | +} |
0 commit comments