|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Weebly\Mutate\Mutators; |
| 4 | + |
| 5 | +use stdClass; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class HexBinaryMutatorTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @param string $hex |
| 12 | + * @param string $expected |
| 13 | + * @dataProvider hexProvider |
| 14 | + */ |
| 15 | + public function testSerialize($hex, $expected) |
| 16 | + { |
| 17 | + $this->assertEquals($expected, (new HexBinaryMutator())->serializeAttribute($hex)); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @param string $expected |
| 22 | + * @param string $binary |
| 23 | + * @dataProvider hexProvider |
| 24 | + */ |
| 25 | + public function testUnserialize($expected, $binary) |
| 26 | + { |
| 27 | + $this->assertEquals($expected, (new HexBinaryMutator())->unserializeAttribute($binary)); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return \Iterator |
| 32 | + */ |
| 33 | + public function hexProvider() |
| 34 | + { |
| 35 | + $hexes = [ |
| 36 | + 'Hex string of length 2' => ['e7', hex2bin('e7')], |
| 37 | + 'Hex string of length 4' => ['232f', hex2bin('232f')], |
| 38 | + 'Hex string of length 6' => ['0b76e0', hex2bin('0b76e0')], |
| 39 | + 'Hex string of length 8' => ['65c1b7b0', hex2bin('65c1b7b0')], |
| 40 | + 'Hex string of length 10' => ['e2f9995c0b', hex2bin('e2f9995c0b')], |
| 41 | + 'Hex string of length 12' => ['cfc46177b9cd', hex2bin('cfc46177b9cd')], |
| 42 | + 'Hex string of length 14' => ['01f4890a5996c1', hex2bin('01f4890a5996c1')], |
| 43 | + 'Hex string of length 16' => ['d8d3a96d2a441b39', hex2bin('d8d3a96d2a441b39')], |
| 44 | + 'Hex string of length 18' => ['5d87fca1c8f5c2ec21', hex2bin('5d87fca1c8f5c2ec21')], |
| 45 | + 'Hex string of length 20' => ['c334821e50532bd40227', hex2bin('c334821e50532bd40227')], |
| 46 | + ]; |
| 47 | + |
| 48 | + return $hexes; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param mixed $value |
| 53 | + * @dataProvider notHexProvider |
| 54 | + * @expectedException \Weebly\Mutate\Exceptions\MutateException |
| 55 | + */ |
| 56 | + public function testWrongFormat($value) |
| 57 | + { |
| 58 | + (new HexBinaryMutator())->serializeAttribute($value); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public function notHexProvider() |
| 65 | + { |
| 66 | + return [ |
| 67 | + 'An object cannot be serialized' => [new stdClass], |
| 68 | + 'An int cannot be serialized' => [0], |
| 69 | + 'Floats cannot be serialized' => [0.3], |
| 70 | + 'Only hex strings can be serialiazed' => ['YzMzNDgyMWU1MDUzMmJkNDAy'], |
| 71 | + 'Null cannot be serialized' => [null], |
| 72 | + 'Booleans cannot be serialized' => [true], |
| 73 | + 'Hex strings should have even lengths to be valid bytes representations' => ['a'], |
| 74 | + ]; |
| 75 | + } |
| 76 | +} |
0 commit comments