Skip to content

Commit

Permalink
Added config tags
Browse files Browse the repository at this point in the history
  • Loading branch information
phphatesme committed Feb 23, 2011
1 parent ad1c9a3 commit 787450c
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (C) 2011 by Nils Langner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
92 changes: 92 additions & 0 deletions src/LiveTest/Config/Config.php
@@ -0,0 +1,92 @@
<?php

namespace LiveTest\Config;

class Config
{
private $includedPages = array();
private $excludedPages = array();

private $testCases = array();
private $inherit = true;

private $baseDir;

private $parentConfig;

public function __construct(Config $parentConfig = null)
{
$this->parentConfig = $parentConfig;
}

public function setBaseDir($baseDir)
{
$this->baseDir = $baseDir;
}

public function getBaseDir()
{
if (is_null($this->baseDir))
{
return $this->parentConfig->getBaseDir();
}
return $this->baseDir;
}

public function includePage($page)
{
$this->includedPages[$page] = $page;
}

public function includePages($pages)
{
foreach( $pages as $page )
{
$this->includePage(trim($page));
}
}

public function excludePage($page)
{
$this->excludedPages[$page] = $page;
}

public function doNotInherit()
{
$this->inherit = false;
}

public function clearPages()
{
$this->includedPages = array();
$this->excludedPages = array();
}

public function createTestCase($name, $className, $parameters)
{
$config = new self($this);

$this->testCases[] = array('config' => $config,'className' => $className,'parameters' => $parameters);

return $config;
}

public function getPages()
{
if ($this->inherit && !is_null($this->parentConfig))
{
$result = array_merge($this->includedPages, $this->parentConfig->getPages());
}
else
{
$result = $this->includedPages;
}

return array_diff($result, $this->excludedPages);
}

public function getTestCases()
{
return $this->testCases;
}
}
41 changes: 41 additions & 0 deletions src/LiveTest/Config/Parser.php
@@ -0,0 +1,41 @@
<?php

namespace LiveTest\Config;

class Parser
{
private $configArray;
private $config;
private $registeredTags;

public function __construct()
{
// @todo This should be done automaticly
$this->registeredTags = array('TestSuite' => 'LiveTest\Config\Tags\TestSuite',
'TestCases' => 'LiveTest\Config\Tags\TestCases',
'TestCase' => 'LiveTest\Config\Tags\TestCase',
'Pages' => 'LiveTest\Config\Tags\Pages',
'IncludePages' => 'LiveTest\Config\Tags\IncludePages',
'ExcludePages' => 'LiveTest\Config\Tags\ExcludePages',
'PageFiles' => 'LiveTest\Config\Tags\PageFiles' );
}

public function parse(array $configArray, Config $config)
{
foreach ($configArray as $configTag => $value)
{
if (array_key_exists($configTag, $this->registeredTags))
{
$tagClassName = $this->registeredTags[$configTag];
$tag = new $tagClassName($value, $config, $this);
$config = $tag->process();
}
else
{
throw new \Exception('Unknown tag (' . $configTag . ')');
}
}

return $config;
}
}
35 changes: 35 additions & 0 deletions src/LiveTest/Config/Tags/Base.php
@@ -0,0 +1,35 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;
use LiveTest\Config\Config;

abstract class Base implements Tag
{
private $config;
private $parser;
private $configParameters;

public function __construct(array $configParameters, Config $config, Parser $parser)
{
$this->config = $config;
$this->parser = $parser;
$this->configParameters = $configParameters;
}

public function getConfig( )
{
return $this->config;
}

protected function getParser( )
{
return $this->parser;
}

protected function getParameters( )
{
return $this->configParameters;
}
}
20 changes: 20 additions & 0 deletions src/LiveTest/Config/Tags/ExcludePages.php
@@ -0,0 +1,20 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;
use LiveTest\Config\Config;

class ExcludePages extends Base
{
public function process()
{
$config = $this->getConfig();

foreach ($this->getParameters() as $page)
{
$config->excludePage($page);
}
return $config;
}
}
17 changes: 17 additions & 0 deletions src/LiveTest/Config/Tags/IncludePages.php
@@ -0,0 +1,17 @@
<?php

