Skip to content

Commit

Permalink
Merge pull request biig-io#18 from swagindustries/feature/test-app
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Aug 18, 2020
2 parents 9e45e5f + fe39347 commit 90e241d
Show file tree
Hide file tree
Showing 25 changed files with 545 additions and 39 deletions.
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,ts}]
indent_size = 2

# Tab indentation (no size specified)
[Makefile]
indent_style = tab

[*.md]
max_line_length = off
trim_trailing_whitespace = false

[*.{yaml,yml,feature}]
indent_size = 2
84 changes: 49 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
name: Test suite

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 0 * * *'
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 0 * * *'

jobs:
tests:
runs-on: ubuntu-latest
unit-test:
runs-on: ubuntu-latest

strategy:
matrix:
php: [7.4]
strategy:
matrix:
php: [7.4]

steps:
- name: Setup PHP
run: |
sudo update-alternatives --set php /usr/bin/php${{ matrix.php }}
echo "date.timezone=UTC" >> /tmp/timezone.ini
sudo mv /tmp/timezone.ini /etc/php/${{ matrix.php }}/cli/conf.d/timezone.ini
echo ${{ matrix.php }} > .php-version
steps:
- uses: actions/checkout@v2

- uses: actions/checkout@v2
- uses: actions/cache@v2
id: cache-composer
with:
path: ~/.composer/cache
key: composer-php-${{ matrix.php }}-${{ github.sha }}
restore-keys: composer-php-${{ matrix.php }}-

- uses: actions/cache@v1
id: cache-composer
with:
path: /home/runner/.composer/cache
key: composer-php:${{ matrix.php }}-${{ github.sha }}
restore-keys: composer-php:${{ matrix.php }}-
- name: Valid composer.json
run: make test.composer

- run: mkdir -p /home/runner/.composer/cache
if: steps.cache-composer.outputs.cache-hit != 'true'
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Valid composer.json
run: make test.composer
- name: Run PHP CS Fixer
run: make test.phpcs

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run PHPUnit tests
run: make test.phpunit

- name: Run PHP CS Fixer
run: make test.phpcs
functional-test:
runs-on: ubuntu-latest

- name: Run PHPUnit tests
run: make test.phpunit
strategy:
matrix:
php: [7.4]

steps:
- uses: actions/checkout@v2

- uses: actions/cache@v2
id: cache-composer
with:
path: ~/.composer/cache
key: composer-php-${{ matrix.php }}-${{ github.sha }}
restore-keys: composer-php-${{ matrix.php }}-


- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run Behat tests
run: make test.behat
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.php_cs.cache
tests/TestApplication/var
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['Tests', 'vendor'])
->exclude(['tests/TestApplication/var', 'vendor'])
;

return PhpCsFixer\Config::create()
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Support of more kind of FormError
- Removed requirement to have CSRF protection enabled #25
- Removed the requirement to have CSRF protection enabled #25
- Behat test suite. Melodiia is officially battle tested!

### Changed
- Huge BC Break on namespaces. You need to rename all classes used to SwagIndustries instead of Biig
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ test.phpcs.fix: ## Run PHP CS Fixer and fix issues if possible
test.phpunit: ## Run PHPUnit tests
php vendor/bin/phpunit

test.behat: ## Run Behat tests
php vendor/bin/behat

hooks.install: ## Install git hooks
@cp -vp resources/git/pre-commit.sh .git/hooks/pre-commit

