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

Tests: Add first simple test #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Symfony

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

permissions:
contents: read

jobs:
symfony-tests:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
- uses: actions/checkout@v3
- name: Copy .env.test.local
run: php -r "file_exists('.env.test.local') || copy('.env.test', '.env.test.local');"
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
composer.lock
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
],
"require-dev": {
"phpstan/phpstan": "^1.8",
"friendsofphp/php-cs-fixer": "^3.9"
"friendsofphp/php-cs-fixer": "^3.9",
"phpunit/phpunit": "^10",
"nyholm/symfony-bundle-test": "^2.0",
"symfony/expression-language": "^6.3",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/orm": "^2.16",
"symfony/security-bundle": "^6.3",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.24",
"symfony/property-access": "^6.3",
"symfony/property-info": "^6.3",
"symfony/serializer": "^6.3"
}
}
97 changes: 97 additions & 0 deletions tests/BundleInitializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Nyholm\BundleTest\TestKernel;
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\KernelInterface;
use Theodo\AccentBundle\TheodoAccentBundle;
use Theodo\AccentBundle\AccessControl\AccentReportFactory;
use Theodo\AccentBundle\AccessControl\RouteAccessControlData;

class BundleInitializationTest extends KernelTestCase
{
protected static function getKernelClass(): string
{
return TestKernel::class;
}

protected static function createKernel(array $options = []): KernelInterface
{
/**
* @var TestKernel $kernel
*/
$kernel = parent::createKernel($options);
$kernel->addTestBundle(DoctrineBundle::class);
$kernel->addTestBundle(ApiPlatformBundle::class);
$kernel->addTestBundle(TheodoAccentBundle::class);
$kernel->addTestConfig(__DIR__.'/config/packages/doctrine.yaml');
$kernel->addTestConfig(__DIR__.'/config/packages/api_platform.yaml');
$kernel->addTestRoutingFile(__DIR__.'/config/routes.yaml');

// Unused, non public services are removed during kernel boot.
// We force them as public during test.
$kernel->addTestCompilerPass(new class () implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
$services = $container->getDefinitions() + $container->getAliases();

foreach ($services as $id => $definition) {
if (stripos($id, 'theodo_accent') === 0) {
$definition->setPublic(true);
}
}
}
});

$kernel->handleOptions($options);

return $kernel;
}

public function testInitBundle(): void
{
$container = self::getContainer();

$this->assertTrue($container->has('theodo_accent.access_control_checker_command'));
$service = $container->get('theodo_accent.access_control_checker_command');
$this->assertInstanceOf(Command::class, $service);
}

public function testExposedRoutes(): void
{
$container = self::getContainer();

$this->assertTrue($container->has('theodo_accent.accent_report_factory'));

/** @var AccentReportFactory $reportFactory */
$reportFactory = $container->get('theodo_accent.accent_report_factory');
$accentReport = $reportFactory->createAccentReport();

$this->assertEquals(2, $accentReport->getUnprotectedRoutesCount());

$resourceRelatedRoutes = array_filter(
$accentReport->getRouteAccessControlList(),
fn ($route) => RouteAccessControlData::RESOURCE_UNRELATED_ROUTE !== $route->getExpression()
);

$this->assertCount(3, $resourceRelatedRoutes);
$checkedRoutes = [];
foreach($resourceRelatedRoutes as $routeData) {
$checkedRoutes[$routeData->getRouteName()] = $routeData->getExpression();
}

$expectedRoutes = [
'_api_/books/{id}{._format}_get' => "is_granted('ROLE_USER_GET')",
'_api_/books{._format}_post' => "is_granted('ROLE_USER_DEFAULT')",
'_api_/books/{id}{._format}_patch' => "is_granted('ROLE_USER_PATCH')",
];

$this->assertEquals($expectedRoutes, $checkedRoutes);
}
}
21 changes: 21 additions & 0 deletions tests/Entity/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;

#[ApiResource(security:"is_granted('ROLE_USER_DEFAULT')")]
#[Get(security:"is_granted('ROLE_USER_GET')")]
#[Post]
#[Patch(security:"is_granted('ROLE_USER_PATCH')")]
class Book
{
public ?int $id = null;

public ?string $author = null;

public ?string $title = null;
}
3 changes: 3 additions & 0 deletions tests/config/packages/api_platform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
api_platform:
resource_class_directories:
- "%kernel.project_dir%/tests/Entity"
3 changes: 3 additions & 0 deletions tests/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
doctrine:
dbal:
url: "pdo-sqlite:///:memory:"
4 changes: 4 additions & 0 deletions tests/config/routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
api_platform:
resource: .
type: api_platform
prefix: /api