1<?php 2 3/** 4 * Hoa 5 * 6 * 7 * @license 8 * 9 * New BSD License 10 * 11 * Copyright © 2007-2017, Hoa community. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions are met: 15 * * Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * * Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * * Neither the name of the Hoa nor the names of its contributors may be 21 * used to endorse or promote products derived from this software without 22 * specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37namespace Hoa\Stream\Test\Unit\Filter; 38 39use Hoa\Stream as LUT; 40use Hoa\Stream\Filter as SUT; 41use Hoa\Test; 42 43/** 44 * Class \Hoa\Stream\Test\Unit\Filter\Filter. 45 * 46 * Test suite of the filter class. 47 * 48 * @copyright Copyright © 2007-2017 Hoa community 49 * @license New BSD License 50 */ 51class Filter extends Test\Unit\Suite 52{ 53 public function case_constants() 54 { 55 $this 56 ->boolean(SUT::OVERWRITE) 57 ->isTrue() 58 ->boolean(SUT::DO_NOT_OVERWRITE) 59 ->isFalse() 60 ->integer(SUT::READ) 61 ->isEqualTo(STREAM_FILTER_READ) 62 ->integer(SUT::WRITE) 63 ->isEqualTo(STREAM_FILTER_WRITE) 64 ->integer(SUT::READ_AND_WRITE) 65 ->isEqualTo(STREAM_FILTER_ALL); 66 } 67 68 public function case_register() 69 { 70 $this 71 ->when($result = SUT::register('foo', \StdClass::class)) 72 ->then 73 ->boolean($result) 74 ->isTrue(); 75 } 76 77 public function case_register_already_registered_do_not_overwrite() 78 { 79 $this 80 ->given( 81 $name = 'foo', 82 $class = \StdClass::class, 83 SUT::register($name, $class) 84 ) 85 ->exception(function () use ($name, $class) { 86 SUT::register($name, $class); 87 }) 88 ->isInstanceOf(LUT\Filter\Exception::class) 89 ->hasMessage('Filter foo is already registered.'); 90 } 91 92 public function case_register_already_registered_do_overwrite() 93 { 94 $this 95 ->given( 96 $name = 'foo', 97 SUT::register($name, \StdClass::class), 98 new \Mock\StdClass() // create it 99 ) 100 ->when($result = SUT::register($name, \Mock\StdClass::class, SUT::OVERWRITE)) 101 ->then 102 ->boolean($result) 103 ->isFalse(); 104 } 105 106 public function case_register_empty_name() 107 { 108 $this 109 ->exception(function () { 110 SUT::register('', \StdClass::class); 111 }) 112 ->isInstanceOf(LUT\Filter\Exception::class) 113 ->hasMessage( 114 'Filter name cannot be empty ' . 115 '(implementation class is StdClass).' 116 ); 117 } 118 119 public function case_register_unknown_class() 120 { 121 $this 122 ->exception(function () { 123 SUT::register('foo', '42Foo'); 124 }) 125 ->isInstanceOf(LUT\Filter\Exception::class) 126 ->hasMessage( 127 'Cannot register the 42Foo class for the filter foo ' . 128 'because it does not exist.' 129 ); 130 } 131 132 public function case_append() 133 { 134 $this 135 ->given( 136 $stream = fopen('hoa://Test/Vfs/Foo?type=file', 'r'), 137 $name = 'string.toupper' 138 ) 139 ->when($result = SUT::append($stream, $name)) 140 ->then 141 ->resource($result) 142 ->isStreamFilter(); 143 } 144 145 public function case_prepend() 146 { 147 $this 148 ->given( 149 $stream = fopen('hoa://Test/Vfs/Foo?type=file', 'r'), 150 $name = 'string.toupper' 151 ) 152 ->when($result = SUT::prepend($stream, $name)) 153 ->then 154 ->resource($result) 155 ->isStreamFilter(); 156 } 157 158 public function case_remove() 159 { 160 $this 161 ->given( 162 $stream = fopen('hoa://Test/Vfs/Foo?type=file', 'r'), 163 $name = 'string.toupper', 164 $filter = SUT::append($stream, $name) 165 ) 166 ->when($result = SUT::remove($filter)) 167 ->then 168 ->boolean($result) 169 ->isTrue(); 170 } 171 172 public function case_remove_by_name() 173 { 174 $this 175 ->given( 176 $stream = fopen('hoa://Test/Vfs/Foo?type=file', 'r'), 177 $name = 'string.toupper', 178 $filter = SUT::append($stream, $name) 179 ) 180 ->when($result = SUT::remove($name)) 181 ->then 182 ->boolean($result) 183 ->isTrue(); 184 } 185 186 public function case_remove_unknown() 187 { 188 $this 189 ->exception(function () { 190 SUT::remove('foo'); 191 }) 192 ->isInstanceOf(LUT\Filter\Exception::class) 193 ->hasMessage( 194 'Cannot remove the stream filter foo ' . 195 'because no resource was found with this name.' 196 ); 197 } 198 199 public function case_is_registered() 200 { 201 $this 202 ->when($result = SUT::isRegistered('string.toupper')) 203 ->then 204 ->boolean($result) 205 ->isTrue(); 206 } 207 208 public function case_is_not_registered() 209 { 210 $this 211 ->when($result = SUT::isRegistered('foo')) 212 ->then 213 ->boolean($result) 214 ->isFalse(); 215 } 216 217 public function case_get_registered() 218 { 219 $this 220 ->when($result = SUT::getRegistered()) 221 ->then 222 ->array($result) 223 ->containsValues([ 224 'string.rot13', 225 'string.toupper', 226 'string.tolower', 227 'string.strip_tags', 228 'consumed', 229 'dechunk' 230 ]); 231 } 232} 233