Skip to content

Instantly share code, notes, and snippets.

@gabfr

gabfr/README.md Secret

Created February 19, 2020 20:50
Show Gist options
  • Save gabfr/65cbfedefe7dbfcbc9606677acb437d9 to your computer and use it in GitHub Desktop.
Save gabfr/65cbfedefe7dbfcbc9606677acb437d9 to your computer and use it in GitHub Desktop.
Process of removing behat/Symfony2Extension (in code examples)

Process of removing Behat/Symfony2Extension

Here below I put the files I had to change to remove the Symfony2Extension, pay attention to the code comments because there's some important instructions there.

Also, gist doesn't let me specify complete paths on the gist file names, so I separated the path with - instead of /. Sorry if it's confusing I made this in a hurry!

default:
autoload:
'%paths.base%/tests/integration/features/context/'
formatters:
progress: true
suites:
api:
paths:
- '%paths.base%/tests/integration/features/api'
contexts:
# Make sure to proper configure your context, removing the old kernel argument specified
- AppApiContext
# kernel: '@kernel'
extensions:
Behat\WebApiExtension:
base_uri: 'local.api.app.com:8003/'
# Also, make sure to remove the config of the extension
# Behat\Symfony2Extension:
# kernel:
# env: 'test'
# bootstrap: tests/integration/features/bootstrap.php
# class: Your\App\Kernel
<?php
declare(strict_types=1);
$_ENV['APP_ENV'] = $_SERVER['APP_ENV'] = 'test';
require dirname(__DIR__) . '/../../config/bootstrap.php'; // here we're including the app config bootstrap
<?php
declare(strict_types=1);
namespace Your\App\integration\features\context;
use Behat\WebApiExtension\Context\WebApiContext;
// use Symfony\Component\HttpKernel\KernelInterface;
use Your\App\Kernel;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
/*
It could be your normal Behat Context, not necessarily the WebApiContext
this is because I'm using it for testing an API.
So I use that WebApiExtension to make it easier to do.
*/
class HelperApiContext extends WebApiContext
{
/**
* Before removing Symfony2Extension, it used to be a KernelInterface
* Now we're using directly as Kernel
* (a class that inherits Symfony\Component\HttpKernel\Kernel)
* @var Kernel
*/
protected $kernel;
// The older constructor:
// public function __construct(KernelInterface $kernel)
// {
// $this->kernel = $kernel;
// }
public function __construct()
{
require_once dirname(__DIR__) . '/bootstrap.php'; // here we're including a integration tests bootstrap file, its contents are included below
$this->kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$this->kernel->boot();
}
// Example of method to make a request to the Kernel directly
protected function sendARequestWithBody(string $method, string $uri, PyStringNode $body = null) : void
{
$url = $this->prepareUrl($uri);
if ($body) {
$body = $this->replacePlaceHolder($body->getRaw());
}
$request = SymfonyRequest::create($url, $method, [], [], [], [], $body);
$request->headers->add($this->getHeaders());
try {
$response = $this->kernel->handle($request)->send();
// Here you don't need specifically to encapsulate it as a guzzle response, did that to suit our needs
$this->response = new GuzzleResponse($response->getStatusCode(), $response->headers->all(), $response->getContent());
} catch (\Exception $e) {
$this->response = new GuzzleResponse(SymfonyResponse::HTTP_INTERNAL_SERVER_ERROR, [], $e->getMessage());
} finally {
$this->removeHeaders();
$request->initialize();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment