Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 5348559

Browse files
committed
Add Check\PhpFlag.
1 parent 333196a commit 5348559

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

src/ZendDiagnostics/Check/PhpFlag.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use InvalidArgumentException;
9+
use Traversable;
10+
use ZendDiagnostics\Result\Failure;
11+
use ZendDiagnostics\Result\Success;
12+
13+
/**
14+
* Make sure given PHP flag is turned on or off in php.ini
15+
*
16+
* This test accepts a string or array of strings for php flags
17+
*/
18+
class PhpFlag extends AbstractCheck implements CheckInterface
19+
{
20+
/**
21+
* @var array
22+
*/
23+
protected $settings;
24+
25+
/**
26+
* @var bool
27+
*/
28+
protected $expectedValue;
29+
30+
/**
31+
* @param string|array|traversable $settingName PHP setting names to check.
32+
* @param bool $expectedValue true or false
33+
* @throws InvalidArgumentException
34+
*/
35+
public function __construct($settingName, $expectedValue)
36+
{
37+
if (is_object($settingName)) {
38+
if (!$settingName instanceof Traversable) {
39+
throw new InvalidArgumentException(
40+
'Expected setting name as string, array or traversable, got ' . get_class($settingName)
41+
);
42+
}
43+
$this->settings = iterator_to_array($settingName);
44+
45+
} elseif (!is_scalar($settingName)) {
46+
if (!is_array($settingName)) {
47+
throw new InvalidArgumentException(
48+
'Expected setting name as string, array or traversable, got ' . gettype($settingName)
49+
);
50+
}
51+
$this->settings = $settingName;
52+
53+
} else {
54+
$this->settings = array($settingName);
55+
}
56+
57+
if (!is_scalar($expectedValue)) {
58+
throw new InvalidArgumentException(
59+
'Expected expected value, expected boolean, got ' . gettype($expectedValue)
60+
);
61+
}
62+
63+
$this->expectedValue = (bool)$expectedValue;
64+
}
65+
66+
/**
67+
* Perform the check
68+
*
69+
* @see \ZendDiagnostics\Check\CheckInterface::check()
70+
* @return Success|Failure
71+
*/
72+
public function check()
73+
{
74+
$failures = array();
75+
76+
foreach ($this->settings as $name) {
77+
if (ini_get($name) != $this->expectedValue) {
78+
$failures[] = $name;
79+
}
80+
}
81+
82+
if (count($failures) > 1) {
83+
return new Failure(
84+
join(', ', $failures) .
85+
' are expected to be ' .
86+
($this->expectedValue ? 'enabled' : 'disabled')
87+
);
88+
} elseif (count($failures)) {
89+
return new Failure(
90+
$failures[0] .
91+
' is expected to be ' .
92+
($this->expectedValue ? 'enabled' : 'disabled')
93+
);
94+
}
95+
96+
if (count($this->settings) > 1) {
97+
return new Success(
98+
join(', ', $this->settings) .
99+
' are all ' .
100+
($this->expectedValue ? 'enabled' : 'disabled')
101+
);
102+
} else {
103+
return new Success(
104+
$this->settings[0] .
105+
' is ' .
106+
($this->expectedValue ? 'enabled' : 'disabled')
107+
);
108+
}
109+
}
110+
}

tests/ZendDiagnosticsTest/ChecksTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ZendDiagnosticsTest;
33

4+
use ArrayObject;
45
use Exception;
56
use SensioLabs\Security\SecurityChecker;
67
use ZendDiagnostics\Check\Callback;
@@ -9,6 +10,7 @@
910
use ZendDiagnostics\Check\DirReadable;
1011
use ZendDiagnostics\Check\DirWritable;
1112
use ZendDiagnostics\Check\ExtensionLoaded;
13+
use ZendDiagnostics\Check\PhpFlag;
1214
use ZendDiagnostics\Check\PhpVersion;
1315
use ZendDiagnostics\Check\ProcessRunning;
1416
use ZendDiagnostics\Check\StreamWrapperExists;
@@ -165,6 +167,65 @@ public function testExtensionLoaded()
165167
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
166168
}
167169

170+
public function testPhpFlag()
171+
{
172+
// Retrieve a set of settings to test against
173+
$all = ini_get_all();
174+
175+
foreach($all as $name => $valueArray) {
176+
if($valueArray['local_value'] == '0') {
177+
break;
178+
}
179+
}
180+
$check = new PhpFlag($name, false);
181+
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
182+
183+
$check = new PhpFlag($name, true);
184+
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
185+
186+
187+
$allFalse = array();
188+
foreach($all as $name => $valueArray) {
189+
if($valueArray['local_value'] == '0') {
190+
$allFalse[] = $name;
191+
}
192+
193+
if(count($allFalse) == 3) {
194+
break;
195+
}
196+
}
197+
198+
$check = new PhpFlag($allFalse, false);
199+
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
200+
201+
$check = new PhpFlag($allFalse, true);
202+
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result = $check->check());
203+
$this->assertStringMatchesFormat('%A' . join(', ', $allFalse) . '%Aenabled%A', $result->getMessage());
204+
205+
$allFalse = new ArrayObject($allFalse);
206+
$check = new PhpFlag($allFalse, false);
207+
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
208+
209+
$check = new PhpFlag($allFalse, true);
210+
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
211+
212+
213+
$notAllFalse = $allFalse;
214+
foreach($all as $name => $valueArray) {
215+
if($valueArray['local_value'] == '1') {
216+
$notAllFalse[] = $name;
217+
break;
218+
}
219+
}
220+
221+
$check = new PhpFlag($notAllFalse, false);
222+
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result = $check->check());
223+
$this->assertStringMatchesFormat("%A$name%A", $result->getMessage());
224+
225+
$check = new PhpFlag($notAllFalse, true);
226+
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
227+
}
228+
168229
public function testStreamWrapperExists()
169230
{
170231
$allWrappers = stream_get_wrappers();

0 commit comments

Comments
 (0)