Skip to content

Commit

Permalink
Merge pull request #79 from TheDragonCode/main
Browse files Browse the repository at this point in the history
Actualize branch
  • Loading branch information
Andrey Helldar committed May 5, 2022
2 parents 8d6a63e + 32d6fd1 commit 20d88d1
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-check.yml
Expand Up @@ -11,4 +11,4 @@ jobs:
uses: actions/checkout@v3

- name: Checking PHP Syntax
uses: TheDragonCode/codestyler@v2.0.0
uses: TheDragonCode/codestyler@v2.0.3
2 changes: 1 addition & 1 deletion .github/workflows/lint-fixer.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/checkout@v3

- name: Checking PHP Syntax
uses: TheDragonCode/codestyler@v2.0.0
uses: TheDragonCode/codestyler@v2.0.3
with:
github_token: ${{ secrets.COMPOSER_TOKEN }}
fix: true
25 changes: 25 additions & 0 deletions .github/workflows/phpunit.yml
@@ -0,0 +1,25 @@
name: phpunit

on: [ push, pull_request ]

jobs:
build:
runs-on: ubuntu-latest

name: PHP Unit

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv, json
coverage: none

- name: Install dependencies
run: composer update --prefer-stable --prefer-dist --no-progress --no-interaction

- name: Execute tests
run: sudo vendor/bin/phpunit --colors=always
12 changes: 10 additions & 2 deletions composer.json
Expand Up @@ -14,15 +14,23 @@
"ext-json": "*",
"dragon-code/support": "^6.1",
"friendsofphp/php-cs-fixer": "^3.8",
"symfony/console": "^5.0 || ^6.0",
"symfony/yaml": "^5.0 || ^6.0"
"symfony/console": "^6.0",
"symfony/yaml": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"DragonCode\\CodeStyler\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"bin": [
"bin/codestyle"
]
Expand Down
22 changes: 22 additions & 0 deletions phpunit.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
7 changes: 5 additions & 2 deletions shell/config.sh
Expand Up @@ -8,5 +8,8 @@
};

# Set git config
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"

# Set safe directory
git config --global --add safe.directory /github/workspace
32 changes: 23 additions & 9 deletions src/Support/PhpVersion.php
Expand Up @@ -14,6 +14,8 @@ class PhpVersion

public const DEFAULT = '8.1';

public const MIN = '7.2';

public function get(): string
{
if ($composer = $this->composer()) {
Expand All @@ -25,24 +27,36 @@ public function get(): string

protected function find(array $composer)
{
$version = Arr::get($composer, 'require.php', Arr::get($composer, 'require-dev.php', ''));
preg_match_all('/\d\.\d/', (string) $this->getVersions($composer), $output);

preg_match_all('/\d\.\d/', $version, $output);
return Arr::of($output[0])
->map(fn (string $value) => $this->getMinVersion($value))
->unique()
->sort()
->values()
->first(default: self::DEFAULT);
}

sort($output[0]);
protected function getVersions(array $composer, array $keys = ['require.php', 'require-dev.php']): ?string
{
foreach ($keys as $key) {
if ($versions = Arr::get($composer, $key)) {
return $versions;
}
}

$versions = Arr::of($output[0])
->filter(static fn (string $value) => Version::of($value)->gte('7.2'))
->values()
->toArray();
return null;
}

return $versions[0] ?? self::DEFAULT;
protected function getMinVersion(string $version): string
{
return Version::of($version)->gt(self::MIN) ? self::MIN : $version;
}

protected function composer(): ?array
{
if ($path = realpath('./composer.json')) {
return file_exists($path) ? json_decode(file_get_contents($path), true) : null;
return Arr::ofFile($path)->toArray();
}

return null;
Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/Support/PhpVersion.php
@@ -0,0 +1,22 @@
<?php

namespace Tests\Fixtures\Support;

use DragonCode\CodeStyler\Support\PhpVersion as BasePhpVersion;

class PhpVersion extends BasePhpVersion
{
protected array $composer = [];

public function setComposer(array $values): self
{
$this->composer = $values;

return $this;
}

protected function composer(): ?array
{
return $this->composer;
}
}
137 changes: 137 additions & 0 deletions tests/Support/PhpVersionTest.php
@@ -0,0 +1,137 @@
<?php

namespace Tests\Support;

use Tests\Fixtures\Support\PhpVersion;
use Tests\TestCase;

class PhpVersionTest extends TestCase
{
public function test72()
{
$helper = PhpVersion::make()->setComposer([
'require' => [
'php' => '^8.0 || ^7.1',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('7.2', $helper);
}

public function test72Dev()
{
$helper = PhpVersion::make()->setComposer([
'require-dev' => [
'php' => '^8.0 || ^7.1',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('7.2', $helper);
}

public function test74()
{
$helper = PhpVersion::make()->setComposer([
'require' => [
'php' => '^8.0 || ^7.4',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('7.4', $helper);
}

public function test74Dev()
{
$helper = PhpVersion::make()->setComposer([
'require-dev' => [
'php' => '^8.0 || ^7.4',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('7.4', $helper);
}

public function test80()
{
$helper = PhpVersion::make()->setComposer([
'require' => [
'php' => '^8.0',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.0', $helper);
}

public function test80Dev()
{
$helper = PhpVersion::make()->setComposer([
'require-dev' => [
'php' => '^8.0',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.0', $helper);
}

public function test81()
{
$helper = PhpVersion::make()->setComposer([
'require' => [
'php' => '^8.1',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.1', $helper);
}

public function test81Dev()
{
$helper = PhpVersion::make()->setComposer([
'require-dev' => [
'php' => '^8.1',
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.1', $helper);
}

public function testDefault()
{
$helper = PhpVersion::make()->setComposer([
'require' => [
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.1', $helper);
}

public function testDefaultDev()
{
$helper = PhpVersion::make()->setComposer([
'require-dev' => [
'ext-json' => '*',
'foo/bar' => '^1.0',
],
])->get();

$this->assertSame('8.1', $helper);
}
}
9 changes: 9 additions & 0 deletions tests/TestCase.php
@@ -0,0 +1,9 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
}

0 comments on commit 20d88d1

Please sign in to comment.