Skip to content

Commit b22a108

Browse files
committed
[TEST] Add PHPStan level 10, optimise Unit Test
1 parent 4b00784 commit b22a108

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939

4040
- name: Install dependencies
4141
run: composer install --prefer-dist
42+
43+
- name: Run PHPStan
44+
run: vendor/bin/phpstan analyse -l 10 source/ tests/
4245

4346
- name: Run unit tests
4447
run: vendor/bin/phpunit --no-coverage tests/

composer.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/BasicVigenereCipher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @author Fahmi Auliya Tsani <[email protected]>
1111
* @version 1.1
1212
*/
13+
#[\AllowDynamicProperties]
1314
class BasicVigenereCipher extends VigenereCipherBlueprint
1415
{
1516
public function __construct(

source/VigenereCipher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function getClassName(string $mode): string
1919
return $path . $className;
2020
}
2121

22-
public static function encrypt(string $data, string $key, string $mode = 'basic'): string|null
22+
public static function encrypt(string $data, string $key, string $mode = 'basic'): string
2323
{
2424
$className = self::getClassName($mode);
2525

@@ -31,7 +31,7 @@ public static function encrypt(string $data, string $key, string $mode = 'basic'
3131
return $encrypt->cipherText;
3232
}
3333

34-
public static function decrypt(string $data, string $key, string $mode = 'basic'): string|null
34+
public static function decrypt(string $data, string $key, string $mode = 'basic'): string
3535
{
3636
$className = self::getClassName($mode);
3737

source/VigenereCipherBlueprint.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use amculin\cryptography\classic\enums\ProcessType;
55

6+
#[\AllowDynamicProperties]
67
abstract class VigenereCipherBlueprint
78
{
89
/**
@@ -25,7 +26,7 @@ abstract class VigenereCipherBlueprint
2526
*
2627
* @var string
2728
*/
28-
public string $plainText;
29+
public string $plainText = '';
2930

3031
/**
3132
* The key used to encrypt plain text/message
@@ -47,7 +48,7 @@ abstract class VigenereCipherBlueprint
4748
*
4849
* @var string
4950
*/
50-
public string $cipherText;
51+
public string $cipherText = '';
5152

5253
/**
5354
* Method to validate key, plainText, and cipherText
@@ -127,7 +128,7 @@ public function setProcess(string $process): void
127128
*
128129
* @return string
129130
*/
130-
public function getPlainText(): string|null
131+
public function getPlainText(): string
131132
{
132133
return $this->plainText;
133134
}
@@ -171,7 +172,7 @@ public function setKey(string $key): void
171172
*
172173
* @return string
173174
*/
174-
public function getCipherText(): string|null
175+
public function getCipherText(): string
175176
{
176177
return $this->cipherText;
177178
}

tests/VIgenereCipherTest.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ public function testCanEncryptInBasicMode():void
1515
$data = 'encryptionprocess';
1616
$key = 'thekey';
1717

18-
$encrypted = VigenereCipher::encrypt($data, $key, VigenereMode::BASIC->value);
18+
$encrypted = (string) VigenereCipher::encrypt($data, $key, VigenereMode::BASIC->value);
1919

2020
$this->assertEquals(strlen($data), strlen($encrypted));
2121
$this->assertEquals('xugbcnmpsxtphjicw', $encrypted);
2222
$this->assertNotEquals($data, $encrypted);
23-
$this->assertIsString($data);
24-
$this->assertIsString($key);
25-
$this->assertIsString($encrypted);
2623
$this->assertMatchesRegularExpression($allowedChars, $data);
2724
$this->assertMatchesRegularExpression($allowedChars, $key);
2825
$this->assertMatchesRegularExpression($allowedChars, $encrypted);
@@ -35,7 +32,7 @@ public function testCanNotEncryptInBasicModeWithInvalidKey():void
3532

3633
$encrypted = VigenereCipher::encrypt($data, $key, VigenereMode::BASIC->value);
3734

38-
$this->assertNull($encrypted);
35+
$this->assertEquals('', $encrypted);
3936
}
4037

4138
public function testCanDecryptInBasicMode():void
@@ -44,14 +41,11 @@ public function testCanDecryptInBasicMode():void
4441
$data = 'xugbcnmpsxtphjicw';
4542
$key = 'thekey';
4643

47-
$decrypted = VigenereCipher::decrypt($data, $key, VigenereMode::BASIC->value);
44+
$decrypted = (string) VigenereCipher::decrypt($data, $key, VigenereMode::BASIC->value);
4845

4946
$this->assertEquals(strlen($data), strlen($decrypted));
5047
$this->assertEquals('encryptionprocess', $decrypted);
5148
$this->assertNotEquals($data, $decrypted);
52-
$this->assertIsString($data);
53-
$this->assertIsString($key);
54-
$this->assertIsString($decrypted);
5549
$this->assertMatchesRegularExpression($allowedChars, $data);
5650
$this->assertMatchesRegularExpression($allowedChars, $key);
5751
$this->assertMatchesRegularExpression($allowedChars, $decrypted);
@@ -64,7 +58,7 @@ public function testCanNotDecryptInBasicModeWithInvalidKey():void
6458

6559
$encrypted = VigenereCipher::decrypt($data, $key, VigenereMode::BASIC->value);
6660

67-
$this->assertNull($encrypted);
61+
$this->assertEquals('', $encrypted);
6862
}
6963

7064
public function testCanEncryptInAlphaNumericMode():void
@@ -78,9 +72,6 @@ public function testCanEncryptInAlphaNumericMode():void
7872
$this->assertEquals(strlen($data), strlen($encrypted));
7973
$this->assertEquals('Xu5B2NMpTxjPHJWCz', $encrypted);
8074
$this->assertNotEquals($data, $encrypted);
81-
$this->assertIsString($data);
82-
$this->assertIsString($key);
83-
$this->assertIsString($encrypted);
8475
$this->assertMatchesRegularExpression($allowedChars, $data);
8576
$this->assertMatchesRegularExpression($allowedChars, $key);
8677
$this->assertMatchesRegularExpression($allowedChars, $encrypted);
@@ -93,7 +84,7 @@ public function testCanNotEncryptInAlphaNumericModeWithInvalidKey():void
9384

9485
$encrypted = VigenereCipher::encrypt($data, $key, VigenereMode::ALPHA_NUMERIC->value);
9586

96-
$this->assertNull($encrypted);
87+
$this->assertEquals('', $encrypted);
9788
}
9889

9990
public function testCanDecryptWithAlphaNumericMode():void
@@ -107,9 +98,6 @@ public function testCanDecryptWithAlphaNumericMode():void
10798
$this->assertEquals(strlen($data), strlen($decrypted));
10899
$this->assertEquals('Encrypti0nProC3s5', $decrypted);
109100
$this->assertNotEquals($data, $decrypted);
110-
$this->assertIsString($data);
111-
$this->assertIsString($key);
112-
$this->assertIsString($decrypted);
113101
$this->assertMatchesRegularExpression($allowedChars, $data);
114102
$this->assertMatchesRegularExpression($allowedChars, $key);
115103
$this->assertMatchesRegularExpression($allowedChars, $decrypted);
@@ -122,7 +110,7 @@ public function testCanNotDecryptInAlphaNumericModeWithInvalidKey():void
122110

123111
$encrypted = VigenereCipher::decrypt($data, $key, VigenereMode::ALPHA_NUMERIC->value);
124112

125-
$this->assertNull($encrypted);
113+
$this->assertEquals('', $encrypted);
126114
}
127115

128116
public function testCanGetBasicVigenereClass(): void
@@ -132,7 +120,6 @@ public function testCanGetBasicVigenereClass(): void
132120

133121
$className = VigenereCipher::getClassName($mode);
134122

135-
$this->assertIsString($className);
136123
$this->assertEquals($path . 'BasicVigenereCipher', $className);
137124
}
138125

@@ -143,7 +130,6 @@ public function testCanGetAlphaNumericVigenereClass(): void
143130

144131
$className = VigenereCipher::getClassName($mode);
145132

146-
$this->assertIsString($className);
147133
$this->assertEquals($path . 'AlnumVigenereCipher', $className);
148134
}
149135
}

0 commit comments

Comments
 (0)