Skip to content

Commit 16159ba

Browse files
committed
init: move classnames snippet and add merge coverage snippet
1 parent a2d9843 commit 16159ba

File tree

14 files changed

+305
-3
lines changed

14 files changed

+305
-3
lines changed

.github/workflows/phpunit.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "PHPUnit testing"
2+
run-name: ${{ github.actor }} is testing with phpunit
3+
4+
on: [pull_request]
5+
6+
jobs:
7+
phpunit:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Check out repository code
12+
uses: actions/checkout@master
13+
14+
- uses: php-actions/composer@master
15+
16+
- name: PHPUnit Tests
17+
uses: php-actions/phpunit@master

.gitignore

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
### MacOs ###
2+
.DS_Store
3+
4+
### Composer ###
5+
composer.lock
16
composer.phar
27
/vendor/
38

4-
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
9+
### PHPUnit ###
10+
.phpunit.result.cache
11+
12+
### Coverage ###
13+
/reports
14+
!/reports/.keep
15+
/coverage

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM php:fpm
2+
3+
RUN apt-get update -y
4+
RUN apt-get install -y zip git
5+
6+
# Install Xdebug extension
7+
RUN pecl install xdebug && docker-php-ext-enable xdebug
8+
RUN echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
9+
10+
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
start: build
2+
docker compose up -d
3+
4+
build:
5+
docker compose build
6+
7+
install:
8+
docker compose exec php composer install
9+
10+
test:
11+
docker compose exec php vendor/bin/phpunit --testdox src
12+
13+
classnames.test:
14+
docker compose exec php vendor/bin/phpunit --testsuite classnames
15+
16+
unit.test:
17+
docker compose exec php vendor/bin/phpunit --testsuite unit --coverage-php reports/unit.php
18+
19+
integration.test:
20+
docker compose exec php vendor/bin/phpunit --testsuite integration --coverage-php reports/integration.php
21+
22+
merge-coverage:
23+
docker compose exec php bin/merge-coverage.php

bin/merge-coverage.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__.'/../vendor/autoload.php';
5+
6+
use \SebastianBergmann\CodeCoverage\CodeCoverage;
7+
use \SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
8+
use \SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
9+
use \PHPUnit\Runner\Version;
10+
11+
$unit = include('reports/unit.php');
12+
$integration = include('reports/integration.php');
13+
14+
$unit->merge($integration);
15+
16+
$reference = sprintf(' and <a href="https://phpunit.de/">PHPUnit %s</a>', Version::id());
17+
18+
(new HtmlReport($reference))->process($unit, 'coverage');
19+
20+
21+
22+

compose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: '3'
2+
3+
services:
4+
php:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
volumes:
9+
- .:/var/www/html

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"autoload": {
3+
"files": [ "src/classnames/function.php" ],
4+
"psr-4": {
5+
"ClassNames\\": "src/classnames",
6+
"MergeCoverage\\": "src/merge-coverage"
7+
}
8+
},
9+
"require-dev": {
10+
"phpunit/phpunit": "~11"
11+
}
12+
}

phpunit.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
6+
<testsuites>
7+
<testsuite name="unit">
8+
<directory>src/merge-coverage/Tests/Unit</directory>
9+
</testsuite>
10+
<testsuite name="integration">
11+
<directory>src/merge-coverage/Tests/Integration</directory>
12+
</testsuite>
13+
<testsuite name="classnames">
14+
<directory>src/classnames/Tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
19+
<include>
20+
<directory>src/merge-coverage</directory>
21+
</include>
22+
23+
<exclude>
24+
<directory>src/merge-coverage/Tests</directory>
25+
</exclude>
26+
</source>
27+
</phpunit>

src/classnames/ClassNames.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace ClassNames;
4+
5+
/**
6+
* Example
7+
* <div class="<?= ClassNames::from(['solid' => true, 'red' => false, 'large' => true]) ?>"></div>
8+
* <div class="<?= ClassNames::from(['solid', 'red' => false, 'large' => true]) ?>"></div>
9+
* <div class="<?= ClassNames::from('solid', ['red' => false, 'large' => true]) ?>"></div>
10+
*
11+
* Result
12+
* <div class="solid large"></div>
13+
*
14+
*/
15+
class ClassNames
16+
{
17+
private $classNames = [];
18+
19+
private function __construct($params)
20+
{
21+
foreach ($params as $param) {
22+
if (!is_array($param)) {
23+
$param = [$param];
24+
}
25+
$this->extract($param);
26+
}
27+
}
28+
29+
private function extract(array $classes)
30+
{
31+
foreach($classes as $index => $value) {
32+
if (is_numeric($index) && is_string($value)) {
33+
$this->classNames[] = $value;
34+
} elseif (is_string($index) && $value === true) {
35+
$this->classNames[] = $index;
36+
}
37+
}
38+
}
39+
40+
public static function from(...$params): string
41+
{
42+
return (string) new self($params);
43+
}
44+
45+
public function __toString()
46+
{
47+
return implode(' ', $this->classNames);
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ClassNames\Tests;
5+
6+
use ClassNames\ClassNames;
7+
use function ClassNames\classnames;
8+
use PHPUnit\Framework\TestCase;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Attributes\Test;
11+
12+
class ClassNamesTest extends TestCase
13+
{
14+
public static function providerClassNames()
15+
{
16+
return [
17+
['solid large', ['solid' => true, 'red' => false, 'large' => true]],
18+
['solid large', 'solid', 'large'],
19+
['solid large', 'solid', ['large' => true]],
20+
['solid large', ['solid' => true], ['large' => true]],
21+
];
22+
}
23+
24+
#[Test]
25+
#[DataProvider('providerClassNames')]
26+
public function classNames($expected, ...$params): void
27+
{
28+
$result = ClassNames::from(...$params);
29+
30+
self::assertSame($expected, $result);
31+
}
32+
33+
#[Test]
34+
#[DataProvider('providerClassNames')]
35+
public function classNamesAsFunction($expected, ...$params): void
36+
{
37+
$result = classnames(...$params);
38+
39+
self::assertSame($expected, (string) $result);
40+
}
41+
}

0 commit comments

Comments
 (0)