Skip to content

Commit

Permalink
Merge pull request #66 from 10up/feature/behat-tests
Browse files Browse the repository at this point in the history
First set of behavioral tests
  • Loading branch information
johnpbloch authored Sep 20, 2016
2 parents 6fc53f5 + a6e4e80 commit bf984be
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 1 deletion.
9 changes: 9 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default:
suites:
default:
contexts:
- FeatureContext
- HooksContext
- FunctionsContext
formatters:
progress: true
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"antecedent/patchwork": "~1.2"
},
"require-dev": {
"behat/behat" : "^3.0"
},
"autoload" : {
"psr-4" : {"WP_Mock\\": "./php/WP_Mock"},
Expand All @@ -23,5 +24,11 @@
"platform": {
"php": "5.3.3"
}
},
"scripts": {
"test:behat": "behat",
"test" : [
"@test:behat"
]
}
}
68 changes: 68 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}

/**
* @BeforeScenario
*/
public function setUpWpMock(BeforeScenarioScope $scope)
{
WP_Mock::setUp();
}

/**
* @AfterScenario
*/
public function tearDownWpMock(AfterScenarioScope $scope)
{
WP_Mock::tearDown();
}

/**
* @Then tearDown should not fail
*/
public function teardownShouldNotFail()
{
WP_Mock::tearDown();
}

/**
* @When I do nothing
*/
public function iDoNothing()
{
// Move along...
}

/**
* @Then tearDown should fail
*/
public function teardownShouldFail()
{
try {
$this->teardownShouldNotFail();
throw new PHPUnit_Framework_ExpectationFailedException('WP_Mock Teardown should have failed!');
} catch (\Mockery\Exception\InvalidCountException $e) {
// Move along
}
}
}
80 changes: 80 additions & 0 deletions features/bootstrap/FunctionsContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Mockery\Exception\NoMatchingExpectationException;

class FunctionsContext implements Context
{

/**
* @Given function :function does not exist
*/
public function functionDoesNotExist($function)
{
PHPUnit_Framework_Assert::assertFalse(function_exists($function));
}

/**
* @Given I mock passthru function :function with args:
*/
public function iMockPassthruFunctionWithArgs($function, TableNode $args)
{
WP_Mock::passthruFunction($function, array(
'args' => $args->getRow(0),
));
}

/**
* @Given I mock function :function to return :value
*/
public function iMockFunctionToReturn($function, $value)
{
WP_Mock::userFunction($function, array('return' => $value));
}

/**
* @When I mock function :function
*/
public function iMockFunction($function)
{
WP_Mock::userFunction($function);
}

/**
* @Then function :function should exist
*/
public function functionShouldExist($function)
{
PHPUnit_Framework_Assert::assertTrue(function_exists($function));
}

/**
* @Then I expect :return when I run :function with args:
*/
public function iExpectWhenIRunWithArgs($return, $function, TableNode $args)
{
PHPUnit_Framework_Assert::assertEquals($return, call_user_func_array($function, $args->getRow(0)));
}

/**
* @Then I expect :return when I run :function
*/
public function iExcpectWhenIRun($return, $function)
{
$this->iExpectWhenIRunWithArgs($return, $function, new TableNode(array(array())));
}

/**
* @Then I expect an error when I run :function with args:
*/
public function iExpectAnErrorWhenIRunWithArgs($function, TableNode $args)
{
try {
$this->iExpectWhenIRunWithArgs(null, $function, $args);
} catch (NoMatchingExpectationException $e) {
// Move along...
}
}

}
197 changes: 197 additions & 0 deletions features/bootstrap/HooksContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;

class HooksContext implements Context
{

private $filterResults = array();

/**
* @BeforeScenario
*/
public function setUpWpMock(BeforeScenarioScope $scope)
{
$this->filterResults = array();
}

/**
* @AfterScenario
*/
public function tearDownWpMock(AfterScenarioScope $scope)
{
$this->filterResults = array();
}

/**
* @Given I expect the following actions added:
*/
public function iExpectTheFollowingActionsAdded(TableNode $table)
{
foreach ($this->getActionsWithDefaults($table) as $action) {
WP_Mock::expectActionAdded(
$action['action'],
$action['callback'],
$action['priority'],
$action['arguments']
);
}
}

/**
* @Given I expect the :action action
*/
public function iExpectTheAction($action)
{
$this->iExpectTheActionWith($action, new TableNode(array()));
}

/**
* @When I expect the :action action with:
*/
public function iExpectTheActionWith($action, TableNode $table)
{
$args = array($action);
$rows = $table->getRows();
if (isset( $rows[0] ) && is_array($rows[0])) {
$args = array_merge($args, $rows[0]);
}
call_user_func_array(array('WP_Mock', 'expectAction'), $args);
}

/**
* @When I add the following actions:
*/
public function iAddTheFollowingActions(TableNode $table)
{
foreach ($this->getActionsWithDefaults($table) as $action) {
add_action(
$action['action'],
$action['callback'],
$action['priority'],
$action['arguments']
);
}
}

/**
* @When I do the :action action
*/
public function iDoTheAction($action)
{
$this->iDoTheActionWith($action, new TableNode(array()));
}

/**
* @When I do the :action action with:
*/
public function iDoTheActionWith($action, TableNode $table)
{
$args = array($action);
$rows = $table->getRows();
if (isset( $rows[0] ) && is_array($rows[0])) {
$args = array_merge($args, $rows[0]);
}
call_user_func_array('do_action', $args);
}

/**
* @Given I expect the following filters added:
*/
public function iExpectTheFollowingFiltersAdded(TableNode $table)
{
$filters = $table->getHash();
$defaults = array(
'filter' => '',
'callback' => '',
'priority' => 10,
'arguments' => 1,
);
foreach ($filters as $filter) {
$filter += $defaults;
WP_Mock::expectFilterAdded(
$filter['filter'],
$filter['callback'],
$filter['priority'],
$filter['arguments']
);
}
}

/**
* @When I add the following filters:
*/
public function iAddTheFollowingFilters(TableNode $table)
{
$filters = $table->getHash();
$defaults = array(
'filter' => '',
'callback' => '',
'priority' => 10,
'arguments' => 1,
);
foreach ($filters as $filter) {
$filter += $defaults;
add_filter(
$filter['filter'],
$filter['callback'],
$filter['priority'],
$filter['arguments']
);
}
}

/**
* @Given I expect filter :filter to respond to :thing with :response
*/
public function iExpectFilterToRespondToWith($filter, $thing, $response)
{
WP_Mock::onFilter($filter)->with($thing)->reply($response);
}

/**
* @Given I expect filter :filter to respond with :response
*/
public function iExpectFilterToRespondWith($filter, $response)
{
$this->iExpectFilterToRespondToWith($filter, null, $response);
}

/**
* @When I apply the filter :filter with :with
*/
public function iApplyFilterWith($filter, $with)
{
$this->filterResults[$filter] = apply_filters($filter, $with);
}

/**
* @Then The filter :filter should return :value
*/
public function theFilterShouldReturn($filter, $value)
{
PHPUnit_Framework_Assert::assertArrayHasKey($filter, $this->filterResults);
PHPUnit_Framework_Assert::assertEquals($this->filterResults[$filter], $value);
}

private function getActionsWithDefaults(TableNode $table)
{
$actions = $table->getHash();
$defaults = array(
'action' => '',
'callback' => '',
'priority' => 10,
'arguments' => 1,
);
foreach ($actions as &$action) {
$action += $defaults;
}
unset( $action );

return $actions;
}

}
Loading

0 comments on commit bf984be

Please sign in to comment.