Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Taysir Tayyab committed Aug 9, 2016
1 parent 7afe8d7 commit 271a3c5
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 3 deletions.
10 changes: 10 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash +x

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

ARGS=''
for i in "$@"; do
ARGS="$ARGS ${i}"
done

$DIR/../vendor/phpunit/phpunit/phpunit --configuration=phpunit.xml $ARGS
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap="tests/bootstrap.php"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="m00t Behat Partial Runner Tests">
<directory>tests/M00t/Behat/PartialRunner</directory>
<testsuite name="Behat Partial Runner Tests">
<directory>tests/Behat/PartialRunner</directory>
</testsuite>
</testsuites>
</phpunit>
81 changes: 81 additions & 0 deletions tests/Behat/PartialRunner/Filter/FilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php namespace Tests\Behat\ParallelRunner\Filter;

use Behat\Gherkin\Keywords\ArrayKeywords;
use Behat\Gherkin\Lexer;
use Behat\Gherkin\Parser;
use PHPUnit\Framework\TestCase;

/**
* Class FilterTest.
*
* Base class for filter testing which sets up a Gherking Feature with several scenarios and a parser.
*/
abstract class FilterTest extends TestCase
{
/**
* @return Parser
*/
protected function getParser()
{
return new Parser(
new Lexer(
new ArrayKeywords([
'en' => [
'feature' => 'Feature',
'background' => 'Background',
'scenario' => 'Scenario',
'scenario_outline' => 'Scenario Outline|Scenario Template',
'examples' => 'Examples|Scenarios',
'given' => 'Given',
'when' => 'When',
'then' => 'Then',
'and' => 'And',
'but' => 'But',
],
])
)
);
}

/**
* @return string
*/
protected function getGherkinFeature()
{
return <<<'GHERKIN'
Feature: Long feature with outline
In order to accomplish objective
As a someone
I have to be able to do something
Scenario: Scenario#1
Given initial step
When action occurs
Then outcomes should be visible
Scenario: Scenario#2
Given initial step
And another initial step
When action occurs
Then outcomes should be visible
Scenario Outline: Scenario#3
When <action> occurs
Then <outcome> should be visible
Examples:
| action | outcome |
| act#1 | out#1 |
| act#2 | out#2 |
| act#3 | out#3 |
GHERKIN;
}

/**
* @return \Behat\Gherkin\Node\FeatureNode|null
*/
protected function getParsedFeature()
{
return $this->getParser()->parse($this->getGherkinFeature());
}
}
275 changes: 275 additions & 0 deletions tests/Behat/PartialRunner/Filter/PartialRunnerFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
<?php namespace Tests\Behat\ParallelRunner\Filter;

use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\ScenarioNode;
use Exception;
use InvalidArgumentException;
use M00t\Behat\PartialRunner\Filter\PartialRunnerFilter;

class ParallelWorkerFilterTest extends FilterTest
{
/**
* This test is for making sure that invalid arguments for construction properly except.
*/
public function testParallelWorkerFilter()
{
// message check
try {
new PartialRunnerFilter(10, -10);
$this->expectException(InvalidArgumentException::class);
} catch (Exception $e) {
$this->assertEquals('Received bad arguments for ($countWorkers, $workerNumber): (10, -10).', $e->getMessage());
}

/***************************
* Invalid Arguments *
**************************/
try {
new PartialRunnerFilter(1, -1);
$this->expectException(InvalidArgumentException::class);
} catch (Exception $e) {
$this->assertTrue($e instanceof InvalidArgumentException);
}

try {
new PartialRunnerFilter(0, 0);
$this->expectException(InvalidArgumentException::class);
} catch (Exception $e) {
$this->assertTrue($e instanceof InvalidArgumentException);
}

try {
new PartialRunnerFilter(-1, -1);
$this->expectException(InvalidArgumentException::class);
} catch (Exception $e) {
$this->assertTrue($e instanceof InvalidArgumentException);
}

try {
new PartialRunnerFilter(1, 2);
$this->expectException(InvalidArgumentException::class);
} catch (Exception $e) {
$this->assertTrue($e instanceof InvalidArgumentException);
}
}

/**
* This test makes sure that isFeatureMatch is always false, regardless of the construct arguments on the filter.
*/
public function testIsFeatureMatch()
{
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);

$filter = new PartialRunnerFilter();
$this->assertFalse($filter->isFeatureMatch($feature));

$filter = new PartialRunnerFilter(2, 1);
$this->assertFalse($filter->isFeatureMatch($feature));

$filter = new PartialRunnerFilter(5, 2);
$this->assertFalse($filter->isFeatureMatch($feature));
}

/**
* This test makes sure that isScenarioMatch is always true, regardless of the construct arguments on the filter.
*/
public function testIsScenarioMatch()
{
$scenario = new ScenarioNode(null, [], [], null, 2);

$filter = new PartialRunnerFilter();
$this->assertTrue($filter->isScenarioMatch($scenario));

$filter = new PartialRunnerFilter(2, 1);
$this->assertTrue($filter->isScenarioMatch($scenario));

$filter = new PartialRunnerFilter(5, 2);
$this->assertTrue($filter->isScenarioMatch($scenario));
}

