Skip to content

Commit

Permalink
action test
Browse files Browse the repository at this point in the history
  • Loading branch information
rosven9856 committed Sep 27, 2024
1 parent 143a047 commit 8d35b36
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
require __DIR__ . '/vendor/autoload.php';

try {
$core = new App\Action();
$core->run();
$action = new App\Action();
$action->run();
} catch (Exception) {
// echo 'Error: ' . $e->getMessage() . "\n" . 'Trace: ' . $e->getTraceAsString();
}
2 changes: 1 addition & 1 deletion infection.json5.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
},
"tmpDir": "var",
"minCoveredMsi": 90,
"minCoveredMsi": 20,
"mutators": {
"@default": true
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
cacheDirectory="var/phpunit"
colors="true"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
Expand Down
106 changes: 106 additions & 0 deletions tests/ActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace App;

use App\Configuration\Configuration;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Action::class)]
final class ActionTest extends TestCase
{
/**
* @var string
*/
private const string DEFAULT_ENV_GITHUB_WORKSPACE = '/usr/bin/app';

private ?Action $action;

#[\Override]
protected function setUp(): void
{
putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE);
putenv('directory=.build');

$this->action = new Action();

$this->removeBuildDirectory();
}

#[\Override]
protected function tearDown(): void
{
putenv('GITHUB_WORKSPACE=');
putenv('directory=');

$this->removeBuildDirectory();

$this->action = null;
}

private function getConfiguration(): Configuration
{
if ($this->action instanceof Action) {
$reflector = new \ReflectionObject($this->action);
$property = $reflector->getProperty('configuration');
$property->setAccessible(true);

$configuration = $property->getValue($this->action);

if ($configuration instanceof Configuration) {
return $configuration;
}
}

return new Configuration();
}

private function removeBuildDirectory(): void
{
$configuration = $this->getConfiguration();
$directory = (string) $configuration->get('build.directory');

if (is_dir($directory)) {
$this->removeDirectory($directory);
}
}

private function removeDirectory(string $dir): void
{
$dir = realpath($dir);

if (is_dir($dir)) {

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir),
\RecursiveIteratorIterator::CHILD_FIRST,
);

foreach ($iterator as $path) {

if (!$path instanceof \SplFileInfo) {
continue;
}

if ($path->isDir()) {
rmdir((string) $path);
} else {
unlink((string) $path);
}
}

rmdir($dir);
}
}

public function testBuildComposerPackage(): void
{
if ($this->action instanceof Action) {
$this->action->run();
}

self::assertFileExists((string) $this->getConfiguration()->get('build.file'));
}
}

0 comments on commit 8d35b36

Please sign in to comment.