1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
<?php
class Swift_StreamFilters_ByteArrayReplacementFilterTest extends \PHPUnit_Framework_TestCase
{
public function testBasicReplacementsAreMade()
{
$filter = $this->_createFilter(array(0x61, 0x62), array(0x63, 0x64));
$this->assertEquals(
array(0x59, 0x60, 0x63, 0x64, 0x65),
$filter->filter(array(0x59, 0x60, 0x61, 0x62, 0x65))
);
}
public function testShouldBufferReturnsTrueIfPartialMatchAtEndOfBuffer()
{
$filter = $this->_createFilter(array(0x61, 0x62), array(0x63, 0x64));
$this->assertTrue($filter->shouldBuffer(array(0x59, 0x60, 0x61)),
'%s: Filter should buffer since 0x61 0x62 is the needle and the ending '.
'0x61 could be from 0x61 0x62'
);
}
public function testFilterCanMakeMultipleReplacements()
{
$filter = $this->_createFilter(array(array(0x61), array(0x62)), array(0x63));
$this->assertEquals(
array(0x60, 0x63, 0x60, 0x63, 0x60),
$filter->filter(array(0x60, 0x61, 0x60, 0x62, 0x60))
);
}
public function testMultipleReplacementsCanBeDifferent()
{
$filter = $this->_createFilter(array(array(0x61), array(0x62)), array(array(0x63), array(0x64)));
$this->assertEquals(
array(0x60, 0x63, 0x60, 0x64, 0x60),
$filter->filter(array(0x60, 0x61, 0x60, 0x62, 0x60))
);
}
public function testShouldBufferReturnsFalseIfPartialMatchNotAtEndOfString()
{
$filter = $this->_createFilter(array(0x0D, 0x0A), array(0x0A));
$this->assertFalse($filter->shouldBuffer(array(0x61, 0x62, 0x0D, 0x0A, 0x63)),
'%s: Filter should not buffer since x0Dx0A is the needle and is not at EOF'
);
}
public function testShouldBufferReturnsTrueIfAnyOfMultipleMatchesAtEndOfString()
{
$filter = $this->_createFilter(array(array(0x61, 0x62), array(0x63)), array(0x64));
$this->assertTrue($filter->shouldBuffer(array(0x59, 0x60, 0x61)),
'%s: Filter should buffer since 0x61 0x62 is a needle and the ending '.
'0x61 could be from 0x61 0x62'
);
}
public function testConvertingAllLineEndingsToCRLFWhenInputIsLF()
{
$filter = $this->_createFilter(
array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
);
$this->assertEquals(
array(0x60, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x63),
$filter->filter(array(0x60, 0x0A, 0x61, 0x0A, 0x62, 0x0A, 0x63))
);
}
public function testConvertingAllLineEndingsToCRLFWhenInputIsCR()
{
$filter = $this->_createFilter(
array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
);
$this->assertEquals(
array(0x60, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x63),
$filter->filter(array(0x60, 0x0D, 0x61, 0x0D, 0x62, 0x0D, 0x63))
);
}
public function testConvertingAllLineEndingsToCRLFWhenInputIsCRLF()
{
$filter = $this->_createFilter(
array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
);
$this->assertEquals(
array(0x60, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x63),
$filter->filter(array(0x60, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x63))
);
}
public function testConvertingAllLineEndingsToCRLFWhenInputIsLFCR()
{
$filter = $this->_createFilter(
array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
);
$this->assertEquals(
array(0x60, 0x0D, 0x0A, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x0D, 0x0A, 0x63),
$filter->filter(array(0x60, 0x0A, 0x0D, 0x61, 0x0A, 0x0D, 0x62, 0x0A, 0x0D, 0x63))
);
}
public function testConvertingAllLineEndingsToCRLFWhenInputContainsLFLF()
{
//Lighthouse Bug #23
$filter = $this->_createFilter(
array(array(0x0D, 0x0A), array(0x0D), array(0x0A)),
array(array(0x0A), array(0x0A), array(0x0D, 0x0A))
);
$this->assertEquals(
array(0x60, 0x0D, 0x0A, 0x0D, 0x0A, 0x61, 0x0D, 0x0A, 0x0D, 0x0A, 0x62, 0x0D, 0x0A, 0x0D, 0x0A, 0x63),
$filter->filter(array(0x60, 0x0A, 0x0A, 0x61, 0x0A, 0x0A, 0x62, 0x0A, 0x0A, 0x63))
);
}
// -- Creation methods
private function _createFilter($search, $replace)
{
return new Swift_StreamFilters_ByteArrayReplacementFilter($search, $replace);
}
}
|