/**
* This tests that FeatureFilter works correctly with the default construction arguments for the filter.
*/
public function testFeatureFilterDefaults()
{
$filter = new PartialRunnerFilter();
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 3);
$this->assertEquals('Scenario#1', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#2', $scenarios[1]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[2]->getTitle());

$this->assertTrue($scenarios[2] instanceof OutlineNode);
$this->assertTrue($scenarios[2]->hasExamples());
$this->assertEquals([
['action' => 'act#1', 'outcome' => 'out#1'],
['action' => 'act#2', 'outcome' => 'out#2'],
['action' => 'act#3', 'outcome' => 'out#3'],
], $scenarios[2]->getExampleTable()->getColumnsHash());
}

/**
* This tests that FeatureFilter works properly when there are 2 test nodes.
*/
public function testFeatureFilterNodes2()
{
/*****************
* Node 1 *
****************/
$filter = new PartialRunnerFilter(2, 0);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 2);
$this->assertEquals('Scenario#1', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[1]->getTitle());

$this->assertTrue($scenarios[1] instanceof OutlineNode);
$this->assertTrue($scenarios[1]->hasExamples());
$this->assertEquals([
['action' => 'act#1', 'outcome' => 'out#1'],
['action' => 'act#3', 'outcome' => 'out#3'],
], $scenarios[1]->getExampleTable()->getColumnsHash());

/*****************
* Node 2 *
****************/
$filter = new PartialRunnerFilter(2, 1);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 2);
$this->assertEquals('Scenario#2', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[1]->getTitle());

$this->assertTrue($scenarios[1] instanceof OutlineNode);
$this->assertTrue($scenarios[1]->hasExamples());
$this->assertEquals([
['action' => 'act#2', 'outcome' => 'out#2'],
], $scenarios[1]->getExampleTable()->getColumnsHash());
}

/**
* This tests if FeatureFilter works properly when there are 3 test nodes.
*/
public function testFeatureFilterNodes3()
{
/*****************
* Node 1 *
****************/
$filter = new PartialRunnerFilter(3, 0);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 2);
$this->assertEquals('Scenario#1', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[1]->getTitle());

$this->assertTrue($scenarios[1] instanceof OutlineNode);
$this->assertTrue($scenarios[1]->hasExamples());
$this->assertEquals([
['action' => 'act#2', 'outcome' => 'out#2'],
], $scenarios[1]->getExampleTable()->getColumnsHash());

/*****************
* Node 2 *
****************/
$filter = new PartialRunnerFilter(3, 1);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 2);
$this->assertEquals('Scenario#2', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[1]->getTitle());

$this->assertTrue($scenarios[1] instanceof OutlineNode);
$this->assertTrue($scenarios[1]->hasExamples());
$this->assertEquals([
['action' => 'act#3', 'outcome' => 'out#3'],
], $scenarios[1]->getExampleTable()->getColumnsHash());

/*****************
* Node 3 *
****************/
$filter = new PartialRunnerFilter(3, 2);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 1);
$this->assertEquals('Scenario#3', $scenarios[0]->getTitle());

$this->assertTrue($scenarios[0] instanceof OutlineNode);
$this->assertTrue($scenarios[0]->hasExamples());
$this->assertEquals([
['action' => 'act#1', 'outcome' => 'out#1'],
], $scenarios[0]->getExampleTable()->getColumnsHash());
}

/**
* This tests if FeatureFilter works properly when there are 4 test nodes.
*/
public function testFeatureFilterNodes4()
{
/*****************
* Node 1 *
****************/
$filter = new PartialRunnerFilter(4, 0);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 2);
$this->assertEquals('Scenario#1', $scenarios[0]->getTitle());
$this->assertEquals('Scenario#3', $scenarios[1]->getTitle());

$this->assertTrue($scenarios[1] instanceof OutlineNode);
$this->assertTrue($scenarios[1]->hasExamples());
$this->assertEquals([
['action' => 'act#3', 'outcome' => 'out#3'],
], $scenarios[1]->getExampleTable()->getColumnsHash());

/*****************
* Node 2 *
****************/
$filter = new PartialRunnerFilter(4, 1);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 1);
$this->assertEquals('Scenario#2', $scenarios[0]->getTitle());

/*****************
* Node 3 *
****************/
$filter = new PartialRunnerFilter(4, 2);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 1);
$this->assertEquals('Scenario#3', $scenarios[0]->getTitle());

$this->assertTrue($scenarios[0] instanceof OutlineNode);
$this->assertTrue($scenarios[0]->hasExamples());
$this->assertEquals([
['action' => 'act#1', 'outcome' => 'out#1'],
], $scenarios[0]->getExampleTable()->getColumnsHash());

/*****************
* Node 4 *
****************/
$filter = new PartialRunnerFilter(4, 3);
$feature = $filter->filterFeature($this->getParsedFeature());
$scenarios = $feature->getScenarios();

$this->assertEquals(count($scenarios), 1);
$this->assertEquals('Scenario#3', $scenarios[0]->getTitle());

$this->assertTrue($scenarios[0] instanceof OutlineNode);
$this->assertTrue($scenarios[0]->hasExamples());
$this->assertEquals([
['action' => 'act#2', 'outcome' => 'out#2'],
], $scenarios[0]->getExampleTable()->getColumnsHash());
}
}

0 comments on commit 271a3c5

Please sign in to comment.