Skip to content

Commit

Permalink
Merge pull request #48 from lucadegasperi/2.2
Browse files Browse the repository at this point in the history
Experimental Behat integration
  • Loading branch information
crynobone committed Jun 20, 2014
2 parents 4c19a9f + 81eb72b commit d038aa2
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"require": {
"php": ">=5.4.0",
"laravel/framework": "4.2.*"
"laravel/framework": "4.2.*",
"behat/behat": "2.5.*"
},
"extra": {
"branch-alias": {
Expand Down
4 changes: 4 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ title: Testbench Change Log

## Version 2.2 {#v2-2}

### v2.2.1 {#v2-2}

* Add experimental support for Behat when testing packages without a server

### v2.2.0 {#v2-2}

* Bump minimum version to PHP v5.4.0.
Expand Down
49 changes: 49 additions & 0 deletions src/Testbench/BehatFeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php namespace Orchestra\Testbench;

use Behat\Behat\Context\BehatContext;
use Illuminate\Auth\UserInterface;
use Orchestra\Testbench\Traits\ApplicationClientTrait;
use Orchestra\Testbench\Traits\ApplicationTrait;
use Orchestra\Testbench\Traits\BehatPHPUnitAssertionsTrait;

abstract class BehatFeatureContext extends BehatContext implements TestCaseInterface
{
use ApplicationTrait;
use ApplicationClientTrait;
use BehatPHPUnitAssertionsTrait;

/**
* Initializes context.
* Every scenario gets its own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters)
{
$this->setUp();
}

/**
* Setup the test environment.
*
* @return void
*/
public function setUp()
{
if (! $this->app) {
$this->refreshApplication();
}
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Define your environment setup.
}
}

207 changes: 207 additions & 0 deletions src/Testbench/Traits/BehatPHPUnitAssertionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php namespace Orchestra\Testbench\Traits;

use Illuminate\View\View;
use PHPUnit_Framework_Assert as PHPUnit;

trait BehatPHPUnitAssertionsTrait
{
/**
* Assert that the client response has an OK status code.
*
* @return void
*/
public function assertResponseOk()
{
$response = $this->client->getResponse();

$actual = $response->getStatusCode();

return PHPUnit::assertTrue($response->isOk(), 'Expected status code 200, got ' .$actual);
}

/**
* Assert that the client response has a given code.
*
* @param int $code
* @return void
*/
public function assertResponseStatus($code)
{
return PHPUnit::assertEquals($code, $this->client->getResponse()->getStatusCode());
}

/**
* Assert that the response view has a given piece of bound data.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function assertViewHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertViewHasAll($key);
}

$response = $this->client->getResponse();

if (! isset($response->original) || ! $response->original instanceof View) {
return PHPUnit::assertTrue(false, 'The response was not a view.');
}

if (is_null($value)) {
PHPUnit::assertArrayHasKey($key, $response->original->getData());
} else {
PHPUnit::assertEquals($value, $response->original->$key);
}
}

/**
* Assert that the view has a given list of bound data.
*
* @param array $bindings
* @return void
*/
public function assertViewHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertViewHas($value);
} else {
$this->assertViewHas($key, $value);
}
}
}

/**
* Assert that the response view is missing a piece of bound data.
*
* @param string $key
* @return void
*/
public function assertViewMissing($key)
{
$response = $this->client->getResponse();

if (! isset($response->original) || ! $response->original instanceof View) {
return PHPUnit::assertTrue(false, 'The response was not a view.');
}

PHPUnit::assertArrayNotHasKey($key, $response->original->getData());
}

/**
* Assert whether the client was redirected to a given URI.
*
* @param string $uri
* @param array $with
* @return void
*/
public function assertRedirectedTo($uri, $with = array())
{
$response = $this->client->getResponse();

PHPUnit::assertInstanceOf('Illuminate\Http\RedirectResponse', $response);

PHPUnit::assertEquals($this->app['url']->to($uri), $response->headers->get('Location'));

$this->assertSessionHasAll($with);
}

/**
* Assert whether the client was redirected to a given route.
*
* @param string $name
* @param array $parameters
* @param array $with
* @return void
*/
public function assertRedirectedToRoute($name, $parameters = array(), $with = array())
{
$this->assertRedirectedTo($this->app['url']->route($name, $parameters), $with);
}

/**
* Assert whether the client was redirected to a given action.
*
* @param string $name
* @param array $parameters
* @param array $with
* @return void
*/
public function assertRedirectedToAction($name, $parameters = array(), $with = array())
{
$this->assertRedirectedTo($this->app['url']->action($name, $parameters), $with);
}

/**
* Assert that the session has a given list of values.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function assertSessionHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertSessionHasAll($key);
}

if (is_null($value)) {
PHPUnit::assertTrue($this->app['session.store']->has($key), "Session missing key: $key");
} else {
PHPUnit::assertEquals($value, $this->app['session.store']->get($key));
}
}

/**
* Assert that the session has a given list of values.
*
* @param array $bindings
* @return void
*/
public function assertSessionHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertSessionHas($value);
} else {
$this->assertSessionHas($key, $value);
}
}
}

/**
* Assert that the session has errors bound.
*
* @param string|array $bindings
* @param mixed $format
* @return void
*/
public function assertSessionHasErrors($bindings = array(), $format = null)
{
$this->assertSessionHas('errors');

$bindings = (array) $bindings;

$errors = $this->app['session.store']->get('errors');

foreach ($bindings as $key => $value) {
if (is_int($key)) {
PHPUnit::assertTrue($errors->has($value), "Session missing error: $value");
} else {
PHPUnit::assertContains($value, $errors->get($key, $format));
}
}
}

/**
* Assert that the session has old input.
*
* @return void
*/
public function assertHasOldInput()
{
$this->assertSessionHas('_old_input');
}
}
26 changes: 26 additions & 0 deletions tests/BehatFeatureContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php namespace Orchestra\Testbench\TestCase;

class BehatFeatureContextTest extends \PHPUnit_Framework_TestCase
{
/**
* Test Orchestra\Testbench\BehatFeatureContext::createApplication() method.
*
* @test
*/
public function testCreateApplicationMethod()
{
$stub = new StubFeatureContext([]);
$app = $stub->createApplication();

$this->assertInstanceOf('\Orchestra\Testbench\TestCaseInterface', $stub);
$this->assertInstanceOf('\Illuminate\Foundation\Application', $app);
$this->assertEquals('UTC', date_default_timezone_get());
$this->assertEquals('testing', $app['env']);
$this->assertInstanceOf('\Illuminate\Config\Repository', $app['config']);
}
}

class StubFeatureContext extends \Orchestra\Testbench\BehatFeatureContext
{

}

0 comments on commit d038aa2

Please sign in to comment.