meta: add tests/Ciphers; change ciphers dir to Ciphers for consistency

This commit is contained in:
Brandon Johnson 2022-07-23 20:40:26 -06:00
parent edb7ef5d54
commit 69e86faafc
8 changed files with 33 additions and 45 deletions

View File

@ -37,7 +37,7 @@ function decrypt(string $text, int $shift): string
if (ctype_alpha($c)) {
$placeValue = ord($c) - ord(ctype_upper($c) ? 'A' : 'a'); // Getting value of character (i.e. 0-25)
$placeValue = ($placeValue - $shift) % 26; // Applying decryption formula
if ($placeValue < 0) { // Handling case where remainder is negative
if ($placeValue < 0) { // Handling case where remainder is negative
$placeValue += 26;
}
$placeValue += ord(ctype_upper($c) ? 'A' : 'a');

View File

@ -104,7 +104,7 @@ function decode(string $text): string
"-----" => "0",
"/" => " "
);
$decodedText = ""; // Stores the decoded text
foreach (explode(" ", $text) as $c) { // Going through each group
if (array_key_exists($c, $MORSE_CODE)) { // Checks if it is a valid character

View File

@ -1,22 +0,0 @@
<?php
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertNotEquals;
use function PHPUnit\Framework\assertTrue;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../ciphers/XORCipher.php';
class CipherTest extends TestCase
{
public function testXor_cipher()
{
$inp_str = "test@string";
$key = "test-key";
$invalid_key = "wrong-key";
assertEquals( $inp_str, xor_cipher( xor_cipher( $inp_str , $key) , $key));
assertNotEquals( $inp_str, xor_cipher( xor_cipher( $inp_str , $key) , $invalid_key));
}
}

28
tests/Ciphers/CiphersTest.php Executable file
View File

@ -0,0 +1,28 @@
<?php
use function PHPUnit\Framework\assertEquals;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Ciphers/CaesarCipher.php';
require_once __DIR__ . '/../../Ciphers/XORCipher.php';
class CiphersTest extends TestCase
{
public function testCaesarCipher()
{
assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7));
assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7 + 26));
assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7));
assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7 + 26));
}
public function testXorCipher()
{
$input_str = "test@string";
$key = "test-key";
$invalid_key = "wrong-key";
assertEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $key));
assertNotEquals( $input_str, xorCipher( xorCipher( $input_str , $key) , $invalid_key));
}
}

View File

@ -3,12 +3,12 @@
use function PHPUnit\Framework\assertEquals;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../ciphers/morseCode.php';
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Ciphers/MorseCode.php';
class MorseCodeTest extends TestCase
{
public function testCaesarCipher()
public function testMorseCodeCipher()
{
assertEquals('TEST 123', decode(encode('TEST 123')));
}

View File

@ -1,18 +0,0 @@
<?php
use function PHPUnit\Framework\assertEquals;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../ciphers/caesarCipher.php';
class CiphersTest extends TestCase
{
public function testCaesarCipher()
{
assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7));
assertEquals('Aopz pz h alza.', encrypt('This is a test.', 7 + 26));
assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7));
assertEquals('This is a test.', decrypt('Aopz pz h alza.', 7 + 26));
}
}