-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: move classnames snippet and add merge coverage snippet
- Loading branch information
Showing
14 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |