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

Prototyped behat end-to-end testing #50

Open
wants to merge 3 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 23 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@ cache:
- $HOME/.composer/cache/files

env:
matrix:
- TARGET="phpspec"
- TARGET="codestyle"
global:
- EZPLATFORM_REPO="https://github.com/ezsystems/ezplatform.git"
- SYMFONY_ENV=behat
- SYMFONY_DEBUG=1

matrix:
include:
- name: "Unit tests with PhpSpec"
env:
- TARGET="phpspec"
- name: "Code style"
env:
- TARGET="codestyle"
- name: "Behat"
env:
- TARGET="behat"
- COMPOSE_FILE="doc/docker/base-dev.yml"
- BEHAT_OPTS="--mode=behat --profile=graphql --suite=graphql"

install:
- if [ $TARGET == "behat" ]; then ./.travis/prepare_ezplatform.sh ${INSTALL_EZ_INSTALL_TYPE}; fi

before_script:
- COMPOSER_MEMORY_LIMIT=-1 composer install
- if [ "$TARGET" != "behat" ]; then COMPOSER_MEMORY_LIMIT=-1 composer install; fi

script:
- if [ "$TARGET" == "phpspec" ] ; then ./vendor/bin/phpspec run --format=pretty; fi
- if [ "$TARGET" == "codestyle" ] ; then ./vendor/bin/php-cs-fixer fix --dry-run -v --show-progress=estimating; fi
- if [ "$TARGET" == "behat" ]; then cd "$HOME/build/ezplatform"; docker-compose exec --user www-data app sh -c "bin/ezbehat ${BEHAT_OPTS}" ; fi

notification:
email: false
12 changes: 12 additions & 0 deletions .travis/prepare_ezplatform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

EZPLATFORM_BRANCH=`php -r 'echo json_decode(file_get_contents("./composer.json"))->extra->_ezplatform_branch_for_behat_tests;'`
EZPLATFORM_BRANCH="${EZPLATFORM_BRANCH:-master}"
PACKAGE_BUILD_DIR=$PWD
EZPLATFORM_BUILD_DIR=${HOME}/build/ezplatform

echo "> Cloning ezsystems/ezplatform:${EZPLATFORM_BRANCH}"
git clone --depth 1 --single-branch --branch "${EZPLATFORM_BRANCH}" ${EZPLATFORM_REPO} ${EZPLATFORM_BUILD_DIR}
cd ${EZPLATFORM_BUILD_DIR}

/bin/bash ./bin/.travis/trusty/setup_ezplatform.sh "${COMPOSE_FILE}" '' "${PACKAGE_BUILD_DIR}"
17 changes: 17 additions & 0 deletions behat_suites.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is meant to be imported from ezplatform's behat.yml.dist.
# All path are relative to the root ezplatform directory.
default:
autoload:
- '%paths.base%/vendor/ezsystems/ezplatform-graphql/features/bootstrap/'

graphql:
suites:
graphql:
autoload:
- '%paths.base%/vendor/ezsystems/ezplatform-graphql/features/bootstrap/'
paths:
- '%paths.base%/vendor/ezsystems/ezplatform-graphql/features/Generator.feature'
contexts:
- GeneratorContext
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we keep the Context classes in Context directories, also define namespaces for them - I think it would be useful to do that here as well, to keep it consistent.

- ConfigurationContext
- CacheContext
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"overblog/graphiql-bundle": "^0.1",
"phpspec/phpspec": "^5.1",
"friendsofphp/php-cs-fixer": "~2.15.1",
"mikey179/vfsstream": "^1.6"
"mikey179/vfsstream": "^1.6",
"behat/behat": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,8 +38,10 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
"dev-master": "1.0.x-dev",
"dev-tmp_ci_branch": "1.0.x-dev"
},
"_ezplatform_branch_for_behat_tests": "behat_graphql"
},
"scripts": {
"fix-cs": "@php ./vendor/bin/php-cs-fixer fix -v --show-progress=estimating"
Expand Down
11 changes: 11 additions & 0 deletions features/Generator.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Schema generation
In order to use GraphQL
As an application maintainer
I need to generate the schema

Scenario: An application maintainer generates the schema
Given the schema has not been generated
When I run the command "ezplatform:graphql:generate-schema"
When I clear the cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: usually we replace the second consecutive usage with And (And I clear the cache)

Then the schema files are generated in "app/config/graphql/ezplatform"
And the GraphQL extension is configured to use that schema
51 changes: 51 additions & 0 deletions features/bootstrap/CacheContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use PHPUnit\Framework\Assert;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

class CacheContext implements \Behat\Symfony2Extension\Context\KernelAwareContext
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no import? 😄

{
/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a matter of taste, but I prefer to use KernelDictionary (https://github.com/Behat/Symfony2Extension/blob/master/src/Behat/Symfony2Extension/Context/KernelDictionary.php), as it results in less boilerplate (no need to add a setter)


/**
* Sets Kernel instance.
*
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @Given /^I clear the cache$/
*/
public function iClearTheCache()
{
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->kernel);
$application->setAutoExit(false);

$input = new ArrayInput(['command' => 'cache:clear', '--env' => 'behat']);

// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
Assert::assertEquals(0, $application->run($input, $output));
$content = $output->fetch();
$this->kernel->shutdown();
$this->kernel->boot();
}
}
46 changes: 46 additions & 0 deletions features/bootstrap/ConfigurationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use PHPUnit\Framework\Assert;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

class ConfigurationContext implements \Behat\Symfony2Extension\Context\KernelAwareContext
{
/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;

/**
* Sets Kernel instance.
*
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @Given /^the GraphQL extension is configured to use that schema$/
*/
public function theGraphQLExtensionIsConfiguredToUseThatSchema()
{
$container = $this->kernel->getContainer();
$executor = $container->get('overblog_graphql.request_executor');
$schema = $executor->getSchema('default');
Assert::assertEquals('Domain', (string)$schema->getQueryType());
Assert::assertEquals('DomainContentMutation', (string)$schema->getMutationType());
}

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

use PHPUnit\Framework\Assert;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

class GeneratorContext implements \Behat\Symfony2Extension\Context\KernelAwareContext
{
/**
* @var string
*/
private $scriptOutput;

/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;

/**
* @When /^I run the command "([^"]+)"$/
*/
public function iRunTheCommand($command)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);

$input = new ArrayInput(['command' => $command, '--env' => 'behat']);

$output = new BufferedOutput();
$application->run($input, $output);

$content = $output->fetch();
}

/**
* @Then /^the schema files are generated in "([^"]*)"$/
*/
public function theSchemaFilesAreGeneratedIn($directory)
{
$finder = new Finder();
Assert::assertFileExists('app/config/graphql/ezplatform/Domain.types.yml');
Assert::assertFileExists('app/config/graphql/ezplatform/DomainContentMutation.types.yml');
}

/**
* @Given /^the schema has not been generated$/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually I struggle with steps like this one, because I understand them differently when they're used as Given or Then (and then I have to look at the code what they do).

The way I see it:
Given the schema has not been generated - check that the directory is empty, remove files if they exist
Then the schema has not been generated - assert that the directory is empty, fail the step if the files are there

Of course this is a minor thing, because I don't imagine people using Then the schema has not been generated, but I'm curious what's your take on this 😉

*/
public function theSchemaHasNotBeenGenerated()
{
if (file_exists('app/config/graphql/ezplatform')) {
$finder = new Finder();
$fs = new Filesystem();
$fs->remove($finder->in('app/config/graphql/ezplatform')->files());
}
}

/**
* Sets Kernel instance.
*
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
}