Skip to content

Commit

Permalink
Tools/Tests/Unit/TestsGenerator/Generator.php
Browse files Browse the repository at this point in the history
  • Loading branch information
masicek committed Dec 19, 2011
1 parent 8a1cb97 commit 722c90e
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 1 deletion.
2 changes: 1 addition & 1 deletion TestsGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace XSLTBenchmark\TestsGenerator;

require_once __DIR__ . '/Templating.php';
require_once __DIR__ . '/Templating/Templating.php';
require_once __DIR__ . '/Test.php';
require_once __DIR__ . '/Params/Params.php';
require_once LIBS . '/PhpPath/PhpPath.min.php';
Expand Down
27 changes: 27 additions & 0 deletions Tools/Tests/Unit/TestsGenerator/Generator/AddAndGetTests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<tests name="Test name" template="test.tpl.xslt" templatingType="smarty">
<files>
<file id="one">one.xml</file>
<file id="two">two.xml</file>
<file id="three">three.xml</file>
<generated id="genOne" generator="easy" output="genOne.xml">
<setting name="first">2</setting>
<setting name="second">5</setting>
</generated>
<generated id="genTwo" generator="easy" output="genTwo.xml">
<setting name="element">20</setting>
</generated>
</files>
<test name="First">
<file input="one" output="genOne" />
<file input="two" output="genTwo" />
<setting name="first seting">Lorem ipsum</setting>
<setting name="second setting">123</setting>
</test>
<test name="Second">
<file input="one" output="genOne" />
<file input="two" output="genTwo" />
<file input="three" output="one" />
<setting name="setting">999</setting>
</test>
</tests>
93 changes: 93 additions & 0 deletions Tools/Tests/Unit/TestsGenerator/Generator/AddAndGetTestsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* XSLT Benchmarking
* @link https://github.com/masicek/XSLT-Benchmarking
* @author Viktor Mašíček <[email protected]>
* @license "New" BSD License
*/

namespace Tests\XSLTBenchmark\TestsGenerator\Generator;

require_once ROOT . '/TestsGenerator/Generator.php';
require_once ROOT . '/TestsGenerator/Test.php';

use \Tests\XSLTBenchmark\TestCase;
use \XSLTBenchmark\TestsGenerator\Generator;
use \XSLTBenchmark\TestsGenerator\Test;

