Skip to content

Commit

Permalink
Merge pull request #24 from Chris53897/feature/improve
Browse files Browse the repository at this point in the history
feat: improvments github actions, composer.json
  • Loading branch information
lorisleiva authored Oct 1, 2021
2 parents 49dc8dc + 76c066d commit 1824d4c
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 138 deletions.
67 changes: 31 additions & 36 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
# .github/workflows/tests.yaml
name: Tests

on: [push]
on: ["push", "pull_request"]

jobs:
phpunit73:
name: "Tests on PHP 7.3"
tests:
runs-on: ubuntu-latest
container:
image: lorisleiva/laravel-docker:7.3
steps:
- uses: actions/checkout@v2
- name: Validate composer.json and composer.lock
run: composer validate
- name: Cache dependencies
uses: actions/cache@v1
with:
path: /composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run tests
run: phpunit

phpunit74:
name: "Tests on PHP 7.4"
runs-on: ubuntu-latest
container:
image: lorisleiva/laravel-docker:7.4
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
stability: [ prefer-lowest, prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} tests
steps:
- uses: actions/checkout@v2
- name: Validate composer.json and composer.lock
run: composer validate
- name: Cache dependencies
uses: actions/cache@v1
with:
path: /composer/cache/files
key: dependencies-composer-${{ hashFiles('composer.json') }}
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run tests
run: phpunit
# basically git clone
- uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

# use PHP of specific version
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pcov
coverage: pcov

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress --no-suggest

- name: Execute tests
run: vendor/bin/phpunit --verbose
8 changes: 3 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
}
],
"require": {
"php" : "^7.3 || ^8.0"
"php" : "^7.4 || ^8.0"
},
"require-dev": {
"phpunit/phpunit" : "^8.0"
"phpunit/phpunit" : "^8.5.21 || ^9.5"
},
"autoload": {
"psr-4": {
Expand All @@ -30,7 +30,5 @@
"psr-4": {
"Lorisleiva\\CronTranslator\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
28 changes: 15 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit 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"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
46 changes: 15 additions & 31 deletions src/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

class CronExpression
{
/** @var string */
public $raw;

/** @var MinutesField */
public $minute;

/** @var HoursField */
public $hour;

/** @var DaysOfMonthField */
public $day;

/** @var MonthsField */
public $month;

/** @var DaysOfWeekField */
public $weekday;

/** @var string */
public $locale;

/** @var bool */
public $timeFormat24hours;

/** @var array */
public $translations;
public string $raw;
public MinutesField $minute;
public HoursField $hour;
public DaysOfMonthField $day;
public MonthsField $month;
public DaysOfWeekField $weekday;
public string $locale;
public bool $timeFormat24hours;
public array $translations;

public function __construct(string $cron, string $locale = 'en', bool $timeFormat24hours = false)
{
Expand All @@ -46,7 +29,7 @@ public function __construct(string $cron, string $locale = 'en', bool $timeForma
$this->loadTranslations();
}

public function getFields()
public function getFields(): array
{
return [
$this->minute,
Expand All @@ -61,9 +44,7 @@ public function langCountable(string $type, int $number)
{
$array = $this->translations[$type];

$value = isset($array[$number])
? $array[$number]
: ($array['default'] ?: '');
$value = $array[$number] ?? ($array['default'] ?: '');

return str_replace(':number', $number, $value);
}
Expand All @@ -86,6 +67,9 @@ protected function ensureLocaleExists(string $fallbackLocale = 'en')
}
}

/**
* @throws TranslationFileMissingException
*/
protected function loadTranslations()
{
$this->translations = [
Expand All @@ -108,7 +92,7 @@ protected function loadTranslationFile(string $file)
return include $filename;
}

protected function getTranslationDirectory()
protected function getTranslationDirectory(): string
{
return __DIR__ . '/lang/' . $this->locale;
}
Expand Down
6 changes: 3 additions & 3 deletions src/CronTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CronTranslator
{
private static $extendedMap = [
private static array $extendedMap = [
'@yearly' => '0 0 1 1 *',
'@annually' => '0 0 1 1 *',
'@monthly' => '0 0 1 * *',
Expand All @@ -15,7 +15,7 @@ class CronTranslator
'@hourly' => '0 * * * *'
];

public static function translate(string $cron, string $locale = 'en', bool $timeFormat24hours = false)
public static function translate(string $cron, string $locale = 'en', bool $timeFormat24hours = false): string
{
if (isset(self::$extendedMap[$cron])) {
$cron = self::$extendedMap[$cron];
Expand Down Expand Up @@ -67,7 +67,7 @@ protected static function orderFields(array $fields)
);
}

protected static function filterType(array $fields, ...$types)
protected static function filterType(array $fields, ...$types): array
{
return array_filter($fields, function (Field $field) use ($types) {
return $field->hasType(...$types);
Expand Down
30 changes: 13 additions & 17 deletions src/CronType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ class CronType
'Every', 'Increment', 'Multiple', 'Once',
];

/** @var string */
public $type;

/** @var ?int */
public $value;

/** @var ?int */
public $count;

/** @var ?int */
public $increment;
public string $type;
public ?int $value;
public ?int $count;
public ?int $increment;

private function __construct(string $type, ?int $value = null, ?int $count = null, ?int $increment = null)
{
Expand All @@ -28,27 +21,30 @@ private function __construct(string $type, ?int $value = null, ?int $count = nul
$this->increment = $increment;
}

public static function every()
public static function every(): CronType
{
return new static('Every');
}

public static function increment(int $increment, int $count = 1)
public static function increment(int $increment, int $count = 1): CronType
{
return new static('Increment', null, $count, $increment);
}

public static function multiple(int $count)
public static function multiple(int $count): CronType
{
return new static('Multiple', null, $count);
}

public static function once(int $value)
public static function once(int $value): CronType
{
return new static('Once', $value);
}

public static function parse(string $expression)
/**
* @throws CronParsingException
*/
public static function parse(string $expression): CronType
{
// Parse "*".
if ($expression === '*') {
Expand Down Expand Up @@ -88,7 +84,7 @@ public static function parse(string $expression)
throw new CronParsingException($expression);
}

public function hasType()
public function hasType(): bool
{
return in_array($this->type, func_get_args());
}
Expand Down
8 changes: 4 additions & 4 deletions src/DaysOfMonthField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DaysOfMonthField extends Field
{
public $position = 2;
public int $position = 2;

public function translateEvery()
{
Expand Down Expand Up @@ -38,16 +38,16 @@ public function translateMultiple()
]);
}

public function translateOnce()
public function translateOnce(): ?string
{
$month = $this->expression->month;

if ($month->hasType('Once')) {
return; // MonthsField adapts to "On January the 1st".
return null; // MonthsField adapts to "On January the 1st".
}

if ($month->hasType('Every') && ! $month->dropped) {
return; // MonthsField adapts to "The 1st of every month".
return null; // MonthsField adapts to "The 1st of every month".
}

if ($month->hasType('Every') && $month->dropped) {
Expand Down
4 changes: 2 additions & 2 deletions src/DaysOfWeekField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DaysOfWeekField extends Field
{
public $position = 4;
public int $position = 4;

public function translateEvery()
{
Expand Down Expand Up @@ -43,7 +43,7 @@ public function translateOnce()
]);
}

public function format()
public function format(): string
{
$weekday = $this->getValue() === 0 ? 7 : $this->getValue();

Expand Down
Loading

0 comments on commit 1824d4c

Please sign in to comment.