-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds config file validator and tests
- Loading branch information
1 parent
4068eed
commit 8612c1a
Showing
11 changed files
with
263 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author hollodotme | ||
*/ | ||
|
||
namespace PHPMQ\Server\Validators; | ||
|
||
use PHPMQ\Server\Validators\Interfaces\ValidatesEnvironment; | ||
|
||
/** | ||
* Class ConfigFileValidator | ||
* @package PHPMQ\Server\Validators | ||
*/ | ||
final class ConfigFileValidator implements ValidatesEnvironment | ||
{ | ||
/** @var string */ | ||
private $configFilePath; | ||
|
||
/** @var bool|\SimpleXMLElement */ | ||
private $xml; | ||
|
||
/** @var bool */ | ||
private $passed = false; | ||
|
||
/** @var array */ | ||
private $messages = []; | ||
|
||
/** @var array|\LibXMLError[] */ | ||
private $xmlErrors = []; | ||
|
||
public function __construct( string $configFilePath ) | ||
{ | ||
$this->configFilePath = $configFilePath; | ||
|
||
libxml_use_internal_errors( true ); | ||
|
||
$this->xml = simplexml_load_file( $configFilePath ); | ||
$this->xmlErrors = libxml_get_errors(); | ||
|
||
libxml_use_internal_errors( false ); | ||
} | ||
|
||
public function failed() : bool | ||
{ | ||
$this->checkForParseErrors(); | ||
|
||
$this->passed && $this->checkMessageQueueServer(); | ||
|
||
return !$this->passed; | ||
} | ||
|
||
private function checkForParseErrors() : void | ||
{ | ||
if ( $this->xml === false ) | ||
{ | ||
$this->addErrorMessage( 'Could not read configuration file: "%s". Parse errors:', $this->configFilePath ); | ||
$this->addXmlErrors(); | ||
|
||
return; | ||
} | ||
|
||
$this->passed = true; | ||
} | ||
|
||
private function addErrorMessage( string $format, ...$params ) : void | ||
{ | ||
$this->messages[] = sprintf( '<bg:red>ERROR:<:bg> ' . $format, ...$params ); | ||
} | ||
|
||
private function addXmlErrors() : void | ||
{ | ||
/** @var \LibXMLError $xmlError */ | ||
foreach ( $this->xmlErrors as $xmlError ) | ||
{ | ||
$this->addErrorMessage( | ||
'[%s] %s in line %d and column %d', | ||
$xmlError->code, | ||
$xmlError->message, | ||
$xmlError->line, | ||
$xmlError->column | ||
); | ||
} | ||
} | ||
|
||
private function checkMessageQueueServer() : void | ||
{ | ||
$baseXPath = '/PHPMQ/servers/messagequeue'; | ||
|
||
if ( !$this->elementExists( $baseXPath ) ) | ||
{ | ||
$this->passed = false; | ||
$this->addErrorMessage( 'Missing configuration part: servers/messagequeue' ); | ||
|
||
return; | ||
} | ||
|
||
$networkXPath = $baseXPath . '/network'; | ||
$unixXPath = $baseXPath . '/unix'; | ||
|
||
if ( !$this->elementExists( $networkXPath ) && !$this->elementExists( $unixXPath ) ) | ||
{ | ||
$this->passed = false; | ||
$this->addErrorMessage( 'Invalid message queue server socket type. Allowed: network, unix' ); | ||
|
||
return; | ||
} | ||
|
||
$networkHostXPath = $networkXPath . '/config[@name="host"]'; | ||
$networkPortXPath = $networkXPath . '/config[@name="port"]'; | ||
|
||
if ( $this->elementExists( $networkXPath ) | ||
&& (!$this->elementExists( $networkHostXPath ) || !$this->elementExists( $networkPortXPath )) ) | ||
{ | ||
$this->passed = false; | ||
$this->addErrorMessage( 'Invalid message queue server socket config. Host and port must be configured.' ); | ||
|
||
return; | ||
} | ||
|
||
$unixPathXPath = $unixXPath . '/config[@name="path"]'; | ||
|
||
if ( $this->elementExists( $unixXPath ) && !$this->elementExists( $unixPathXPath ) ) | ||
{ | ||
$this->passed = false; | ||
$this->addErrorMessage( 'Invalid message queue server socket config. Path must be configured.' ); | ||
|
||
return; | ||
} | ||
} | ||
|
||
private function elementExists( string $xpath ) : bool | ||
{ | ||
return (count( $this->xml->xpath( $xpath ) ) >= 1); | ||
} | ||
|
||
public function getMessages() : array | ||
{ | ||
return $this->messages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author hollodotme | ||
*/ | ||
|
||
namespace PHPMQ\Server\Tests\Unit\Validators; | ||
|
||
use PHPMQ\Server\Validators\ConfigFileValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class ConfigFileValidatorTest | ||
* @package PHPMQ\Server\Tests\Unit\Validators | ||
*/ | ||
final class ConfigFileValidatorTest extends TestCase | ||
{ | ||
public function testValidationFailsForBrokenXmlConfigFile() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/broken.xml' ); | ||
|
||
$this->assertTrue( $validator->failed() ); | ||
$this->assertNotEmpty( $validator->getMessages() ); | ||
} | ||
|
||
public function testValidationFailsNotForValidXmlConfigFile() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/valid.xml' ); | ||
|
||
$this->assertFalse( $validator->failed() ); | ||
$this->assertEmpty( $validator->getMessages() ); | ||
} | ||
|
||
public function testFailsIfMessageQueueServerIsNotConfigured() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/missing-message-queue-server.xml' ); | ||
|
||
$this->assertTrue( $validator->failed() ); | ||
$this->assertNotEmpty( $validator->getMessages() ); | ||
} | ||
|
||
public function testFailsIfMessageQueueServerSocketTypeIsInvalid() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/invalid-message-queue-server-socket-type.xml' ); | ||
|
||
$this->assertTrue( $validator->failed() ); | ||
$this->assertNotEmpty( $validator->getMessages() ); | ||
} | ||
|
||
public function testFailsIfMessageQueueServerNetworkSocketIsInvalid() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/invalid-message-queue-server-network-socket.xml' ); | ||
|
||
$this->assertTrue( $validator->failed() ); | ||
$this->assertNotEmpty( $validator->getMessages() ); | ||
} | ||
|
||
public function testFailsIfMessageQueueServerUnixSocketIsInvalid() : void | ||
{ | ||
$validator = new ConfigFileValidator( __DIR__ . '/Fixtures/invalid-message-queue-server-unix-socket.xml' ); | ||
|
||
$this->assertTrue( $validator->failed() ); | ||
$this->assertNotEmpty( $validator->getMessages() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<Broken> | ||
</PHPMQ> |
8 changes: 8 additions & 0 deletions
8
tests/Unit/Validators/Fixtures/invalid-message-queue-server-network-socket.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<servers> | ||
<messagequeue> | ||
<network/> | ||
</messagequeue> | ||
</servers> | ||
</PHPMQ> |
8 changes: 8 additions & 0 deletions
8
tests/Unit/Validators/Fixtures/invalid-message-queue-server-socket-type.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<servers> | ||
<messagequeue> | ||
<invalid/> | ||
</messagequeue> | ||
</servers> | ||
</PHPMQ> |
8 changes: 8 additions & 0 deletions
8
tests/Unit/Validators/Fixtures/invalid-message-queue-server-unix-socket.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<servers> | ||
<messagequeue> | ||
<unix/> | ||
</messagequeue> | ||
</servers> | ||
</PHPMQ> |
4 changes: 4 additions & 0 deletions
4
tests/Unit/Validators/Fixtures/missing-message-queue-server.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<servers/> | ||
</PHPMQ> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<PHPMQ> | ||
<servers> | ||
<messagequeue> | ||
<network> | ||
<config name="host" value="192.168.3.13"/> | ||
<config name="port" value="9100"/> | ||
</network> | ||
</messagequeue> | ||
</servers> | ||
</PHPMQ> |