/**
* AddAndGetTestsTest
*
* @author Viktor Mašíček <[email protected]>
*
* @covers \XSLTBenchmark\TestsGenerator\Generator::addTests
* @covers \XSLTBenchmark\TestsGenerator\Generator::getTests
*/
class AddAndGetTestsTest extends TestCase
{


public function test()
{
$templates = $this->setDirSep(__DIR__ . '/A');
$templateXYZ = $this->setDirSep(__DIR__ . '/A/XYZ');
$tests = $this->setDirSep(__DIR__ . '/B');
$tmp = $this->setDirSep(__DIR__ . '/C');

mkdir($templates);
mkdir($templateXYZ);
mkdir($tests);
mkdir($tmp);

copy(
$this->setDirSep(__DIR__ . '/AddAndGetTests.xml'),
$this->setDirSep($templateXYZ . '/params.xml')
);

$generator = new Generator($templates, $tests, $tmp);
$generator->addTests('XYZ', 'params.xml');
$addedTests = $generator->getTests();

// make expected tests
$expectedTests = array();

// first test
$test = new Test('');
$this->setPropertyValue($test, 'name', 'Test name - First');
$this->setPropertyValue($test, 'templatePath', $this->setDirSep($templateXYZ . '/test.tpl.xslt'));
$this->setPropertyValue($test, 'templatingType', 'smarty');
$this->setPropertyValue($test, 'path', $this->setDirSep($tests . '/test-name-first'));
$this->setPropertyValue($test, 'settings', array('first seting' => 'Lorem ipsum', 'second setting' => 123));
$this->setPropertyValue($test, 'filesPaths', array(
$this->setDirSep($templateXYZ . '/one.xml') => $this->setDirSep($tmp . '/genOne.xml'),
$this->setDirSep($templateXYZ . '/two.xml') => $this->setDirSep($tmp . '/genTwo.xml'),
));
$expectedTests[] = $test;

// second test
$test = new Test('');
$this->setPropertyValue($test, 'name', 'Test name - Second');
$this->setPropertyValue($test, 'templatePath', $this->setDirSep($templateXYZ . '/test.tpl.xslt'));
$this->setPropertyValue($test, 'templatingType', 'smarty');
$this->setPropertyValue($test, 'path', $this->setDirSep($tests . '/test-name-second'));
$this->setPropertyValue($test, 'settings', array('setting' => 999));
$this->setPropertyValue($test, 'filesPaths', array(
$this->setDirSep($templateXYZ . '/one.xml') => $this->setDirSep($tmp . '/genOne.xml'),
$this->setDirSep($templateXYZ . '/two.xml') => $this->setDirSep($tmp . '/genTwo.xml'),
$this->setDirSep($templateXYZ . '/three.xml') => $this->setDirSep($templateXYZ . '/one.xml'),
));
$expectedTests[] = $test;

$this->assertEquals($expectedTests, $addedTests);

unlink($this->setDirSep($templateXYZ . '/params.xml'));
rmdir($templateXYZ);
rmdir($templates);
rmdir($tests);
unlink($this->setDirSep($tmp . '/genOne.xml'));
unlink($this->setDirSep($tmp . '/genTwo.xml'));
rmdir($tmp);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
My default text

Lorem ipsum 1.1
Lorem ipsum 1.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
My default text

Lorem ipsum 2.1
Lorem ipsum 2.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<test template="template.xslt">
<couple input="one.xml" output="genOne.xml"/>
<couple input="two.xml" output="genTwo.xml"/>
</test>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<test template="template.xslt">
<couple input="one.xml" output="genOne.xml"/>
</test>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
My default text

{$testVariable1}
{$testVariable2}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
147 changes: 147 additions & 0 deletions Tools/Tests/Unit/TestsGenerator/Generator/GenerateAllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* XSLT Benchmarking
* @link https://github.com/masicek/XSLT-Benchmarking
* @author Viktor Mašíček <[email protected]>
* @license "New" BSD License
*/

namespace Tests\XSLTBenchmark\TestsGenerator\Generator;

require_once ROOT . '/TestsGenerator/Generator.php';
require_once ROOT . '/TestsGenerator/Test.php';

use \Tests\XSLTBenchmark\TestCase;
use \XSLTBenchmark\TestsGenerator\Generator;
use \XSLTBenchmark\TestsGenerator\Test;

/**
* AddAndGetTestsTest
*
* @author Viktor Mašíček <[email protected]>
*
* @covers \XSLTBenchmark\TestsGenerator\Generator::generateAll
* @covers \XSLTBenchmark\TestsGenerator\Generator::generateTest
* @covers \XSLTBenchmark\TestsGenerator\Generator::generateInputOutputCouples
*/
class GenerateAllTest extends TestCase
{


public function test()
{
$fixture = $this->setDirSep(__DIR__ . '/FixtureGenerateAll');
$tests = $this->setDirSep(__DIR__ . '/tests');
mkdir($tests);

$generator = new Generator(__DIR__, __DIR__, __DIR__);

// make tests
$testsList = array();

// first test
$test = new Test('');
$this->setPropertyValue($test, 'name', 'Test name - First');
$this->setPropertyValue($test, 'templatePath', $this->setDirSep($fixture . '/template.tpl.xslt'));
$this->setPropertyValue($test, 'templatingType', 'smarty');
$this->setPropertyValue($test, 'path', $this->setDirSep($tests . '/test-name-first'));
$this->setPropertyValue($test, 'settings', array('testVariable1' => 'Lorem ipsum 1.1', 'testVariable2' => 'Lorem ipsum 1.2'));
$this->setPropertyValue($test, 'filesPaths', array(
$this->setDirSep($fixture . '/one.xml') => $this->setDirSep($fixture . '/genOne.xml'),
$this->setDirSep($fixture . '/two.xml') => $this->setDirSep($fixture . '/genTwo.xml'),
));
$testsList[] = $test;

// second test
$test = new Test('');
$this->setPropertyValue($test, 'name', 'Test name - Second');
$this->setPropertyValue($test, 'templatePath', $this->setDirSep($fixture . '/template.tpl.xslt'));
$this->setPropertyValue($test, 'templatingType', 'smarty');
$this->setPropertyValue($test, 'path', $this->setDirSep($tests . '/test-name-second'));
$this->setPropertyValue($test, 'settings', array('testVariable1' => 'Lorem ipsum 2.1', 'testVariable2' => 'Lorem ipsum 2.2'));
$this->setPropertyValue($test, 'filesPaths', array(
$this->setDirSep($fixture . '/one.xml') => $this->setDirSep($fixture . '/genOne.xml'),
));
$testsList[] = $test;

$this->setPropertyValue($generator, 'templates', $testsList);

// fisrt test
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/template.xslt'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/one.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/genOne.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/two.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/genTwo.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-first/__params.xml'));
// second test
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-second/template.xslt'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-second/one.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-second/genOne.xml'));
$this->assertFileNotExists($this->setDirSep($tests . '/test-name-second/__params.xml'));

