Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Check\PhpFlag.

  • Loading branch information...
commit 53485590b94fe0709d044d232001ba72755ec80a 1 parent 333196a
Artur Bodera Thinkscape authored
110 src/ZendDiagnostics/Check/PhpFlag.php
View
@@ -0,0 +1,110 @@
+<?php
+/**
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendDiagnostics\Check;
+
+use InvalidArgumentException;
+use Traversable;
+use ZendDiagnostics\Result\Failure;
+use ZendDiagnostics\Result\Success;
+
+/**
+ * Make sure given PHP flag is turned on or off in php.ini
+ *
+ * This test accepts a string or array of strings for php flags
+ */
+class PhpFlag extends AbstractCheck implements CheckInterface
+{
+ /**
+ * @var array
+ */
+ protected $settings;
+
+ /**
+ * @var bool
+ */
+ protected $expectedValue;
+
+ /**
+ * @param string|array|traversable $settingName PHP setting names to check.
+ * @param bool $expectedValue true or false
+ * @throws InvalidArgumentException
+ */
+ public function __construct($settingName, $expectedValue)
+ {
+ if (is_object($settingName)) {
+ if (!$settingName instanceof Traversable) {
+ throw new InvalidArgumentException(
+ 'Expected setting name as string, array or traversable, got ' . get_class($settingName)
+ );
+ }
+ $this->settings = iterator_to_array($settingName);
+
+ } elseif (!is_scalar($settingName)) {
+ if (!is_array($settingName)) {
+ throw new InvalidArgumentException(
+ 'Expected setting name as string, array or traversable, got ' . gettype($settingName)
+ );
+ }
+ $this->settings = $settingName;
+
+ } else {
+ $this->settings = array($settingName);
+ }
+
+ if (!is_scalar($expectedValue)) {
+ throw new InvalidArgumentException(
+ 'Expected expected value, expected boolean, got ' . gettype($expectedValue)
+ );
+ }
+
+ $this->expectedValue = (bool)$expectedValue;
+ }
+
+ /**
+ * Perform the check
+ *
+ * @see \ZendDiagnostics\Check\CheckInterface::check()
+ * @return Success|Failure
+ */
+ public function check()
+ {
+ $failures = array();
+
+ foreach ($this->settings as $name) {
+ if (ini_get($name) != $this->expectedValue) {
+ $failures[] = $name;
+ }
+ }
+
+ if (count($failures) > 1) {
+ return new Failure(
+ join(', ', $failures) .
+ ' are expected to be ' .
+ ($this->expectedValue ? 'enabled' : 'disabled')
+ );
+ } elseif (count($failures)) {
+ return new Failure(
+ $failures[0] .
+ ' is expected to be ' .
+ ($this->expectedValue ? 'enabled' : 'disabled')
+ );
+ }
+
+ if (count($this->settings) > 1) {
+ return new Success(
+ join(', ', $this->settings) .
+ ' are all ' .
+ ($this->expectedValue ? 'enabled' : 'disabled')
+ );
+ } else {
+ return new Success(
+ $this->settings[0] .
+ ' is ' .
+ ($this->expectedValue ? 'enabled' : 'disabled')
+ );
+ }
+ }
+}
61 tests/ZendDiagnosticsTest/ChecksTest.php
View
@@ -1,6 +1,7 @@
<?php
namespace ZendDiagnosticsTest;
+use ArrayObject;
use Exception;
use SensioLabs\Security\SecurityChecker;
use ZendDiagnostics\Check\Callback;
@@ -9,6 +10,7 @@
use ZendDiagnostics\Check\DirReadable;
use ZendDiagnostics\Check\DirWritable;
use ZendDiagnostics\Check\ExtensionLoaded;
+use ZendDiagnostics\Check\PhpFlag;
use ZendDiagnostics\Check\PhpVersion;
use ZendDiagnostics\Check\ProcessRunning;
use ZendDiagnostics\Check\StreamWrapperExists;
@@ -165,6 +167,65 @@ public function testExtensionLoaded()
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
}
+ public function testPhpFlag()
+ {
+ // Retrieve a set of settings to test against
+ $all = ini_get_all();
+
+ foreach($all as $name => $valueArray) {
+ if($valueArray['local_value'] == '0') {
+ break;
+ }
+ }
+ $check = new PhpFlag($name, false);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
+
+ $check = new PhpFlag($name, true);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
+
+
+ $allFalse = array();
+ foreach($all as $name => $valueArray) {
+ if($valueArray['local_value'] == '0') {
+ $allFalse[] = $name;
+ }
+
+ if(count($allFalse) == 3) {
+ break;
+ }
+ }
+
+ $check = new PhpFlag($allFalse, false);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
+
+ $check = new PhpFlag($allFalse, true);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result = $check->check());
+ $this->assertStringMatchesFormat('%A' . join(', ', $allFalse) . '%Aenabled%A', $result->getMessage());
+
+ $allFalse = new ArrayObject($allFalse);
+ $check = new PhpFlag($allFalse, false);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Success', $check->check());
+
+ $check = new PhpFlag($allFalse, true);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
+
+
+ $notAllFalse = $allFalse;
+ foreach($all as $name => $valueArray) {
+ if($valueArray['local_value'] == '1') {
+ $notAllFalse[] = $name;
+ break;
+ }
+ }
+
+ $check = new PhpFlag($notAllFalse, false);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result = $check->check());
+ $this->assertStringMatchesFormat("%A$name%A", $result->getMessage());
+
+ $check = new PhpFlag($notAllFalse, true);
+ $this->assertInstanceOf('ZendDiagnostics\Result\Failure', $check->check());
+ }
+
public function testStreamWrapperExists()
{
$allWrappers = stream_get_wrappers();
Please sign in to comment.
Something went wrong with that request. Please try again.