|
| 1 | +<?php |
| 2 | +namespace ScriptFUSIONTest\Unit\Mapper\Strategy; |
| 3 | + |
| 4 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 5 | +use ScriptFUSION\Mapper\Expression; |
| 6 | +use ScriptFUSION\Mapper\Strategy\Replace; |
| 7 | +use ScriptFUSIONTest\MockFactory; |
| 8 | + |
| 9 | +final class ReplaceTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + use MockeryPHPUnitIntegration; |
| 12 | + |
| 13 | + /** |
| 14 | + * @dataProvider provideReplacements |
| 15 | + */ |
| 16 | + public function testReplace($input, $search, $replace, $output) |
| 17 | + { |
| 18 | + $replace = (new Replace($input, $search, $replace))->setMapper(MockFactory::mockMapper($input)); |
| 19 | + |
| 20 | + self::assertSame($output, $replace([])); |
| 21 | + } |
| 22 | + |
| 23 | + public function provideReplacements() |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'Single removal' => ['foo', 'o', null, 'f'], |
| 27 | + 'Substring removal' => ['foo', 'oo', null, 'f'], |
| 28 | + |
| 29 | + 'Single replacement' => ['foo', 'f', 'b', 'boo'], |
| 30 | + 'Substring replacement' => ['foo', 'foo', 'bar', 'bar'], |
| 31 | + |
| 32 | + 'Multiple removal' => ['foo', ['f', 'o'], null, ''], |
| 33 | + 'Multiple replacement' => ['foo', ['f', 'o'], ['h', 'a'], 'haa'], |
| 34 | + |
| 35 | + 'Insufficient replacements (uniform types)' => ['foo', ['f', 'o'], ['x'], 'xxx'], |
| 36 | + 'Insufficient replacements (mixed types)' => ['foo', ['f', 'o'], 'x', 'xxx'], |
| 37 | + 'Extra replacements (uniform types)' => ['foo', ['f'], ['b', 'w'], 'boo'], |
| 38 | + 'Extra replacements (mixed types)' => ['foo', 'f', ['b', 'w'], 'boo'], |
| 39 | + |
| 40 | + 'Recursive replacement' => ['foo', ['f', 'b'], ['b', 'w'], 'woo'], |
| 41 | + |
| 42 | + 'Regex single removal' => ['foo', new Expression('[o]'), null, 'f'], |
| 43 | + 'Regex substring removal' => ['foo', new Expression('[oo]'), null, 'f'], |
| 44 | + |
| 45 | + 'Regex single replacement' => ['foo', new Expression('[o$]'), 'x', 'fox'], |
| 46 | + 'Regex substring replacement' => ['foo', new Expression('[^foo$]'), 'bar', 'bar'], |
| 47 | + |
| 48 | + 'Regex multiple removal' => ['foo', [new Expression('[f]'), new Expression('[o]')], null, ''], |
| 49 | + 'Regex multiple replacement' => ['foo', [new Expression('[f]'), new Expression('[o]')], ['h', 'a'], 'haa'], |
| 50 | + |
| 51 | + 'Regex insufficient replacements' => ['foo', [new Expression('[f]'), new Expression('[o]')], ['x'], 'xxx'], |
| 52 | + 'Regex extra replacements' => ['foo', new Expression('[f]'), ['b', 'w'], 'boo'], |
| 53 | + |
| 54 | + 'Mixed mode replacement' => ['foo', ['f', new Expression('[o$]')], ['c', 'x'], 'cox'], |
| 55 | + 'Sub-match replacement' => ['foo', new Expression('[(f)oo]'), 'o$1$1', 'off'], |
| 56 | + ]; |
| 57 | + } |
| 58 | +} |
0 commit comments