Skip to content

Commit

Permalink
init: move classnames snippet and add merge coverage snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
codisart committed Mar 30, 2024
1 parent a2d9843 commit 16159ba
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/phpunit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "PHPUnit testing"
run-name: ${{ github.actor }} is testing with phpunit

on: [pull_request]

jobs:
phpunit:
runs-on: ubuntu-latest

steps:
- name: Check out repository code
uses: actions/checkout@master

- uses: php-actions/composer@master

- name: PHPUnit Tests
uses: php-actions/phpunit@master
15 changes: 12 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
### MacOs ###
.DS_Store

### Composer ###
composer.lock
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
### PHPUnit ###
.phpunit.result.cache

### Coverage ###
/reports
!/reports/.keep
/coverage
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM php:fpm

RUN apt-get update -y
RUN apt-get install -y zip git

# Install Xdebug extension
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
start: build
docker compose up -d

build:
docker compose build

install:
docker compose exec php composer install

test:
docker compose exec php vendor/bin/phpunit --testdox src

classnames.test:
docker compose exec php vendor/bin/phpunit --testsuite classnames

unit.test:
docker compose exec php vendor/bin/phpunit --testsuite unit --coverage-php reports/unit.php

integration.test:
docker compose exec php vendor/bin/phpunit --testsuite integration --coverage-php reports/integration.php

merge-coverage:
docker compose exec php bin/merge-coverage.php
22 changes: 22 additions & 0 deletions bin/merge-coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../vendor/autoload.php';

use \SebastianBergmann\CodeCoverage\CodeCoverage;
use \SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
use \SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
use \PHPUnit\Runner\Version;

$unit = include('reports/unit.php');
$integration = include('reports/integration.php');

$unit->merge($integration);

$reference = sprintf(' and <a href="https://phpunit.de/">PHPUnit %s</a>', Version::id());

(new HtmlReport($reference))->process($unit, 'coverage');




9 changes: 9 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'

services:
php:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/var/www/html
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"autoload": {
"files": [ "src/classnames/function.php" ],
"psr-4": {
"ClassNames\\": "src/classnames",
"MergeCoverage\\": "src/merge-coverage"
}
},
"require-dev": {
"phpunit/phpunit": "~11"
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="unit">
<directory>src/merge-coverage/Tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>src/merge-coverage/Tests/Integration</directory>
</testsuite>
<testsuite name="classnames">
<directory>src/classnames/Tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src/merge-coverage</directory>
</include>

<exclude>
<directory>src/merge-coverage/Tests</directory>
</exclude>
</source>
</phpunit>
49 changes: 49 additions & 0 deletions src/classnames/ClassNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace ClassNames;

/**
* Example
* <div class="<?= ClassNames::from(['solid' => true, 'red' => false, 'large' => true]) ?>"></div>
* <div class="<?= ClassNames::from(['solid', 'red' => false, 'large' => true]) ?>"></div>
* <div class="<?= ClassNames::from('solid', ['red' => false, 'large' => true]) ?>"></div>
*
* Result
* <div class="solid large"></div>
*
*/
class ClassNames
{
private $classNames = [];

private function __construct($params)
{
foreach ($params as $param) {
if (!is_array($param)) {
$param = [$param];
}
$this->extract($param);
}
}

private function extract(array $classes)
{
foreach($classes as $index => $value) {
if (is_numeric($index) && is_string($value)) {
$this->classNames[] = $value;
} elseif (is_string($index) && $value === true) {
$this->classNames[] = $index;
}
}
}

public static function from(...$params): string
{
return (string) new self($params);
}

public function __toString()
{
return implode(' ', $this->classNames);
}
}
41 changes: 41 additions & 0 deletions src/classnames/Tests/ClassNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace ClassNames\Tests;

use ClassNames\ClassNames;
use function ClassNames\classnames;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;

class ClassNamesTest extends TestCase
{
public static function providerClassNames()
{
return [
['solid large', ['solid' => true, 'red' => false, 'large' => true]],
['solid large', 'solid', 'large'],
['solid large', 'solid', ['large' => true]],
['solid large', ['solid' => true], ['large' => true]],
];
}

#[Test]
#[DataProvider('providerClassNames')]
public function classNames($expected, ...$params): void
{
$result = ClassNames::from(...$params);

self::assertSame($expected, $result);
}

#[Test]
#[DataProvider('providerClassNames')]
public function classNamesAsFunction($expected, ...$params): void
{
$result = classnames(...$params);

self::assertSame($expected, (string) $result);
}
}
18 changes: 18 additions & 0 deletions src/classnames/function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace ClassNames;

/**
* Example
* <div class="<?= cn(['solid' => true, 'red' => false, 'large' => true]) ?>"></div>
* <div class="<?= cn(['solid', 'red' => false, 'large' => true]) ?>"></div>
* <div class="<?= cn('solid', ['red' => false, 'large' => true]) ?>"></div>
*
* Result
* <div class="solid large"></div>
*
*/
function classnames(...$params): string {
return ClassNames::from(...$params);
}
25 changes: 25 additions & 0 deletions src/merge-coverage/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace MergeCoverage;

class Main
{
public function add(int ...$operands) {
if (count($operands) === 0) {
return 0;
}
return array_sum($operands);
}

public function multiply(int ...$operands) {
if (count($operands) === 0) {
return 1;
}
$result = 1;
foreach($operands as $operand){
$result *= $operand;
}
return $result;
}
}
20 changes: 20 additions & 0 deletions src/merge-coverage/Tests/Integration/MainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace MergeCoverage\Tests\Integration;

use PHPUnit\Framework\TestCase;
use MergeCoverage\Main;

#[CoversClass(Codisart\MergeCoverage\Main::class)]
class MainTest extends TestCase
{

public function testMultiply() {
$main = new Main();

self::assertEquals(1, $main->multiply());
self::assertEquals(1, $main->multiply(1));
self::assertEquals(2, $main->multiply(1,2));
}
}
20 changes: 20 additions & 0 deletions src/merge-coverage/Tests/Unit/MainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace MergeCoverage\Tests\Unit;

use PHPUnit\Framework\TestCase;
use MergeCoverage\Main;

#[CoversClass(Codisart\MergeCoverage\Main::class)]
class MainTest extends TestCase
{

public function testAdd() {
$main = new Main();

self::assertEquals(0, $main->add());
self::assertEquals(1, $main->add(1));
self::assertEquals(3, $main->add(1,2));
}
}

0 comments on commit 16159ba

Please sign in to comment.