mirror of
https://github.com/envoyr/nginx-configurator.git
synced 2026-05-04 14:53:42 +00:00
Added some tests and factory impl
This commit is contained in:
38
tests/spec/BuilderSpec.php
Normal file
38
tests/spec/BuilderSpec.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace spec\Madkom\NginxConfigurator;
|
||||
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Config\Upstream;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* Class BuilderSpec
|
||||
* @package spec\Madkom\NginxConfigurator
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
* @mixin Builder
|
||||
*/
|
||||
class BuilderSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(Builder::class);
|
||||
}
|
||||
|
||||
function it_can_build_with_Server_node(Server $server)
|
||||
{
|
||||
$server->__toString()->willReturn("server {
|
||||
}");
|
||||
$this->append($server);
|
||||
$this->dump()->shouldBeString();
|
||||
}
|
||||
|
||||
function it_can_build_with_Upstream_node(Upstream $upstream)
|
||||
{
|
||||
$upstream->__toString()->willReturn("upstream name {
|
||||
}");
|
||||
$this->append($upstream);
|
||||
$this->dump()->shouldBeString();
|
||||
}
|
||||
}
|
||||
32
tests/spec/FactorySpec.php
Normal file
32
tests/spec/FactorySpec.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace spec\Madkom\NginxConfigurator;
|
||||
|
||||
use Madkom\NginxConfigurator\Config\Location;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Factory;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
|
||||
/**
|
||||
* Class FactorySpec
|
||||
* @package spec\Madkom\NginxConfigurator
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
* @mixin Factory
|
||||
*/
|
||||
class FactorySpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(Factory::class);
|
||||
}
|
||||
|
||||
function it_can_create_Server_node()
|
||||
{
|
||||
$this->createServer(80)->shouldReturnAnInstanceOf(Server::class);
|
||||
}
|
||||
|
||||
function it_can_create_Location_node()
|
||||
{
|
||||
$this->createLocation('/test', '~')->shouldReturnAnInstanceOf(Location::class);
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,15 @@
|
||||
namespace spec\Madkom\NginxConfigurator;
|
||||
|
||||
use Ferno\Loco\ParseFailureException;
|
||||
use Madkom\NginxConfigurator\Config\Http;
|
||||
use Madkom\NginxConfigurator\Config\Location;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Config\Upstream;
|
||||
use Madkom\NginxConfigurator\Node\Context;
|
||||
use Madkom\NginxConfigurator\Node\Directive;
|
||||
use Madkom\NginxConfigurator\Node\Literal;
|
||||
use Madkom\NginxConfigurator\Node\Node;
|
||||
use Madkom\NginxConfigurator\Node\Param;
|
||||
use Madkom\NginxConfigurator\Node\RootNode;
|
||||
use Madkom\NginxConfigurator\Parser;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
@@ -25,7 +29,7 @@ class ParserSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Madkom\NginxConfigurator\Parser');
|
||||
$this->shouldHaveType(Parser::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,4 +188,67 @@ EOF
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function it_can_parse_Upstream_and_Http_contexts()
|
||||
{
|
||||
$root = $this->parse(<<<EOF
|
||||
http {
|
||||
}
|
||||
upstream name {
|
||||
internal;
|
||||
}
|
||||
EOF
|
||||
);
|
||||
$root->shouldReturnAnInstanceOf(RootNode::class);
|
||||
|
||||
$contexts = $root->search(function (Node $node) {
|
||||
return $node;
|
||||
})->getWrappedObject();
|
||||
/** @var Context $context */
|
||||
foreach ($contexts as $index => $context) {
|
||||
switch ($index) {
|
||||
case 0:
|
||||
Assert::assertInstanceOf(Http::class, $context);
|
||||
break;
|
||||
case 1:
|
||||
Assert::assertInstanceOf(Upstream::class, $context);
|
||||
/** @var Directive $directive */
|
||||
foreach ($context as $index => $directive) {
|
||||
switch ($index) {
|
||||
case 0:
|
||||
Assert::assertEquals('internal', $directive->getName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ParseFailureException
|
||||
*/
|
||||
function it_can_parse_Literal_directive()
|
||||
{
|
||||
/** @var RootNode $root */
|
||||
$root = $this->parse(<<<EOF
|
||||
internal "txt";
|
||||
EOF
|
||||
);
|
||||
$root->shouldReturnAnInstanceOf(RootNode::class);
|
||||
|
||||
$directives = $root->search(function (Node $node) {
|
||||
return $node;
|
||||
})->getWrappedObject();
|
||||
/** @var Directive $directive */
|
||||
foreach ($directives as $directive) {
|
||||
break;
|
||||
}
|
||||
Assert::assertEquals($directive->getName(), 'internal');
|
||||
Assert::assertInstanceOf(Traversable::class, $directive->getParams());
|
||||
/** @var Param $param */
|
||||
foreach ($directive->getParams() as $param) {
|
||||
Assert::assertInstanceOf(Literal::class, $param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user