Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phpunit 6 compatibility #70

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

matrix:
Expand Down
4 changes: 2 additions & 2 deletions bin/start_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ http {
root $TRAVIS_BUILD_DIR/testapp;

location / {
fastcgi_pass 127.0.0.1:9000;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root/index.php;
include /etc/nginx/fastcgi_params;
Expand All @@ -43,7 +43,7 @@ http {
}
CONF
echo " Starting the HHVM daemon"
hhvm --mode server -vServer.Type=fastcgi -vServer.IP='127.0.0.1' -vServer.Port=9000 > "$TRAVIS_BUILD_DIR/server.log" 2>&1 &
hhvm --mode server -vServer.Type=fastcgi -vServer.IP='127.0.0.1' -vServer.Port=9001 > "$TRAVIS_BUILD_DIR/server.log" 2>&1 &
echo " Starting nginx"
sudo mkdir -p /var/log/nginx/
sudo nginx -c "$TRAVIS_BUILD_DIR/.nginx.conf"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
],

"require": {
"php": ">=5.4",
"php": ">=5.6",
"behat/behat": "~3.0",
"guzzlehttp/guzzle": "4 - 6",
"phpunit/phpunit": "4 - 5"
"phpunit/phpunit": "4 - 6"
},

"require-dev": {
Expand Down
21 changes: 17 additions & 4 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use PHPUnit\Framework\Assert;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -44,7 +45,7 @@ public static function cleanTestFolders()
public function prepareScenario()
{
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat-web-api' . DIRECTORY_SEPARATOR .
md5(microtime() * rand(0, 10000));
md5(uniqid(rand(0, 10000)));

mkdir($dir . '/features/bootstrap', 0777, true);

Expand Down Expand Up @@ -119,7 +120,11 @@ public function itShouldPassWith($success, PyStringNode $text)
*/
public function theOutputShouldContain(PyStringNode $text)
{
PHPUnit_Framework_Assert::assertContains($this->getExpectedOutput($text), $this->getOutput());
if (class_exists(Assert::class)) {
Assert::assertContains($this->getExpectedOutput($text), $this->getOutput());
} else {
PHPUnit_Framework_Assert::assertContains($this->getExpectedOutput($text), $this->getOutput());
}
}

private function getExpectedOutput(PyStringNode $expectedText)
Expand Down Expand Up @@ -162,13 +167,21 @@ public function itShouldFail($success)
echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput();
}

PHPUnit_Framework_Assert::assertNotEquals(0, $this->getExitCode());
if (class_exists(Assert::class)) {
Assert::assertNotEquals(0, $this->getExitCode());
} else {
PHPUnit_Framework_Assert::assertNotEquals(0, $this->getExitCode());
}
} else {
if (0 !== $this->getExitCode()) {
echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput();
}

PHPUnit_Framework_Assert::assertEquals(0, $this->getExitCode());
if (class_exists(Assert::class)) {
Assert::assertEquals(0, $this->getExitCode());
} else {
PHPUnit_Framework_Assert::assertEquals(0, $this->getExitCode());
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion features/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Feature: client aware context
use Behat\WebApiExtension\Context\ApiClientAwareContext;
use GuzzleHttp\ClientInterface;
use PHPUnit\Framework\Assert;
class FeatureContext implements ApiClientAwareContext
{
Expand All @@ -24,7 +25,11 @@ Feature: client aware context
* @Then /^the client should be set$/
*/
public function theClientShouldBeSet() {
PHPUnit_Framework_Assert::assertInstanceOf('GuzzleHttp\Client', $this->client);
if (class_exists(Assert::class)) {
Assert::assertInstanceOf('GuzzleHttp\Client', $this->client);
} else {
PHPUnit_Framework_Assert::assertInstanceOf('GuzzleHttp\Client', $this->client);
}
}
}
"""
Expand Down
35 changes: 28 additions & 7 deletions src/Context/WebApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\Assert;
use PHPUnit_Framework_Assert as Assertions;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -219,7 +220,11 @@ public function theResponseCodeShouldBe($code)
{
$expected = intval($code);
$actual = intval($this->response->getStatusCode());
Assertions::assertSame($expected, $actual);
if (class_exists(Assert::class)) {
Assert::assertSame($expected, $actual);
} else {
Assertions::assertSame($expected, $actual);
}
}

/**
Expand All @@ -233,7 +238,11 @@ public function theResponseShouldContain($text)
{
$expectedRegexp = '/' . preg_quote($text) . '/i';
$actual = (string) $this->response->getBody();
Assertions::assertRegExp($expectedRegexp, $actual);
if (class_exists(Assert::class)) {
Assert::assertRegExp($expectedRegexp, $actual);
} else {
Assertions::assertRegExp($expectedRegexp, $actual);
}
}

/**
Expand All @@ -247,7 +256,11 @@ public function theResponseShouldNotContain($text)
{
$expectedRegexp = '/' . preg_quote($text) . '/';
$actual = (string) $this->response->getBody();
Assertions::assertNotRegExp($expectedRegexp, $actual);
if (class_exists(Assert::class)) {
Assert::assertNotRegExp($expectedRegexp, $actual);
} else {
Assertions::assertNotRegExp($expectedRegexp, $actual);
}
}

/**
Expand Down Expand Up @@ -278,10 +291,18 @@ public function theResponseShouldContainJson(PyStringNode $jsonString)
);
}

Assertions::assertGreaterThanOrEqual(count($etalon), count($actual));
foreach ($etalon as $key => $needle) {
Assertions::assertArrayHasKey($key, $actual);
Assertions::assertEquals($etalon[$key], $actual[$key]);
if (class_exists(Assert::class)) {
Assert::assertGreaterThanOrEqual(count($etalon), count($actual));
foreach ($etalon as $key => $needle) {
Assert::assertArrayHasKey($key, $actual);
Assert::assertEquals($etalon[$key], $actual[$key]);
}
} else {
Assertions::assertGreaterThanOrEqual(count($etalon), count($actual));
foreach ($etalon as $key => $needle) {
Assertions::assertArrayHasKey($key, $actual);
Assertions::assertEquals($etalon[$key], $actual[$key]);
}
}
}

Expand Down