Expand Down
11 changes: 11 additions & 0 deletions behat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default:
suites:
default:
contexts:
- SwagIndustries\Melodiia\Tests\Behat\Context\BasicsContext
- SwagIndustries\Melodiia\Tests\Behat\Context\TodoContext
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: tests/TestApplication/config/bootstrap.php
kernel:
class: TestApplication\Kernel
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "symfony-bundle",
"scripts": {
"phpcs": "php-cs-fixer fix --using-cache=false",
"test-app": "@php -S localhost:8000 -t tests/TestApplication/public",
"post-install-cmd": "make hooks.install"
},
"require": {
Expand All @@ -22,7 +23,11 @@
"symfony/symfony": "^4.3 || ^5.0",
"doctrine/orm": "^2.6",
"justinrainbow/json-schema": "^5.2",
"doctrine/annotations": "^1.8"
"doctrine/annotations": "^1.8",
"doctrine/doctrine-bundle": "^2.1",
"behat/behat": "^3.7",
"friends-of-behat/symfony-extension": "^2.1",
"fzaninotto/faker": "^1.9"
},
"autoload": {
"psr-4": {
Expand All @@ -31,7 +36,9 @@
},
"autoload-dev": {
"psr-4": {
"SwagIndustries\\Melodiia\\Test\\": "tests/Melodiia/"
"SwagIndustries\\Melodiia\\Test\\": "tests/Melodiia/",
"TestApplication\\": "tests/TestApplication/src/",
"SwagIndustries\\Melodiia\\Tests\\Behat\\": "tests/Behat/"
}
},
"license": "MIT",
Expand Down
61 changes: 61 additions & 0 deletions features/crud.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Feature:
In order to create, read, update and delete data using the API
As a client software
I should be able use standard REST CRUD actions with todos

Scenario: list some todo
Given there are some todos
When I make a GET request on "/todos"
Then I should retrieve:
"""
{
"meta": {
"totalPages": 1,
"totalResults": 3,
"currentPage": 1,
"maxPerPage": 30
},
"links": {
"prev": null,
"next": null,
"last": "http://localhost/todos",
"first": "http://localhost/todos"
},
"data": [
{
"id": 1,
"content": "foo"
},
{
"id": 2,
"content": "bar"
},
{
"id": 3,
"content": "baz"
}
]
}
"""

Scenario: create a todo and get it
Given I make a "POST" request on "/todos" with the content:
"""
{
"content": "hello"
}
"""
And the last response contains:
"""
{
"id": "1"
}
"""
When I make a GET request on "/todos/1"
Then I should retrieve:
"""
{
"id": 1,
"content": "hello"
}
"""
6 changes: 6 additions & 0 deletions src/Bridge/Symfony/Form/DomainObjectsDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function createObject(iterable $form, string $dataClass = null)
}

$ref = new \ReflectionClass($dataClass);

$constructor = $ref->getConstructor();
if (null === $constructor) {
return new $dataClass();
}

$constructorParameters = $ref->getConstructor()->getParameters();

// Case of anemic object, we have nothing to do here.
Expand Down
26 changes: 26 additions & 0 deletions tests/Behat/Context/AbstractContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace SwagIndustries\Melodiia\Tests\Behat\Context;

use Behat\Behat\Context\Context;
use Faker\Factory;
use Faker\Generator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractContext implements Context
{
private KernelInterface $kernel;
protected Generator $faker;

public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
$this->faker = Factory::create();
}

protected function getContainer(): ContainerInterface
{
return $this->kernel->getContainer();
}
}
69 changes: 69 additions & 0 deletions tests/Behat/Context/BasicsContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace SwagIndustries\Melodiia\Tests\Behat\Context;

use Behat\Gherkin\Node\PyStringNode;
use Doctrine\ORM\Tools\SchemaTool;

class BasicsContext extends AbstractContext
{
private $response;

/**
* @When I make a GET request on :uri
* @Given I make a :verb request on :uri with the content:
*/
public function iMakeARequestOn($uri, $verb = 'GET', PyStringNode $content = null)
{
$client = $this->getContainer()->get('test.client');

if ('GET' === $verb) {
$client->request($verb, $uri);
$this->response = $client->getResponse();

return;
}

if (empty($content)) {
throw new \Exception(sprintf('Cannot process request "%s" with no content.', $verb));
}

// Just validate json
json_decode($content->getRaw(), true, 512, JSON_THROW_ON_ERROR);

$client->request($verb, $uri, [], [], [], $content);
$this->response = $client->getResponse();
}

/**
* @Then I should retrieve:
* @Given the last response contains:
*/
public function iShouldRetrieve(PyStringNode $string)
{
$json = $this->response->getContent();
$expected = \json_encode(\json_decode($string->getRaw(), true, 512, JSON_THROW_ON_ERROR));

if ($json !== $expected) {
if (json_decode($json)) {
echo "Expected: $expected\n";
echo "Actual: $json\n";
}

throw new \Exception('Expected request result does not match actual result.');
}
}

/**
* @BeforeScenario
*/
public function resetDb()
{
@unlink(dirname(__DIR__) . '/../TestApplication/var/data.db');

$entityManager = $this->getContainer()->get('doctrine')->getManager();
$metadatas = $entityManager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($entityManager);
$schemaTool->createSchema($metadatas);
}
}
Loading

0 comments on commit 90e241d

Please sign in to comment.