Skip to content

Commit

Permalink
Make it compatible with PHPUnit 6
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed Oct 12, 2017
1 parent d0161fa commit 12eda35
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": ">=5.6",
"behat/behat": "~3.0",
"guzzlehttp/guzzle": "4 - 6",
"phpunit/phpunit": "4 - 5"
"phpunit/phpunit": "4 - 6"
},

"require-dev": {
Expand Down
19 changes: 16 additions & 3 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 @@ -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

0 comments on commit 12eda35

Please sign in to comment.