Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed May 15, 2024
1 parent a9b234f commit 6480c71
Show file tree
Hide file tree
Showing 31 changed files with 614 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
Expand All @@ -28,7 +27,7 @@ jobs:
run: composer validate --strict

- name: Install dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Normalize composer.json
run: composer normalize --dry-run --no-check-lock
Expand Down
30 changes: 25 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
Expand All @@ -26,6 +25,27 @@ jobs:
- php-version: "8.2"
dependencies: "highest" # Pimcore 11.*

env:
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: 3306
MYSQL_USER: root
MYSQL_PASSWORD: pimcore
MYSQL_DATABASE: pimcore
MYSQL_SERVER_VERSION: "10.11.5-MariaDB"

services:
mariadb:
image: mariadb:10.11.5
env:
MYSQL_ROOT_PASSWORD: pimcore
options: >-
--health-cmd="healthcheck.sh --connect --innodb_initialized"
--health-interval=10s
--health-timeout=5s
--health-retries=5
ports:
- 3306:3306

steps:
- name: Git Checkout
uses: actions/checkout@v4
Expand All @@ -36,13 +56,13 @@ jobs:
php-version: ${{ matrix.php-version }}

- name: Install dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}

# - name: Install Pimcore Admin UI
# run: composer require --dev pimcore/admin-ui-classic-bundle --no-interaction
# if: matrix.dependencies == 'highest'
- name: Install Pimcore Admin UI
run: composer require --dev pimcore/admin-ui-classic-bundle --no-interaction
if: matrix.dependencies == 'highest'

- name: Execute tests
run: composer tests
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"phpstan/phpstan": "^1.10.46",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-symfony": "^1.3.5",
"phpunit/phpunit": "^9.6.14"
"phpunit/phpunit": "^9.6.14",
"spatie/phpunit-snapshot-assertions": "^4.2",
"symfony/browser-kit": "^5.4 || ^6.4",
"teamneusta/pimcore-testing-framework": "^0.12.4"
},
"autoload": {
"psr-4": {
Expand All @@ -46,7 +49,10 @@
"autoload-dev": {
"psr-4": {
"Neusta\\Pimcore\\BackendBrandingBundle\\Tests\\": "tests/"
}
},
"classmap": [
"tests/app/TestKernel.php"
]
},
"config": {
"allow-plugins": {
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/CssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __invoke(): Response
return new Response(
$this->cssProvider->getRules()->toString(),
Response::HTTP_OK,
['Content-type' => 'text/css'],
['Content-Type' => 'text/css'],
);
}
}
10 changes: 10 additions & 0 deletions src/Css/CssRuleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ final class CssRuleList
/** @var list<CssRule> */
private array $rules = [];

/**
* @param iterable<CssRule> $rules
*/
public function addRules(iterable $rules): void
{
foreach ($rules as $rule) {
$this->addRule($rule);
}
}

public function addRule(CssRule $rule): void
{
$this->rules[] = $rule;
Expand Down
4 changes: 1 addition & 3 deletions src/CssProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function getRules(): CssRuleList
$css = new CssRuleList();

foreach ($this->providers as $provider) {
foreach ($provider() as $rule) {
$css->addRule($rule);
}
$css->addRules($provider());
}

return $css;
Expand Down
28 changes: 28 additions & 0 deletions tests/Controller/CssControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Tests\Controller;

use Neusta\Pimcore\TestingFramework\Database\ResetDatabase;
use Pimcore\Test\WebTestCase;
use Spatie\Snapshots\MatchesSnapshots;

class CssControllerTest extends WebTestCase
{
use MatchesSnapshots;
use ResetDatabase;

/**
* @test
*/
public function it_returns_css(): void
{
$client = self::createClient();

$client->request('GET', '/neusta/backend-branding/css');

self::assertResponseIsSuccessful();
self::assertResponseHeaderSame('Content-Type', 'text/css; charset=UTF-8');
$this->assertMatchesTextSnapshot($client->getResponse()->getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
body.x-body #pimcore_body {
background-color: #6428b4;
}

body.x-body #pimcore_loading.loaded {
background-color: #6428b4;
}

body.x-body .sf-minitoolbar {
background-color: #6428b4;
}

body.x-body #pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar {
background-color: #6428b4;
}

#pimcore_loading.loaded {
background-color: #6428b4;
}

.x-body .sf-minitoolbar {
background-color: #6428b4;
}

#pimcore_sidebar {
background-color: #028a0f;
}

