1<?php 2/* 3 * This file is part of PHPUnit. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11class Extensions_PhptTestCaseTest extends \PHPUnit_Framework_TestCase 12{ 13 const EXPECT_CONTENT = <<<EOF 14--TEST-- 15EXPECT test 16--FILE-- 17<?php echo "Hello PHPUnit!"; ?> 18--EXPECT-- 19Hello PHPUnit! 20EOF; 21 22 const EXPECTF_CONTENT = <<<EOF 23--TEST-- 24EXPECTF test 25--FILE-- 26<?php echo "Hello PHPUnit!"; ?> 27--EXPECTF-- 28Hello %s! 29EOF; 30 31 const EXPECTREGEX_CONTENT = <<<EOF 32--TEST-- 33EXPECTREGEX test 34--FILE-- 35<?php echo "Hello PHPUnit!"; ?> 36--EXPECTREGEX-- 37Hello [HPU]{4}[nit]{3}! 38EOF; 39 40 const FILE_SECTION = <<<EOF 41<?php echo "Hello PHPUnit!"; ?> 42 43EOF; 44 45 protected $filename; 46 protected $testCase; 47 protected $phpUtil; 48 49 protected function setUp() 50 { 51 $this->filename = sys_get_temp_dir() . '/phpunit.phpt'; 52 touch($this->filename); 53 54 $this->phpUtil = $this->getMockForAbstractClass('PHPUnit_Util_PHP', [], '', false); 55 56 $this->testCase = new PHPUnit_Extensions_PhptTestCase($this->filename, $this->phpUtil); 57 } 58 59 protected function tearDown() 60 { 61 @unlink($this->filename); 62 63 $this->filename = null; 64 $this->testCase = null; 65 } 66 67 /** 68 * Defines the content of the current PHPT test. 69 * 70 * @param string $content 71 */ 72 private function setPhpContent($content) 73 { 74 file_put_contents($this->filename, $content); 75 } 76 77 public function testShouldRunFileSectionAsTest() 78 { 79 $this->setPhpContent(self::EXPECT_CONTENT); 80 81 $fileSection = '<?php echo "Hello PHPUnit!"; ?>' . PHP_EOL; 82 83 $this->phpUtil 84 ->expects($this->once()) 85 ->method('runJob') 86 ->with($fileSection) 87 ->will($this->returnValue(['stdout' => '', 'stderr' => ''])); 88 89 $this->testCase->run(); 90 } 91 92 public function testShouldRunSkipifSectionWhenExists() 93 { 94 $skipifSection = '<?php /** Nothing **/ ?>' . PHP_EOL; 95 96 $phptContent = self::EXPECT_CONTENT . PHP_EOL; 97 $phptContent .= '--SKIPIF--' . PHP_EOL; 98 $phptContent .= $skipifSection; 99 100 $this->setPhpContent($phptContent); 101 102 $this->phpUtil 103 ->expects($this->at(0)) 104 ->method('runJob') 105 ->with($skipifSection) 106 ->will($this->returnValue(['stdout' => '', 'stderr' => ''])); 107 108 $this->testCase->run(); 109 } 110 111 public function testShouldNotRunTestSectionIfSkipifSectionReturnsOutputWithSkipWord() 112 { 113 $skipifSection = '<?php echo "skip: Reason"; ?>' . PHP_EOL; 114 115 $phptContent = self::EXPECT_CONTENT . PHP_EOL; 116 $phptContent .= '--SKIPIF--' . PHP_EOL; 117 $phptContent .= $skipifSection; 118 119 $this->setPhpContent($phptContent); 120 121 $this->phpUtil 122 ->expects($this->once()) 123 ->method('runJob') 124 ->with($skipifSection) 125 ->will($this->returnValue(['stdout' => 'skip: Reason', 'stderr' => ''])); 126 127 $this->testCase->run(); 128 } 129 130 public function testShouldRunCleanSectionWhenDefined() 131 { 132 $cleanSection = '<?php unlink("/tmp/something"); ?>' . PHP_EOL; 133 134 $phptContent = self::EXPECT_CONTENT . PHP_EOL; 135 $phptContent .= '--CLEAN--' . PHP_EOL; 136 $phptContent .= $cleanSection; 137 138 $this->setPhpContent($phptContent); 139 140 $this->phpUtil 141 ->expects($this->at(1)) 142 ->method('runJob') 143 ->with($cleanSection); 144 145 $this->testCase->run(); 146 } 147 148 /** 149 * @expectedException PHPUnit_Framework_Exception 150 * @expectedExceptionMessage Invalid PHPT file 151 */ 152 public function testShouldThrowsAnExceptionWhenPhptFileIsEmpty() 153 { 154 $this->setPhpContent(''); 155 156 $this->testCase->run(); 157 } 158 159 /** 160 * @expectedException PHPUnit_Framework_Exception 161 * @expectedExceptionMessage Invalid PHPT file 162 */ 163 public function testShouldThrowsAnExceptionWhenFileSectionIsMissing() 164 { 165 $this->setPhpContent( 166 <<<EOF 167--TEST-- 168Something to decribe it 169--EXPECT-- 170Something 171EOF 172 ); 173 $this->testCase->run(); 174 } 175 176 /** 177 * @expectedException PHPUnit_Framework_Exception 178 * @expectedExceptionMessage Invalid PHPT file 179 */ 180 public function testShouldThrowsAnExceptionWhenThereIsNoExpecOrExpectifOrExpecregexSectionInPhptFile() 181 { 182 $this->setPhpContent( 183 <<<EOF 184--TEST-- 185Something to decribe it 186--FILE-- 187<?php 188echo "Hello world!\n"; 189?> 190EOF 191 ); 192 $this->testCase->run(); 193 } 194 195 public function testShouldValidateExpectSession() 196 { 197 $this->setPhpContent(self::EXPECT_CONTENT); 198 199 $this->phpUtil 200 ->expects($this->once()) 201 ->method('runJob') 202 ->with(self::FILE_SECTION) 203 ->will($this->returnValue(['stdout' => 'Hello PHPUnit!', 'stderr' => ''])); 204 205 $result = $this->testCase->run(); 206 207 $this->assertTrue($result->wasSuccessful()); 208 } 209 210 public function testShouldValidateExpectfSession() 211 { 212 $this->setPhpContent(self::EXPECTF_CONTENT); 213 214 $this->phpUtil 215 ->expects($this->once()) 216 ->method('runJob') 217 ->with(self::FILE_SECTION) 218 ->will($this->returnValue(['stdout' => 'Hello PHPUnit!', 'stderr' => ''])); 219 220 $result = $this->testCase->run(); 221 222 $this->assertTrue($result->wasSuccessful()); 223 } 224 225 public function testShouldValidateExpectregexSession() 226 { 227 $this->setPhpContent(self::EXPECTREGEX_CONTENT); 228 229 $this->phpUtil 230 ->expects($this->once()) 231 ->method('runJob') 232 ->with(self::FILE_SECTION) 233 ->will($this->returnValue(['stdout' => 'Hello PHPUnit!', 'stderr' => ''])); 234 235 $result = $this->testCase->run(); 236 237 $this->assertTrue($result->wasSuccessful()); 238 } 239 240 public function testParseIniSection() 241 { 242 $phptTestCase = new PhpTestCaseProxy(__FILE__); 243 $settings = $phptTestCase->parseIniSection("foo=1\nbar = 2\rbaz = 3\r\nempty=\nignore"); 244 245 $expected = [ 246 'foo=1', 247 'bar = 2', 248 'baz = 3', 249 'empty=', 250 'ignore', 251 ]; 252 253 $this->assertEquals($expected, $settings); 254 } 255} 256 257class PhpTestCaseProxy extends PHPUnit_Extensions_PhptTestCase 258{ 259 public function parseIniSection($content) 260 { 261 return parent::parseIniSection($content); 262 } 263} 264