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
11/**
12 * @author     Henrique Moody <henriquemoody@gmail.com>
13 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
14 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
15 *
16 * @link       http://www.phpunit.de/
17 */
18class PHPUnit_Util_PHPTest extends PHPUnit_Framework_TestCase
19{
20    public function testShouldNotUseStderrRedirectionByDefault()
21    {
22        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
23
24        $this->assertFalse($phpMock->useStderrRedirection());
25    }
26
27    public function testShouldDefinedIfUseStderrRedirection()
28    {
29        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
30        $phpMock->setUseStderrRedirection(true);
31
32        $this->assertTrue($phpMock->useStderrRedirection());
33    }
34
35    public function testShouldDefinedIfDoNotUseStderrRedirection()
36    {
37        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
38        $phpMock->setUseStderrRedirection(false);
39
40        $this->assertFalse($phpMock->useStderrRedirection());
41    }
42
43    /**
44     * @expectedException PHPUnit_Framework_Exception
45     * @expectedExceptionMessage Argument #1 (No Value) of PHPUnit_Util_PHP::setUseStderrRedirection() must be a boolean
46     */
47    public function testShouldThrowsExceptionWhenStderrRedirectionVariableIsNotABoolean()
48    {
49        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
50        $phpMock->setUseStderrRedirection(null);
51    }
52
53    public function testShouldUseGivenSettingsToCreateCommand()
54    {
55        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
56
57        $settings = [
58            'allow_url_fopen=1',
59            'auto_append_file=',
60            'display_errors=1',
61        ];
62
63        $expectedCommandFormat  = '%s -d allow_url_fopen=1 -d auto_append_file= -d display_errors=1';
64        $actualCommand          = $phpMock->getCommand($settings);
65
66        $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
67    }
68
69    public function testShouldRedirectStderrToStdoutWhenDefined()
70    {
71        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
72        $phpMock->setUseStderrRedirection(true);
73
74        $expectedCommandFormat  = '%s 2>&1';
75        $actualCommand          = $phpMock->getCommand([]);
76
77        $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
78    }
79
80    public function testShouldUseArgsToCreateCommand()
81    {
82        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
83        $phpMock->setArgs('foo=bar');
84
85        $expectedCommandFormat  = '%s -- foo=bar';
86        $actualCommand          = $phpMock->getCommand([]);
87
88        $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
89    }
90
91    public function testShouldHaveFileToCreateCommand()
92    {
93        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
94
95        $expectedCommandFormat  = '%s -%c \'file.php\'';
96        $actualCommand          = $phpMock->getCommand([], 'file.php');
97
98        $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand);
99    }
100
101    public function testStdinGetterAndSetter()
102    {
103        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
104        $phpMock->setStdin('foo');
105
106        $this->assertEquals('foo', $phpMock->getStdin());
107    }
108
109    public function testArgsGetterAndSetter()
110    {
111        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
112        $phpMock->setArgs('foo=bar');
113
114        $this->assertEquals('foo=bar', $phpMock->getArgs());
115    }
116
117    public function testEnvGetterAndSetter()
118    {
119        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
120        $phpMock->setEnv(['foo' => 'bar']);
121
122        $this->assertEquals(['foo' => 'bar'], $phpMock->getEnv());
123    }
124
125    public function testTimeoutGetterAndSetter()
126    {
127        $phpMock = $this->getMockForAbstractClass('PHPUnit_Util_PHP');
128        $phpMock->setTimeout(30);
129
130        $this->assertEquals(30, $phpMock->getTimeout());
131    }
132}
133