// generate all tests from template
$generator->generateAll();

// fisrt test
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/template.xslt'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/one.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/genOne.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/two.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/genTwo.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-first/__params.xml'));
// second test
$this->assertFileExists($this->setDirSep($tests . '/test-name-second/template.xslt'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-second/one.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-second/genOne.xml'));
$this->assertFileExists($this->setDirSep($tests . '/test-name-second/__params.xml'));

// content
$this->assertFileEquals(
$this->setDirSep($fixture . '/expectedTemplate1.xslt'),
$this->setDirSep($tests . '/test-name-first/template.xslt')
);
$this->assertFileEquals(
$this->setDirSep($fixture . '/expectedTemplate2.xslt'),
$this->setDirSep($tests . '/test-name-second/template.xslt')
);
$this->assertXmlFileEqualsXmlFile(
$this->setDirSep($fixture . '/expected__params1.xml'),
$this->setDirSep($tests . '/test-name-first/__params.xml')
);
$this->assertXmlFileEqualsXmlFile(
$this->setDirSep($fixture . '/expected__params2.xml'),
$this->setDirSep($tests . '/test-name-second/__params.xml')
);


// remove all generated files and directories
// fisrt test
unlink($this->setDirSep($tests . '/test-name-first/template.xslt'));
unlink($this->setDirSep($tests . '/test-name-first/one.xml'));
unlink($this->setDirSep($tests . '/test-name-first/genOne.xml'));
unlink($this->setDirSep($tests . '/test-name-first/two.xml'));
unlink($this->setDirSep($tests . '/test-name-first/genTwo.xml'));
unlink($this->setDirSep($tests . '/test-name-first/__params.xml'));
rmdir($this->setDirSep($tests . '/test-name-first'));
// second test
unlink($this->setDirSep($tests . '/test-name-second/template.xslt'));
unlink($this->setDirSep($tests . '/test-name-second/one.xml'));
unlink($this->setDirSep($tests . '/test-name-second/genOne.xml'));
unlink($this->setDirSep($tests . '/test-name-second/__params.xml'));
rmdir($this->setDirSep($tests . '/test-name-second'));

rmdir($tests);

// remove temporary file
$files = scandir(__DIR__);
foreach ($files as $file)
{
if (strpos($file, '.file.template.tpl.xslt.php') !== FALSE)
{
unlink($this->setDirSep(__DIR__ . '/' . $file));
}
}
}

}
74 changes: 74 additions & 0 deletions Tools/Tests/Unit/TestsGenerator/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* XSLT Benchmarking
* @link https://github.com/masicek/XSLT-Benchmarking
* @author Viktor Mašíček <[email protected]>
* @license "New" BSD License
*/

namespace Tests\XSLTBenchmark\TestsGenerator\Generator;

require_once ROOT . '/TestsGenerator/Generator.php';

use \Tests\XSLTBenchmark\TestCase;
use \XSLTBenchmark\TestsGenerator\Generator;

/**
* GeneratorTest
*
* @author Viktor Mašíček <[email protected]>
*
* @covers \XSLTBenchmark\TestsGenerator\Generator::__construct
*/
class GeneratorTest extends TestCase
{


public function testOk()
{
$templates = $this->setDirSep(__DIR__ . '/A');
$tests = $this->setDirSep(__DIR__ . '/B');
$tmp = $this->setDirSep(__DIR__ . '/C');

mkdir($templates);
mkdir($tests);
mkdir($tmp);

$generator = new Generator($templates, $tests, $tmp);

$this->assertEquals($templates, $this->getPropertyValue($generator, 'templatesDirectory'));
$this->assertEquals($tests, $this->getPropertyValue($generator, 'testsDirectory'));
$this->assertEquals($tmp, $this->getPropertyValue($generator, 'tmpDirectory'));

rmdir($templates);
rmdir($tests);
rmdir($tmp);
}


public function testBadTemplatedDir()
{
$unknown = $this->setDirSep(__DIR__ . '/unknown');
$this->setExpectedException('\PhpPath\NotExistsPathException');
$generator = new Generator($unknown, __DIR__, __DIR__);
}


public function testBadTestsDir()
{
$unknown = $this->setDirSep(__DIR__ . '/unknown');
$this->setExpectedException('\PhpPath\NotExistsPathException');
$generator = new Generator(__DIR__, $unknown, __DIR__);
}


public function testBadTmpDir()
{
$unknown = $this->setDirSep(__DIR__ . '/unknown');
$this->setExpectedException('\PhpPath\NotExistsPathException');
$generator = new Generator(__DIR__, __DIR__, $unknown);
}


}

0 comments on commit 722c90e

Please sign in to comment.