Skip to content

Commit

Permalink
Create Pipeline (#7)
Browse files Browse the repository at this point in the history
Setup pipeline with tests and cs-fixer
  • Loading branch information
davidhoelzel authored Mar 18, 2024
1 parent fc9253b commit 7125d6e
Show file tree
Hide file tree
Showing 24 changed files with 177 additions and 121 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests

on:
pull_request:
push:
branches:
- main

jobs:
tests:
name: "PHP ${{ matrix.php-version }}"
runs-on: ubuntu-latest
continue-on-error: false
strategy:
matrix:
php-version: ['8.2', '8.3']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2
- name: Checkout code
uses: actions/checkout@v3
- name: Install Dependencies
run: composer update --no-interaction --prefer-dist --optimize-autoloader --prefer-stable
- name: PHP-CS-Fixer
run: vendor/bin/php-cs-fixer fix --diff --dry-run
- name: Run tests
run: vendor/bin/phpunit


1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/vendor/
.phpunit.cache
composer.lock
.php-cs-fixer.cache
16 changes: 16 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('var');

$config = new PhpCsFixer\Config();
return $config
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'yoda_style' => false,
])
->setFinder($finder);
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require-dev": {
"phpunit/phpunit": "^10.5",
"symfony/browser-kit": "^6.4|^7.0",
"symfony/css-selector": "^6.4|^7.0"
"symfony/css-selector": "^6.4|^7.0",
"friendsofphp/php-cs-fixer": "^3.51"
}
}
5 changes: 3 additions & 2 deletions config/documentation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;
Expand All @@ -10,7 +11,7 @@
use Qossmic\TwigDocBundle\Twig\TwigDocExtension;

return static function (ContainerConfigurator $container) {
$container->services() ->set('twig_doc.controller.documentation', TwigDocController::class)
$container->services()->set('twig_doc.controller.documentation', TwigDocController::class)
->public()
->autoconfigure()
->autowire()
Expand All @@ -35,5 +36,5 @@
->autowire()
->tag('twig.extension')
->alias(TwigDocExtension::class, 'twig_doc.twig.extension')
;
;
};
9 changes: 6 additions & 3 deletions src/Component/ComponentCategory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Qossmic\TwigDocBundle\Component;
Expand All @@ -15,14 +16,15 @@ class ComponentCategory
#[Assert\Regex('/^\w+$/')]
private string $name;

public function getParent(): ?ComponentCategory
public function getParent(): ?self
{
return $this->parent;
}

public function setParent(?ComponentCategory $parent): ComponentCategory
public function setParent(?self $parent): self
{
$this->parent = $parent;

return $this;
}

Expand All @@ -31,9 +33,10 @@ public function getName(): string
return $this->name;
}

public function setName(string $name): ComponentCategory
public function setName(string $name): self
{
$this->name = $name;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Component/ComponentInvalid.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Qossmic\TwigDocBundle\Component;
Expand Down
30 changes: 19 additions & 11 deletions src/Component/ComponentItem.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Qossmic\TwigDocBundle\Component;
Expand Down Expand Up @@ -30,9 +31,10 @@ public function getName(): string
return $this->name;
}

public function setName(string $name): ComponentItem
public function setName(string $name): self
{
$this->name = $name;

return $this;
}

Expand All @@ -41,9 +43,10 @@ public function getTitle(): string
return $this->title;
}

public function setTitle(string $title): ComponentItem
public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

Expand All @@ -52,9 +55,10 @@ public function getDescription(): string
return $this->description;
}

public function setDescription(string $description): ComponentItem
public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}

Expand All @@ -63,9 +67,10 @@ public function getTags(): array
return $this->tags;
}

public function setTags(array $tags): ComponentItem
public function setTags(array $tags): self
{
$this->tags = $tags;

return $this;
}

Expand All @@ -74,27 +79,28 @@ public function getParameters(): array
return $this->parameters;
}

public function setParameters(array $parameters): ComponentItem
public function setParameters(array $parameters): self
{
$this->parameters = $parameters;

return $this;
}

public function setVariations(array $variations): ComponentItem
public function setVariations(array $variations): self
{
$this->variations = $variations;

return $this;
}

public function addParameter(string $name, mixed $value): ComponentItem
public function addParameter(string $name, mixed $value): self
{
$this->parameters[$name] = $value;

return $this;
}

public function removeParameter(string $name): ComponentItem
public function removeParameter(string $name): self
{
unset($this->parameters[$name]);

Expand All @@ -106,13 +112,14 @@ public function getVariations(): array
return $this->variations;
}

