Skip to content

Commit ece32cb

Browse files
committed
Added unit test for Base 16 implemntation.
1 parent 3e85e23 commit ece32cb

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

tests/base/test-base-16.php

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
<?php
2+
3+
/*******************************************************************\
4+
|* Author: Djordje Jocic *|
5+
|* Year: 2019 *|
6+
|* License: MIT License (MIT) *|
7+
|* =============================================================== *|
8+
|* Personal Website: http://www.djordjejocic.com/ *|
9+
|* =============================================================== *|
10+
|* Permission is hereby granted, free of charge, to any person *|
11+
|* obtaining a copy of this software and associated documentation *|
12+
|* files (the "Software"), to deal in the Software without *|
13+
|* restriction, including without limitation the rights to use, *|
14+
|* copy, modify, merge, publish, distribute, sublicense, and/or *|
15+
|* sell copies of the Software, and to permit persons to whom the *|
16+
|* Software is furnished to do so, subject to the following *|
17+
|* conditions. *|
18+
|* --------------------------------------------------------------- *|
19+
|* The above copyright notice and this permission notice shall be *|
20+
|* included in all copies or substantial portions of the Software. *|
21+
|* --------------------------------------------------------------- *|
22+
|* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *|
23+
|* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *|
24+
|* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *|
25+
|* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *|
26+
|* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *|
27+
|* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, RISING *|
28+
|* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *|
29+
|* OTHER DEALINGS IN THE SOFTWARE. *|
30+
\*******************************************************************/
31+
32+
use PHPUnit\Framework\TestCase;
33+
use Jocic\Encoders\Base\Base16;
34+
35+
/**
36+
* <i>TestBase16</i> class is used for testing method implementation of the
37+
* class <i>Base16</i>.
38+
*
39+
* @author Djordje Jocic <[email protected]>
40+
* @copyright 2019 All Rights Reserved
41+
* @version 1.0.0
42+
*/
43+
44+
class TestBase16 extends TestCase
45+
{
46+
/*********************\
47+
|* GET & SET METHODS *|
48+
\*********************/
49+
50+
// GET & SET METHODS GO HERE
51+
52+
/*****************\
53+
|* CHECK METHODS *|
54+
\*****************/
55+
56+
/**
57+
* Tests encoding validation for the <i>Base 16</i> implementation.
58+
*
59+
* @author Djordje Jocic <[email protected]>
60+
* @copyright 2019 All Rights Reserved
61+
* @version 1.0.0
62+
*
63+
* @return void
64+
*/
65+
66+
public function testValidation()
67+
{
68+
// Core Variables
69+
70+
$encoder = new Base16();
71+
72+
// Other Variables
73+
74+
$testValues = [
75+
"" => true,
76+
"66" => true,
77+
"666F6F" => true,
78+
"&43" => false,
79+
"=TeR=#" => false,
80+
"Uf934d" => false
81+
];
82+
83+
// Step 1 - Test Valid Values
84+
85+
foreach ($testValues as $testValue => $testResult)
86+
{
87+
88+
$this->assertSame($testResult,
89+
$encoder->isEncodingValid($testValue), $testValue);
90+
}
91+
}
92+
93+
/*******************\
94+
|* PRIMARY METHODS *|
95+
\*******************/
96+
97+
/**
98+
* Tests encoding process of the <i>Base 16</i> implementation.
99+
*
100+
* @author Djordje Jocic <[email protected]>
101+
* @copyright 2019 All Rights Reserved
102+
* @version 1.0.0
103+
*
104+
* @return void
105+
*/
106+
107+
public function testEncodingProcesses()
108+
{
109+
// Core Variables
110+
111+
$encoder = new Base16();
112+
113+
// Logic
114+
115+
$this->assertSame([
116+
6, 6, 6, 15, 6, 15
117+
], $encoder->convertInputToChunks("foo"));
118+
}
119+
120+
/**
121+
* Tests decoding process of the <i>Base 16</i> implementation.
122+
*
123+
* @author Djordje Jocic <[email protected]>
124+
* @copyright 2019 All Rights Reserved
125+
* @version 1.0.0
126+
*
127+
* @return void
128+
*/
129+
130+
public function testDecodingProcesses()
131+
{
132+
// Core Variables
133+
134+
$encoder = new Base16();
135+
136+
// Step 2 - Chunk Conversion
137+
138+
$this->assertSame([
139+
6, 6, 6, 15, 6, 15
140+
], $encoder->convertEncodingToChunks("666F6F"));
141+
142+
try
143+
{
144+
$encoder->convertEncodingToChunks("#");
145+
146+
$this->fail("Exception should've been thrown!");
147+
}
148+
catch (\Exception $e)
149+
{
150+
$this->assertEquals("Invalid encoding provided, it " .
151+
"can't be converted. Encoding: \"#\"", $e->getMessage());
152+
}
153+
}
154+
155+
/*********************\
156+
|* SECONDARY METHODS *|
157+
\*********************/
158+
159+
/**
160+
* Tests encoding of the <i>Base 16</i> implementation.
161+
*
162+
* @author Djordje Jocic <[email protected]>
163+
* @copyright 2019 All Rights Reserved
164+
* @version 1.0.0
165+
*
166+
* @return void
167+
*/
168+
169+
public function testEncoding()
170+
{
171+
// Core Variables
172+
173+
$encoder = new Base16();
174+
175+
// Logic.
176+
177+
$this->assertSame("", $encoder->encode(""));
178+
$this->assertSame("66", $encoder->encode("f"));
179+
$this->assertSame("666F", $encoder->encode("fo"));
180+
$this->assertSame("666F6F", $encoder->encode("foo"));
181+
$this->assertSame("666F6F62", $encoder->encode("foob"));
182+
$this->assertSame("666F6F6261", $encoder->encode("fooba"));
183+
$this->assertSame("666F6F626172", $encoder->encode("foobar"));
184+
$this->assertSame("666F6F62617231", $encoder->encode("foobar1"));
185+
$this->assertSame("666F6F6261723132", $encoder->encode("foobar12"));
186+
$this->assertSame("666F6F626172313233", $encoder->encode("foobar123"));
187+
}
188+
189+
/**
190+
* Tests decoding of the <i>Base 16</i> implementation.
191+
*
192+
* @author Djordje Jocic <[email protected]>
193+
* @copyright 2019 All Rights Reserved
194+
* @version 1.0.0
195+
*
196+
* @return void
197+
*/
198+
199+
public function testDecoding()
200+
{
201+
// Core Variables
202+
203+
$encoder = new Base16();
204+
205+
// Logic
206+
207+
$this->assertSame("", $encoder->decode(""));
208+
$this->assertSame("f", $encoder->decode("66"));
209+
$this->assertSame("fo", $encoder->decode("666F"));
210+
$this->assertSame("foo", $encoder->decode("666F6F"));
211+
$this->assertSame("foob", $encoder->decode("666F6F62"));
212+
$this->assertSame("fooba", $encoder->decode("666F6F6261"));
213+
$this->assertSame("foobar", $encoder->decode("666F6F626172"));
214+
$this->assertSame("foobar1", $encoder->decode("666F6F62617231"));
215+
$this->assertSame("foobar12", $encoder->decode("666F6F6261723132"));
216+
$this->assertSame("foobar123", $encoder->decode("666F6F626172313233"));
217+
}
218+
}
219+
220+
?>

0 commit comments

Comments
 (0)