namespace LiveTest\Config\Tags;

class IncludePages extends Base
{
public function process()
{
$config = $this->getConfig();

foreach ($this->getParameters() as $page)
{
$config->includePage($page);
}
return $config;
}
}
23 changes: 23 additions & 0 deletions src/LiveTest/Config/Tags/PageFiles.php
@@ -0,0 +1,23 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;
use LiveTest\Config\Config;

class PageFiles extends Base
{
public function process()
{
$config = $this->getConfig();
$config->doNotInherit();

$parameters = $this->getParameters();

foreach ($this->getParameters() as $file)
{
$config->includePages( file( $config->getBaseDir().'/'.$file ) );
}
return $config;
}
}
21 changes: 21 additions & 0 deletions src/LiveTest/Config/Tags/Pages.php
@@ -0,0 +1,21 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;
use LiveTest\Config\Config;

class Pages extends Base
{
public function process()
{
$config = $this->getConfig();
$config->doNotInherit();

foreach ($this->getParameters() as $page)
{
$config->includePage($page);
}
return $config;
}
}
12 changes: 12 additions & 0 deletions src/LiveTest/Config/Tags/Tag.php
@@ -0,0 +1,12 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;

use LiveTest\Config\Config;

interface Tag
{
public function __construct(array $configParameters, Config $config, Parser $parser);
}
27 changes: 27 additions & 0 deletions src/LiveTest/Config/Tags/TestCases.php
@@ -0,0 +1,27 @@
<?php

namespace LiveTest\Config\Tags;

class TestCases extends Base
{
public function process()
{
$config = $this->getConfig();
foreach ($this->getParameters() as $name => $value)
{
$parameters = array();
if (array_key_exists('Parameter', $value))
{
$parameters = $value['Parameter'];
}

$testCaseConfig = $this->getConfig()->createTestCase($name, $value['TestCase'], $parameters);

unset($value['Parameter']);
unset($value['TestCase']);

$this->getParser()->parse($value, $testCaseConfig);
}
return $config;
}
}
26 changes: 26 additions & 0 deletions src/LiveTest/Config/Tags/TestSuite.php
@@ -0,0 +1,26 @@
<?php

namespace LiveTest\Config\Tags;

use LiveTest\Config\Parser;
use LiveTest\Config\Config;

class TestSuite implements Tag
{
private $config;
private $parser;
private $configParameters;

public function __construct(array $configParameters, Config $config, Parser $parser)
{
$this->config = $config;
$this->parser = $parser;
$this->configParameters = $configParameters;
}

public function process( )
{
$this->parser->parse(($this->configParameters), $this->config);
return $this->config;
}
}
30 changes: 30 additions & 0 deletions test/Unit/LiveTest/Config/ParserTest.php
@@ -0,0 +1,30 @@
<?php

namespace Test\Unit\LiveTest\Parser;

use Base\Config\Yaml;

use LiveTest\Config\Config;

use LiveTest\Config\Parser;

class ParserTest extends \PHPUnit_Framework_TestCase
{
public function testParser()
{
$config = new Config();
$config->setBaseDir(__DIR__ . '/fixtures/');
$configYaml = new Yaml(__DIR__ . '/fixtures/testsuite.yml');

$parser = new Parser();
$parsedConfig = $parser->parse($configYaml->toArray(), $config);

foreach ($parsedConfig->getTestCases() as $testCase)
{
\Base\Debug\DebugHelper::doVarDump($testCase['className']);
\Base\Debug\DebugHelper::doVarDump($testCase['config']->getPages());
}

\Base\Debug\DebugHelper::doVarDump($parsedConfig->getPages());
}
}
2 changes: 2 additions & 0 deletions test/Unit/LiveTest/Config/fixtures/pagefile.txt
@@ -0,0 +1,2 @@
http://www.example.com
http://www.example.com/index.php

0 comments on commit 787450c

Please sign in to comment.