#pimcore_loading.loaded {
background-color: #028a0f;
}

.x-body .sf-minitoolbar {
background-color: #028a0f;
}

#pimcore_signet {
background-image: url("logo.svg");
background-size: 70%;
background-position: center;
background-color: #000 !important;
}

#pimcore_avatar {
background-color: #000 !important;
}

#pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar {
background-image: url("logo.svg");
background-size: 40px;
background-position: center;
}
45 changes: 45 additions & 0 deletions tests/Css/CssPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Tests\Css;

use Neusta\Pimcore\BackendBrandingBundle\Css\CssProperty;
use PHPUnit\Framework\TestCase;

class CssPropertyTest extends TestCase
{
/**
* @test
*/
public function it_has_name_and_value(): void
{
self::assertSame('color: red', (string) new CssProperty('color', 'red'));
}

/**
* @test
*/
public function its_value_may_be_a_url(): void
{
self::assertSame(
'background-image: url("image.jpeg")',
(string) new CssProperty('background-image', 'image.jpeg', isUrl: true),
);
}

/**
* @test
*/
public function it_may_be_important(): void
{
self::assertSame(
'color: red !important',
(string) new CssProperty('color', 'red', isImportant: true),
);

self::assertSame(
'background-image: url("image.jpeg") !important',
(string) new CssProperty('background-image', 'image.jpeg', isUrl: true, isImportant: true),
);
}
}
40 changes: 40 additions & 0 deletions tests/Css/CssRuleListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Tests\Css;

use Neusta\Pimcore\BackendBrandingBundle\Css\CssProperty;
use Neusta\Pimcore\BackendBrandingBundle\Css\CssRule;
use Neusta\Pimcore\BackendBrandingBundle\Css\CssRuleList;
use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

class CssRuleListTest extends TestCase
{
use MatchesSnapshots;

/**
* @test
*/
public function it_contains_a_bunch_of_rules(): void
{
$ruleList = new CssRuleList();

$ruleList->addRule(new CssRule('#some-rule',
new CssProperty('color', 'blue'),
new CssProperty('margin', '0'),
new CssProperty('display', 'block'),
));

$ruleList->addRule(new CssRule('.another-rule',
new CssProperty('background-image', 'image.jpg', isUrl: true),
new CssProperty('padding', '0'),
));

$ruleList->addRule(new CssRule('.some.specific .rule',
new CssProperty('border', '5px solid black', isImportant: true),
));

$this->assertMatchesTextSnapshot($ruleList->toString());
}
}
42 changes: 42 additions & 0 deletions tests/Css/CssRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Tests\Css;

use Neusta\Pimcore\BackendBrandingBundle\Css\CssProperty;
use Neusta\Pimcore\BackendBrandingBundle\Css\CssRule;
use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

class CssRuleTest extends TestCase
{
use MatchesSnapshots;

/**
* @test
*/
public function it_is_a_selector_with_properties(): void
{
$this->assertMatchesTextSnapshot(new CssRule('.my-rule',
new CssProperty('color', 'blue'),
new CssProperty('margin', '0'),
new CssProperty('display', 'block'),
));
}

/**
* @test
*/
public function it_overwrites_properties_with_the_same_name(): void
{
$rule = new CssRule('.my-rule',
new CssProperty('color', 'blue'),
new CssProperty('margin', '0'),
new CssProperty('display', 'block'),
);

$rule->setProperty(new CssProperty('display', 'inline'));

$this->assertMatchesTextSnapshot($rule);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#some-rule {
color: blue;
margin: 0;
display: block;
}

.another-rule {
background-image: url("image.jpg");
padding: 0;
}

.some.specific .rule {
border: 5px solid black !important;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.my-rule {
color: blue;
margin: 0;
display: block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.my-rule {
color: blue;
margin: 0;
display: inline;
}
Loading

0 comments on commit 6480c71

Please sign in to comment.