|
| 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\Base64; |
| 34 | + |
| 35 | + /** |
| 36 | + * <i>TestBase64</i> class is used for testing method implementation of the |
| 37 | + * class <i>Base64</i>. |
| 38 | + * |
| 39 | + * @author Djordje Jocic <[email protected]> |
| 40 | + * @copyright 2019 All Rights Reserved |
| 41 | + * @version 1.0.0 |
| 42 | + */ |
| 43 | + |
| 44 | + class TestBase64 extends TestCase |
| 45 | + { |
| 46 | + /*********************\ |
| 47 | + |* GET & SET METHODS *| |
| 48 | + \*********************/ |
| 49 | + |
| 50 | + /** |
| 51 | + * Tests <i>getBaseTableMethod</i> method for the <i>Base 64</i> |
| 52 | + * implementation. |
| 53 | + * |
| 54 | + * @author Djordje Jocic <[email protected]> |
| 55 | + * @copyright 2019 All Rights Reserved |
| 56 | + * @version 1.0.0 |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + |
| 61 | + public function testGetBaseTableMethod() |
| 62 | + { |
| 63 | + // Core Variables |
| 64 | + |
| 65 | + $encoder = new Base64(); |
| 66 | + |
| 67 | + // Logic |
| 68 | + |
| 69 | + $this->assertSame([ |
| 70 | + "A", "B", "C", "D", "E", "F", "G", "H", "I", |
| 71 | + "J", "K", "L", "M", "N", "O", "P", "Q", "R", |
| 72 | + "S", "T", "U", "V", "W", "X", "Y", "Z", "a", |
| 73 | + "b", "c", "d", "e", "f", "g", "h", "i", "j", |
| 74 | + "k", "l", "m", "n", "o", "p", "q", "r", "s", |
| 75 | + "t", "u", "v", "w", "x", "y", "z", "0", "1", |
| 76 | + "2", "3", "4", "5", "6", "7", "8", "9", "+", |
| 77 | + "/" |
| 78 | + ], $encoder->getBaseTable()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Tests <i>getBasePadding</i> method for the <i>Base 64</i> |
| 83 | + * implementation. |
| 84 | + * |
| 85 | + * @author Djordje Jocic <[email protected]> |
| 86 | + * @copyright 2019 All Rights Reserved |
| 87 | + * @version 1.0.0 |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + |
| 92 | + public function testGetBasePaddingMethod() |
| 93 | + { |
| 94 | + // Core Variables |
| 95 | + |
| 96 | + $encoder = new Base64(); |
| 97 | + |
| 98 | + // Logic |
| 99 | + |
| 100 | + $this->assertSame("=", $encoder->getBasePadding()); |
| 101 | + } |
| 102 | + |
| 103 | + /*****************\ |
| 104 | + |* CHECK METHODS *| |
| 105 | + \*****************/ |
| 106 | + |
| 107 | + /** |
| 108 | + * Tests encoding validation for the <i>Base 64</i> implementation. |
| 109 | + * |
| 110 | + * @author Djordje Jocic <[email protected]> |
| 111 | + * @copyright 2019 All Rights Reserved |
| 112 | + * @version 1.0.0 |
| 113 | + * |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + |
| 117 | + public function testValidation() |
| 118 | + { |
| 119 | + // Core Variables |
| 120 | + |
| 121 | + $encoder = new Base64(); |
| 122 | + |
| 123 | + // Other Variables |
| 124 | + |
| 125 | + $testValues = [ |
| 126 | + "" => true, |
| 127 | + "Zg==" => true, |
| 128 | + "Zm9vYg==" => true, |
| 129 | + "&43" => false, |
| 130 | + "==T~R==f" => false, |
| 131 | + "Uf9@4FER" => false |
| 132 | + ]; |
| 133 | + |
| 134 | + // Step 1 - Test Valid Values |
| 135 | + |
| 136 | + foreach ($testValues as $testValue => $testResult) |
| 137 | + { |
| 138 | + $this->assertSame($testResult, |
| 139 | + $encoder->isEncodingValid($testValue), $testValue); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /*******************\ |
| 144 | + |* PRIMARY METHODS *| |
| 145 | + \*******************/ |
| 146 | + |
| 147 | + /** |
| 148 | + * Tests encoding process of the <i>Base 64</i> implementation. |
| 149 | + * |
| 150 | + * @author Djordje Jocic <[email protected]> |
| 151 | + * @copyright 2019 All Rights Reserved |
| 152 | + * @version 1.0.0 |
| 153 | + * |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + |
| 157 | + public function testEncodingProcesses() |
| 158 | + { |
| 159 | + // Core Variables |
| 160 | + |
| 161 | + $encoder = new Base64(); |
| 162 | + |
| 163 | + // Step 1 - Apply Padding |
| 164 | + |
| 165 | + $this->assertSame("Zg==", $encoder->applyPadding("Zg")); |
| 166 | + |
| 167 | + try |
| 168 | + { |
| 169 | + $encoder->applyPadding("#"); |
| 170 | + |
| 171 | + $this->fail("Exception should've been thrown!"); |
| 172 | + } |
| 173 | + catch (\Exception $e) |
| 174 | + { |
| 175 | + $this->assertEquals("Invalid encoding provided, padding" . |
| 176 | + " can't be applied. Encoding: \"#\"", $e->getMessage()); |
| 177 | + } |
| 178 | + |
| 179 | + // Step 2 - Chunk Conversion |
| 180 | + |
| 181 | + $this->assertSame([ |
| 182 | + 25, 38, 61, 47 |
| 183 | + ], $encoder->convertInputToChunks("foo")); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Tests decoding process of the <i>Base 64</i> implementation. |
| 188 | + * |
| 189 | + * @author Djordje Jocic <[email protected]> |
| 190 | + * @copyright 2019 All Rights Reserved |
| 191 | + * @version 1.0.0 |
| 192 | + * |
| 193 | + * @return void |
| 194 | + */ |
| 195 | + |
| 196 | + public function testDecodingProcesses() |
| 197 | + { |
| 198 | + // Core Variables |
| 199 | + |
| 200 | + $encoder = new Base64(); |
| 201 | + |
| 202 | + // Step 1 - Strip Padding |
| 203 | + |
| 204 | + $this->assertSame("Zg", $encoder->stripPadding("Zg==")); |
| 205 | + |
| 206 | + try |
| 207 | + { |
| 208 | + $encoder->stripPadding("#"); |
| 209 | + |
| 210 | + $this->fail("Exception should've been thrown!"); |
| 211 | + } |
| 212 | + catch (\Exception $e) |
| 213 | + { |
| 214 | + $this->assertEquals("Invalid encoding provided, padding" . |
| 215 | + " can't be stripped. Encoding: \"#\"", $e->getMessage()); |
| 216 | + } |
| 217 | + |
| 218 | + // Step 2 - Chunk Conversion |
| 219 | + |
| 220 | + $this->assertSame([ |
| 221 | + 25, 32 |
| 222 | + ], $encoder->convertEncodingToChunks("Zg==")); |
| 223 | + |
| 224 | + try |
| 225 | + { |
| 226 | + $encoder->convertEncodingToChunks("#"); |
| 227 | + |
| 228 | + $this->fail("Exception should've been thrown!"); |
| 229 | + } |
| 230 | + catch (\Exception $e) |
| 231 | + { |
| 232 | + $this->assertEquals("Invalid encoding provided, it " . |
| 233 | + "can't be converted. Encoding: \"#\"", $e->getMessage()); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + /*********************\ |
| 238 | + |* SECONDARY METHODS *| |
| 239 | + \*********************/ |
| 240 | + |
| 241 | + /** |
| 242 | + * Tests encoding of the <i>Base 32</i> implementation. |
| 243 | + * |
| 244 | + * @author Djordje Jocic <[email protected]> |
| 245 | + * @copyright 2019 All Rights Reserved |
| 246 | + * @version 1.0.0 |
| 247 | + * |
| 248 | + * @return void |
| 249 | + */ |
| 250 | + |
| 251 | + public function testEncoding() |
| 252 | + { |
| 253 | + // Core Variables |
| 254 | + |
| 255 | + $encoder = new Base64(); |
| 256 | + |
| 257 | + // Logic. |
| 258 | + |
| 259 | + $this->assertSame("", $encoder->encode("")); |
| 260 | + $this->assertSame("Zg==", $encoder->encode("f")); |
| 261 | + $this->assertSame("Zm8=", $encoder->encode("fo")); |
| 262 | + $this->assertSame("Zm9v", $encoder->encode("foo")); |
| 263 | + $this->assertSame("Zm9vYg==", $encoder->encode("foob")); |
| 264 | + $this->assertSame("Zm9vYmE=", $encoder->encode("fooba")); |
| 265 | + $this->assertSame("Zm9vYmFy", $encoder->encode("foobar")); |
| 266 | + $this->assertSame("Zm9vYmFyMQ==", $encoder->encode("foobar1")); |
| 267 | + $this->assertSame("Zm9vYmFyMTI=", $encoder->encode("foobar12")); |
| 268 | + $this->assertSame("Zm9vYmFyMTIz", $encoder->encode("foobar123")); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Tests decoding of the <i>Base 64</i> implementation. |
| 273 | + * |
| 274 | + * @author Djordje Jocic <[email protected]> |
| 275 | + * @copyright 2019 All Rights Reserved |
| 276 | + * @version 1.0.0 |
| 277 | + * |
| 278 | + * @return void |
| 279 | + */ |
| 280 | + |
| 281 | + public function testDecoding() |
| 282 | + { |
| 283 | + // Core Variables |
| 284 | + |
| 285 | + $encoder = new Base64(); |
| 286 | + |
| 287 | + // Logic |
| 288 | + |
| 289 | + $this->assertSame("", $encoder->decode("")); |
| 290 | + $this->assertSame("f", $encoder->decode("Zg==")); |
| 291 | + $this->assertSame("fo", $encoder->decode("Zm8=")); |
| 292 | + $this->assertSame("foo", $encoder->decode("Zm9v")); |
| 293 | + $this->assertSame("foob", $encoder->decode("Zm9vYg==")); |
| 294 | + $this->assertSame("fooba", $encoder->decode("Zm9vYmE=")); |
| 295 | + $this->assertSame("foobar", $encoder->decode("Zm9vYmFy")); |
| 296 | + $this->assertSame("foobar1", $encoder->decode("Zm9vYmFyMQ==")); |
| 297 | + $this->assertSame("foobar12", $encoder->decode("Zm9vYmFyMTI=")); |
| 298 | + $this->assertSame("foobar123", $encoder->decode("Zm9vYmFyMTIz")); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | +?> |
0 commit comments