Skip to content

Commit

Permalink
Merge pull request #2 from tienvx/testing
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
tienvx committed Oct 2, 2021
2 parents a72b2a2 + 4729dc3 commit dd4fbd6
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 52 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: main

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0']
name: PHP ${{ matrix.php-versions }}
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: pcov
tools: phpstan, php-cs-fixer:3, phpunit, composer:v2
#extensions: mbstring, intl

- name: Checkout
uses: actions/checkout@v2

- name: Install
run: composer install --prefer-source

- name: Run PHP CS Fixer
run: php-cs-fixer fix --diff --dry-run

- name: Run PHPStan
run: phpstan analyse src tests

- name: Test & Generate Code Coverage
run: phpunit

- name: Upload coverage results to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=clover.xml -v
if: matrix.php-versions == '7.4'
2 changes: 2 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('Resources/assets/dist')
->exclude('Resources/assets/node_modules')
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
;
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# UX Collection JS
# UX Collection JS [![Build Status][actions_badge]][actions_link] [![Coverage Status][coveralls_badge]][coveralls_link]

UX collection JS is a Symfony bundle providing Symfony UX integration for collection form type with the help from [Symfony Collection JS](https://github.com/ruano-a/symfonyCollectionJs) library.

Expand All @@ -23,7 +23,7 @@ Use the new CollectionType class defined by this bundle:

```php
// ...
use Tienvx\Bundle\UXCollection\Form\CollectionJsType;
use Tienvx\UX\CollectionJs\Form\CollectionJsType;

class TaskType extends AbstractType
{
Expand All @@ -37,6 +37,7 @@ class TaskType extends AbstractType
'allow_remove' => true,
'allow_move_up' => true,
'allow_move_down' => true,
'render_expanded' => false,
'prototype' => true,
])
// ...
Expand All @@ -54,3 +55,9 @@ Please make sure to update tests as appropriate.

## License
[MIT](LICENSE)

[actions_badge]: https://github.com/tienvx/ux-collection-js/workflows/main/badge.svg
[actions_link]: https://github.com/tienvx/ux-collection-js/actions

[coveralls_badge]: https://coveralls.io/repos/tienvx/ux-collection-js/badge.svg?branch=master&service=github
[coveralls_link]: https://coveralls.io/github/tienvx/ux-collection-js?branch=master
23 changes: 13 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,37 @@
],
"autoload": {
"psr-4": {
"Tienvx\\Bundle\\UXCollectionJs\\": "src/"
"Tienvx\\UX\\CollectionJs\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tienvx\\Bundle\\UXCollectionJs\\Tests\\": "tests/"
"Tienvx\\UX\\CollectionJs\\Tests\\": "tests/"
}
},
"require": {
"php": "^7.4|^8.0",
"symfony/config": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/form": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0"
"symfony/config": "^4.4.17|^5.0",
"symfony/dependency-injection": "^4.4.17|^5.0",
"symfony/form": "^4.4.17|^5.0",
"symfony/http-kernel": "^4.4.17|^5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"symfony/framework-bundle": "^4.4.17|^5.0",
"symfony/twig-bundle": "^4.4.17|^5.0",
"symfony/var-dumper": "^4.4.17|^5.0"
},
"conflict": {
"symfony/symfony": "*"
"symfony/flex": "<1.13"
},
"extra": {
"branch-alias": {
"dev-main": "1.0-dev"
},
"thanks": {
"name": "symfony/ux",
"url": "https://github.com/symfony/ux"
"name": "ruano-a/symfonyCollectionJs",
"url": "https://github.com/ruano-a/symfonyCollectionJs"
}
}
}
22 changes: 14 additions & 8 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
failOnWarning="true">
<php>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
<env name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
Expand Down
9 changes: 9 additions & 0 deletions src/CollectionJsBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tienvx\UX\CollectionJs;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CollectionJsBundle extends Bundle
{
}
31 changes: 0 additions & 31 deletions src/DependencyInjection/FormCollectionExtension.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Form/CollectionJsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Tienvx\Bundle\UXCollectionJs\Form;
namespace Tienvx\UX\CollectionJs\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Tienvx\UX\CollectionJs\Form\CollectionJsType;

return static function (ContainerConfigurator $container): void {
$container->services()
->set(CollectionJsType::class)
->tag('form.type')
;
};
26 changes: 26 additions & 0 deletions tests/CollectionJsBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tienvx\UX\CollectionJs\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Kernel;
use Tienvx\UX\CollectionJs\Tests\Kernel\EmptyAppKernel;
use Tienvx\UX\CollectionJs\Tests\Kernel\FrameworkAppKernel;

class CollectionJsBundleTest extends TestCase
{
public function provideKernels()
{
yield 'empty' => [new EmptyAppKernel('test', true)];
yield 'framework' => [new FrameworkAppKernel('test', true)];
}

/**
* @dataProvider provideKernels
*/
public function testBootKernel(Kernel $kernel)
{
$kernel->boot();
$this->assertArrayHasKey('CollectionJsBundle', $kernel->getBundles());
}
}
44 changes: 44 additions & 0 deletions tests/Form/CollectionJsTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tienvx\UX\CollectionJs\Tests\Form;

use Symfony\Component\Form\Tests\Extension\Core\Type\CollectionTypeTest;
use Symfony\Component\Form\Tests\Extension\Core\Type\FileTypeTest;
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
use Tienvx\UX\CollectionJs\Form\CollectionJsType;

class CollectionJsTypeTest extends CollectionTypeTest
{
public const TESTED_TYPE = CollectionJsType::class;

public function testDefaultOptions()
{
$form = $this->factory
->create(static::TESTED_TYPE, null, [
'entry_type' => FileTypeTest::TESTED_TYPE,
])
;

$view = $form->createView();
$this->assertFalse($view->vars['allow_move_up']);
$this->assertFalse($view->vars['allow_move_down']);
$this->assertFalse($view->vars['render_expanded']);
}

public function testCustomOptions()
{
$form = $this->factory
->create(static::TESTED_TYPE, null, [
'entry_type' => TextTypeTest::TESTED_TYPE,
'allow_move_up' => true,
'allow_move_down' => true,
'render_expanded' => true,
])
;

$view = $form->createView();
$this->assertTrue($view->vars['allow_move_up']);
$this->assertTrue($view->vars['allow_move_down']);
$this->assertTrue($view->vars['render_expanded']);
}
}
27 changes: 27 additions & 0 deletions tests/Kernel/AppKernelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tienvx\UX\CollectionJs\Tests\Kernel;

trait AppKernelTrait
{
public function getCacheDir()
{
return $this->createTmpDir('cache');
}

public function getLogDir()
{
return $this->createTmpDir('logs');
}

private function createTmpDir(string $type): string
{
$dir = sys_get_temp_dir() . '/collection_js_bundle/' . uniqid($type . '_', true);

if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}

return $dir;
}
}
21 changes: 21 additions & 0 deletions tests/Kernel/EmptyAppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tienvx\UX\CollectionJs\Tests\Kernel;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
use Tienvx\UX\CollectionJs\CollectionJsBundle;

class EmptyAppKernel extends Kernel
{
use AppKernelTrait;

public function registerBundles()
{
return [new CollectionJsBundle()];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
}
}
26 changes: 26 additions & 0 deletions tests/Kernel/FrameworkAppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tienvx\UX\CollectionJs\Tests\Kernel;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Tienvx\UX\CollectionJs\CollectionJsBundle;

class FrameworkAppKernel extends Kernel
{
use AppKernelTrait;

public function registerBundles()
{
return [new FrameworkBundle(), new CollectionJsBundle()];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
});
}
}

0 comments on commit dd4fbd6

Please sign in to comment.