public function addVariation(string $name, array $variation): ComponentItem
public function addVariation(string $name, array $variation): self
{
$this->variations[$name] = $variation;

return $this;
}

public function removeVariation(string $name): ComponentItem
public function removeVariation(string $name): self
{
unset($this->variations[$name]);

Expand All @@ -124,9 +131,10 @@ public function getCategory(): ComponentCategory
return $this->category;
}

public function setCategory(ComponentCategory $category): ComponentItem
public function setCategory(ComponentCategory $category): self
{
$this->category = $category;

return $this;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Component/ComponentItemFactory.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace Qossmic\TwigDocBundle\Component;

use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Qossmic\TwigDocBundle\Exception\InvalidComponentConfigurationException;
use Qossmic\TwigDocBundle\Service\CategoryService;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ComponentItemFactory
{
Expand All @@ -23,7 +24,7 @@ public function create(array $data): ComponentItem
if ($category === null) {
$violations = ConstraintViolationList::createFromMessage(
sprintf(
"invalid %s \"%s\". Valid categories are: %s. Valid sub-categories are: %s",
'invalid %s "%s". Valid categories are: %s. Valid sub-categories are: %s',
isset($data['sub_category']) ? 'sub_category' : 'category',
$data['sub_category'] ?? $data['category'],
implode(', ', array_keys($this->categoryService->getCategories())),
Expand Down Expand Up @@ -53,7 +54,7 @@ private function createItem(array $data): ComponentItem
->setTags($data['tags'] ?? [])
->setParameters($data['parameters'] ?? [])
->setVariations($data['variations'] ?? [
'default' => $this->createVariationParameters($data['parameters'] ?? [])
'default' => $this->createVariationParameters($data['parameters'] ?? []),
]);

return $item;
Expand All @@ -63,7 +64,7 @@ public function createVariationParameters(array $parameters): array
{
$params = [];
foreach ($parameters as $name => $type) {
if (is_array($type)) {
if (\is_array($type)) {
$paramValue = $this->createVariationParameters($type);
} else {
$paramValue = $this->createParamValue($type);
Expand All @@ -86,7 +87,7 @@ private function createParamValue(string $type): bool|int|float|string|null
return random_int(0, 100000);
case 'bool':
case 'boolean':
return [true, false][rand(0,1)];
return [true, false][rand(0, 1)];
case 'float':
case 'double':
return (float) rand(1, 1000) / 100;
Expand Down
14 changes: 7 additions & 7 deletions src/Controller/TwigDocController.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
<?php

declare(strict_types=1);

namespace Qossmic\TwigDocBundle\Controller;

use Qossmic\TwigDocBundle\Service\ComponentService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Twig\Environment;
use Qossmic\TwigDocBundle\Component\ComponentItem;
use Qossmic\TwigDocBundle\Service\ComponentService;

class TwigDocController
{
public function __construct(
private readonly Environment $twig,
private readonly ComponentService $componentService,
private readonly ?Profiler $profiler = null
)
{
) {
}

public function index(Request $request): Response
Expand Down Expand Up @@ -55,14 +54,15 @@ public function componentView(Request $request): Response
}
$breakpoint = $request->query->get('breakpoint');
// disable profiler to get rid of toolbar in dev
if($this->profiler) {
$this->profiler->disable();
if ($this->profiler) {
$this->profiler->disable();
}

return new Response(
$this->twig->render('@TwigDoc/component.html.twig', [
'component' => $component,
'componentData' => $request->query->all('data'),
'quantity' => $request->query->get('quantity')
'quantity' => $request->query->get('quantity'),
])
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

class Configuration implements ConfigurationInterface
{

/**
* @inheritDoc
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('twig_doc');
Expand Down Expand Up @@ -40,13 +36,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->variableNode('parameters')
->validate()
->ifTrue(fn ($v) => is_string($v) === false && is_array($v) === false)
->ifTrue(fn ($v) => \is_string($v) === false && \is_array($v) === false)
->thenInvalid('parameters must be either a scalar or an array')
->end()
->end()
->variableNode('variations')
->validate()
->ifTrue(fn ($v) => is_string($v) === false && is_array($v) === false)
->ifTrue(fn ($v) => \is_string($v) === false && \is_array($v) === false)
->thenInvalid('variations must be either a scalar or an array')
->end()
->end()
Expand Down
8 changes: 2 additions & 6 deletions src/DependencyInjection/TwigDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@

class TwigDocExtension extends Extension
{

/**
* @inheritDoc
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('documentation.php');

$definition = $container->getDefinition('twig_doc.service.component');
Expand Down
Loading

0 comments on commit 7125d6e

Please sign in to comment.