From bfa6866430cbb3a97f981fe0d769e2fef931eb62 Mon Sep 17 00:00:00 2001 From: Arthur LORENT Date: Mon, 23 May 2022 17:42:43 +0200 Subject: [PATCH] v3.0 (#9) * Added PHP 8.1 support * Added Laravel 9 support * Dropped PHP 7.4 and Laravel 6 support --- .editorconfig | 3 - .gitattributes | 1 - .github/CONTRIBUTING.md | 55 ++++++++ .github/FUNDING.yml | 1 + .github/ISSUE_TEMPLATE/config.yml | 11 ++ .github/SECURITY.md | 3 + .github/workflows/{main.yml => ci.yml} | 27 ++-- .gitignore | 6 +- CHANGELOG.md | 8 ++ README.md | 8 +- composer.json | 31 ++--- docs/upgrade-guides/from-v1-to-v2.md | 2 +- docs/upgrade-guides/from-v2-to-v3.md | 25 ++++ phpcs.xml | 19 ++- phpmd.xml | 119 ++++++++---------- phpstan.neon.dist | 1 + phpunit.xml | 27 ++-- src/Callbacks/OnStuckJobs.php | 7 +- src/Commands/NotifyStuckJobs.php | 12 +- src/Commands/SimulateStuckJobs.php | 12 +- src/Notifications/JobsAreStuck.php | 26 +--- src/StuckJobsNotifier.php | 26 ++-- src/StuckJobsNotifierServiceProvider.php | 10 -- ...dJobsNotifierTestCase.php => TestCase.php} | 24 +--- tests/Unit/StuckJobsMonitoringTest.php | 71 +++++++---- ..._10_12_200000_create_failed_jobs_table.php | 25 ++-- 26 files changed, 301 insertions(+), 259 deletions(-) create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/SECURITY.md rename .github/workflows/{main.yml => ci.yml} (69%) create mode 100644 docs/upgrade-guides/from-v2-to-v3.md rename tests/{FailedJobsNotifierTestCase.php => TestCase.php} (63%) diff --git a/.editorconfig b/.editorconfig index 7d38cf2..a7c44dd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,3 @@ -; This file is for unifying the coding style for different editors and IDEs. -; More information at http://editorconfig.org - root = true [*] diff --git a/.gitattributes b/.gitattributes index 4bffc1c..c11b808 100755 --- a/.gitattributes +++ b/.gitattributes @@ -8,7 +8,6 @@ /.editorconfig export-ignore /.gitattributes export-ignore /.gitignore export-ignore -/.scrutinizer.yml export-ignore /phpcs.xml export-ignore /phpmd.xml export-ignore /phpstan.neon.dist export-ignore diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..546274f --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[PSR-12 Coding Standard](https://www.php-fig.org/psr/psr-12/)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +**Happy coding**! diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 608def2..60b3450 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,3 @@ +github: Okipa ko_fi: arthurlorent custom: https://paypal.me/arthurlorent diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..faf88b6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/okipa/laravel-form-components/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/okipa/laravel-form-components/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a bug + url: https://github.com/okipa/laravel-form-components/issues/new + about: Report a reproducable bug diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..6ce557d --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,3 @@ +# Security Policy + +If you discover any security related issues, please email arthur.lorent@gmail.com instead of using the issue tracker. diff --git a/.github/workflows/main.yml b/.github/workflows/ci.yml similarity index 69% rename from .github/workflows/main.yml rename to .github/workflows/ci.yml index e920430..ccf3851 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/ci.yml @@ -14,13 +14,15 @@ jobs: strategy: fail-fast: true matrix: - php: ['7.4', '8.0'] - laravel: ['7.*', '8.*'] + php: ['8.1', '8.0'] + laravel: ['9.*', '8.*'] include: - - laravel: '7.*' - testbench: '5.*' + - laravel: '9.*' + testbench: '7.*' + larastan: '2.*' - laravel: '8.*' testbench: '6.*' + larastan: '1.*' name: P${{ matrix.php }} - L${{ matrix.laravel }} @@ -49,30 +51,29 @@ jobs: # Lower PHP and laravel versions. - name: PHPUnit - if: matrix.php != '8.0' || matrix.laravel != '8.*' - run: vendor/bin/phpunit + if: matrix.php != '8.1' || matrix.laravel != '9.*' + run: vendor/bin/testbench package:test --parallel --no-coverage # Last PHP and laravel versions. - name: Code analysis - if: matrix.php == '8.0' && matrix.laravel == '8.*' + if: matrix.php == '8.1' && matrix.laravel == '9.*' run: | vendor/bin/phpcs - vendor/bin/phpmd config,src text phpmd.xml + vendor/bin/phpmd config,src,tests text phpmd.xml vendor/bin/phpstan analyse - name: PHPUnit + Code coverage - if: matrix.php == '8.0' && matrix.laravel == '8.*' + if: matrix.php == '8.1' && matrix.laravel == '9.*' run: | mkdir -p build/logs - vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml + vendor/bin/testbench package:test --parallel --coverage-text --coverage-clover build/logs/clover.xml - name: Code coverage upload to Coveralls - if: matrix.php == '8.0' && matrix.laravel == '8.*' + if: matrix.php == '8.1' && matrix.laravel == '9.*' env: COVERALLS_RUN_LOCALLY: 1 COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} run: | - echo ${GITHUB_REF##*/} - composer require php-coveralls/php-coveralls + composer require php-coveralls/php-coveralls --with-all-dependencies vendor/bin/php-coveralls -v --coverage_clover=build/logs/clover.xml diff --git a/.gitignore b/.gitignore index 33ebff1..e3e7ca6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ build composer.lock vendor coverage -tests/Support/temp -tests/temp -.phpunit.result.cache \ No newline at end of file +.php-cs-fixer.cache +.phpunit.result.cache +.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index 394273e..3cd7102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [3.0.0](https://github.com/Okipa/laravel-stuck-jobs-notifier/compare/2.1.0...3.0.0) + +2022-05-23 + +* Added PHP 8.1 support +* Added Laravel 9 support +* Dropped PHP 7.4 and Laravel 6 support + ## [2.1.0](https://github.com/Okipa/laravel-stuck-jobs-notifier/compare/2.0.0...2.1.0) 2020-11-11 diff --git a/README.md b/README.md index 0906eba..2bbe882 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ![Laravel Stuck Jobs Notifier](/docs/laravel-stuck-jobs-notifier.png) -

+

Latest Stable Version @@ -30,11 +30,13 @@ Found this package helpful? Please consider supporting my work! | Laravel version | PHP version | Package version | |---|---|---| -| ^7.0 | ^7.4 | ^2.0 | -| ^6.0 | ^7.4 | ^1.0 | +| ^8.0 | ^9.0 | ^8.0 | ^8.1 | ^3.0 | +| ^7.0 | ^8.0 | ^7.4 | ^8.0 | ^2.0 | +| ^6.0 | ^7.0 | ^7.4 | ^1.0 | ## Upgrade guide +* [From v2 to V3](/docs/upgrade-guides/from-v2-to-v3.md) * [From v1 to V2](/docs/upgrade-guides/from-v1-to-v2.md) * [From okipa/failed-jobs-notifier](/docs/upgrade-guides/from-failed-job-notifier.md) diff --git a/composer.json b/composer.json index c259c0d..fa5cf6b 100644 --- a/composer.json +++ b/composer.json @@ -25,16 +25,19 @@ } ], "require": { - "php": "^7.4||^8.0", - "illuminate/support": "^7.0||^8.0" + "php": "^8.0|^8.1", + "illuminate/contracts": "^8.0|^9.0" }, "require-dev": { - "nunomaduro/larastan": "^0.6", - "orchestra/testbench": "^5.0||^6.0", - "phpmd/phpmd": "^2.8", - "squizlabs/php_codesniffer": "^3.5", - "laravel-notification-channels/webhook": "^2.0", - "laravel/slack-notification-channel": "^2.0" + "brianium/paratest": "^6.4", + "laravel-notification-channels/webhook": "^2.3", + "laravel/slack-notification-channel": "^2.4", + "nunomaduro/collision": "^5.10|^6.0", + "nunomaduro/larastan": "^1.0|^2.0", + "orchestra/testbench": "^6.0|^7.0", + "phpmd/phpmd": "^2.11", + "roave/security-advisories": "dev-latest", + "squizlabs/php_codesniffer": "^3.6" }, "autoload": { "psr-4": { @@ -47,13 +50,11 @@ } }, "scripts": { - "test": [ - "vendor/bin/phpcbf", - "vendor/bin/phpcs", - "vendor/bin/phpmd config,src text phpmd.xml", - "vendor/bin/phpstan analyse --memory-limit=-1 --error-format=table", - "vendor/bin/phpunit -d --memory-limit=-1 --testdox --coverage-text" - ] + "phpcs": ["vendor/bin/phpcbf", "vendor/bin/phpcs"], + "phpmd": "vendor/bin/phpmd config,src,tests text phpmd.xml", + "phpstan": "vendor/bin/phpstan analyse --memory-limit=2G", + "phpunit" : "vendor/bin/testbench package:test --parallel --no-coverage", + "test": ["@phpcs", "@phpmd", "@phpstan", "@phpunit"] }, "suggest": { "guzzlehttp/guzzle": "Required for sending notifications via Slack", diff --git a/docs/upgrade-guides/from-v1-to-v2.md b/docs/upgrade-guides/from-v1-to-v2.md index 41a4f5d..ab30e26 100644 --- a/docs/upgrade-guides/from-v1-to-v2.md +++ b/docs/upgrade-guides/from-v1-to-v2.md @@ -14,7 +14,7 @@ In the opposite, if you intend to send Slack and/or webhook notifications, insta ## See all changes -See all change with the [comparison tool](https://github.com/Okipa/laravel-table/compare/1.5.0...2.0.0). +See all change with the [comparison tool](https://github.com/Okipa/laravel-stuck-job-notifier/compare/1.5.0...2.0.0). ## Undocumented changes diff --git a/docs/upgrade-guides/from-v2-to-v3.md b/docs/upgrade-guides/from-v2-to-v3.md new file mode 100644 index 0000000..dc469c2 --- /dev/null +++ b/docs/upgrade-guides/from-v2-to-v3.md @@ -0,0 +1,25 @@ +# Upgrade from v1 to V2 + +Follow the steps below to upgrade the package. + +## Laravel and PHP supported versions update + +This package does now support PHP 8.0, PHP 8.1, Laravel 8 and Laravel 9. + +PHP 7.4 and Laravel 7.0 are no longer supported in v3. + +## How to upgrade ? + +Just bump the version the package to version `^3.0`. + +You also may have to bump the package optional dependencies listed in the [installation documentation part](../../README.md#installation) if you have installed them: +* `laravel-notification-channels/webhook` should be upgrade to version `^2.3` +* `laravel/slack-notification-channel` should be upgrade to version `^2.4` + +## See all changes + +See all change with the [comparison tool](https://github.com/Okipa/laravel-stuck-job-notifier/compare/2.1.0...3.0.0). + +## Undocumented changes + +If you see any forgotten and undocumented change, please submit a PR to add them to this upgrade guide. diff --git a/phpcs.xml b/phpcs.xml index fa47091..a661812 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,11 +1,28 @@ PSR-12 validation - ./config + + ./config ./src ./tests + + */*.js + */*.css + */*.xml + */*.blade.php + + + + ./tests/* + + ./tests/* + + + ./config/* + ./tests/* + diff --git a/phpmd.xml b/phpmd.xml index ee86b4e..57e20be 100644 --- a/phpmd.xml +++ b/phpmd.xml @@ -1,67 +1,58 @@ - Written using this resource : https://phpmd.org/rules/index.html - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 3 - - - - - - - - - - - - + xmlns="http://pmd.sf.net/ruleset/1.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" + xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> + Written using this resource : https://phpmd.org/rules/index.html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 3 + + + + + + + + + + + + + + diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b0dfefb..4b9cb7c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,6 +6,7 @@ parameters: paths: - config - src + - tests # The level 8 is the highest level level: 5 diff --git a/phpunit.xml b/phpunit.xml index d9883b5..4ec21cd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,31 +1,36 @@ - + executionOrder="random" + failOnWarning="true" + failOnRisky="true" + failOnEmptyTestSuite="true" + beStrictAboutOutputDuringTests="true" + verbose="true" + printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"> + + + tests + + + - src + ./src - - - ./tests/ - - - diff --git a/src/Callbacks/OnStuckJobs.php b/src/Callbacks/OnStuckJobs.php index eb16776..d087f01 100644 --- a/src/Callbacks/OnStuckJobs.php +++ b/src/Callbacks/OnStuckJobs.php @@ -8,12 +8,7 @@ class OnStuckJobs { - /** - * @param \Illuminate\Support\Collection $stuckJobs - * @param bool $isTesting - * - * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\StuckJobsDetected - */ + /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\StuckJobsDetected */ public function __invoke(Collection $stuckJobs, bool $isTesting = false) { $stuckJobsCount = $stuckJobs->count(); diff --git a/src/Commands/NotifyStuckJobs.php b/src/Commands/NotifyStuckJobs.php index 10ffbe2..07254c7 100644 --- a/src/Commands/NotifyStuckJobs.php +++ b/src/Commands/NotifyStuckJobs.php @@ -7,18 +7,10 @@ class NotifyStuckJobs extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ + /** @var string */ protected $signature = 'queue:stuck:notify'; - /** - * The console command description. - * - * @var string - */ + /** @var string */ protected $description = 'Notify when failed jobs are stuck for a defined number of hours.'; /** diff --git a/src/Commands/SimulateStuckJobs.php b/src/Commands/SimulateStuckJobs.php index 9945e01..e77f540 100644 --- a/src/Commands/SimulateStuckJobs.php +++ b/src/Commands/SimulateStuckJobs.php @@ -8,18 +8,10 @@ class SimulateStuckJobs extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ + /** @var string */ protected $signature = 'queue:stuck:simulate'; - /** - * The console command description. - * - * @var string - */ + /** @var string */ protected $description = 'Simulate stuck jobs detection for testing purpose.'; /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\StuckJobsDetected */ diff --git a/src/Notifications/JobsAreStuck.php b/src/Notifications/JobsAreStuck.php index 04c1ece..8f04459 100644 --- a/src/Notifications/JobsAreStuck.php +++ b/src/Notifications/JobsAreStuck.php @@ -3,6 +3,7 @@ namespace Okipa\LaravelStuckJobsNotifier\Notifications; use Carbon\Carbon; +use Carbon\CarbonInterface; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; @@ -11,37 +12,21 @@ class JobsAreStuck extends Notification { - protected Collection $stuckJobs; - - protected bool $isTesting; - protected int $stuckJobsCount; - protected Carbon $stuckSince; + protected CarbonInterface $stuckSince; - public function __construct(Collection $stuckJobs, bool $isTesting) + public function __construct(protected Collection $stuckJobs, protected bool $isTesting) { - $this->stuckJobs = $stuckJobs; - $this->isTesting = $isTesting; $this->stuckJobsCount = $stuckJobs->count(); $this->stuckSince = Carbon::parse($this->stuckJobs->min('failed_at')); } - /** - * Get the notification's delivery channels. - * - * @return array - */ public function via(): array { return config('stuck-jobs-notifier.channels'); } - /** - * Get the mail representation of the notification. - * - * @return \Illuminate\Notifications\Messages\MailMessage - */ public function toMail(): MailMessage { return (new MailMessage())->level('error') @@ -99,11 +84,6 @@ public function toSlack(): SlackMessage )); } - /** - * Get the webhook representation of the notification. - * - * @return \NotificationChannels\Webhook\WebhookMessage - */ public function toWebhook(): WebhookMessage { // Rocket chat webhook example. diff --git a/src/StuckJobsNotifier.php b/src/StuckJobsNotifier.php index 15997c3..4519a96 100644 --- a/src/StuckJobsNotifier.php +++ b/src/StuckJobsNotifier.php @@ -3,7 +3,9 @@ namespace Okipa\LaravelStuckJobsNotifier; use Carbon\Carbon; +use Carbon\CarbonInterface; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Okipa\LaravelStuckJobsNotifier\Callbacks\OnStuckJobs; @@ -35,10 +37,7 @@ public function notify(): void } } - /** - * @return bool - * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidAllowedToRun - */ + /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidAllowedToRun */ public function isAllowedToRun(): bool { $allowedToRun = config('stuck-jobs-notifier.allowed_to_run'); @@ -53,7 +52,6 @@ public function isAllowedToRun(): bool } /** - * @return \Illuminate\Support\Collection * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InexistentFailedJobsTable * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidHoursLimit */ @@ -65,9 +63,7 @@ public function getStuckFailedJobs(): Collection return DB::table('failed_jobs')->where('failed_at', '<=', $dateLimit)->get(); } - /** - * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InexistentFailedJobsTable - */ + /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InexistentFailedJobsTable */ public function checkFailedJobsTableExists(): void { if (! Schema::hasTable('failed_jobs')) { @@ -76,21 +72,15 @@ public function checkFailedJobsTableExists(): void } } - /** - * @return \Carbon\Carbon - * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidHoursLimit - */ - public function getDateLimit(): Carbon + /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidHoursLimit */ + public function getDateLimit(): CarbonInterface { $hoursLimit = $this->getHoursLimit(); - return Carbon::now()->subHours($hoursLimit); + return Date::now()->subHours($hoursLimit); } - /** - * @return int - * @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidHoursLimit - */ + /** @throws \Okipa\LaravelStuckJobsNotifier\Exceptions\InvalidHoursLimit */ public function getHoursLimit(): int { $hoursLimit = config('stuck-jobs-notifier.hours_limit'); diff --git a/src/StuckJobsNotifierServiceProvider.php b/src/StuckJobsNotifierServiceProvider.php index 28e9c89..6ace902 100644 --- a/src/StuckJobsNotifierServiceProvider.php +++ b/src/StuckJobsNotifierServiceProvider.php @@ -8,11 +8,6 @@ class StuckJobsNotifierServiceProvider extends ServiceProvider { - /** - * Bootstrap any application services. - * - * @return void - */ public function boot(): void { if ($this->app->runningInConsole()) { @@ -23,11 +18,6 @@ public function boot(): void ], 'stuck-jobs-notifier:config'); } - /** - * Register any application services. - * - * @return void - */ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/stuck-jobs-notifier.php', 'stuck-jobs-notifier'); diff --git a/tests/FailedJobsNotifierTestCase.php b/tests/TestCase.php similarity index 63% rename from tests/FailedJobsNotifierTestCase.php rename to tests/TestCase.php index ff6b3a6..3566937 100644 --- a/tests/FailedJobsNotifierTestCase.php +++ b/tests/TestCase.php @@ -4,19 +4,10 @@ use Faker\Factory; use Okipa\LaravelStuckJobsNotifier\StuckJobsNotifierServiceProvider; -use Orchestra\Testbench\TestCase; +use Orchestra\Testbench\TestCase as Orchestra; -abstract class FailedJobsNotifierTestCase extends TestCase +class TestCase extends Orchestra { - protected $faker; - - /** - * Define environment setup. - * - * @param \Illuminate\Foundation\Application $app - * - * @return void - */ protected function getEnvironmentSetUp($app): void { $app['config']->set('queue.default', 'sync'); @@ -28,25 +19,14 @@ protected function getEnvironmentSetUp($app): void ]); } - /** - * Get package providers. - * - * @return array - */ protected function getPackageProviders($app): array { return [StuckJobsNotifierServiceProvider::class]; } - /** - * Setup the test environment. - * - * @return void - */ protected function setUp(): void { parent::setUp(); $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); - $this->faker = Factory::create(); } } diff --git a/tests/Unit/StuckJobsMonitoringTest.php b/tests/Unit/StuckJobsMonitoringTest.php index 9339b3b..35fc903 100644 --- a/tests/Unit/StuckJobsMonitoringTest.php +++ b/tests/Unit/StuckJobsMonitoringTest.php @@ -17,61 +17,66 @@ use Okipa\LaravelStuckJobsNotifier\Test\Dummy\AnotherNotifiable; use Okipa\LaravelStuckJobsNotifier\Test\Dummy\Callbacks\AnotherCallback; use Okipa\LaravelStuckJobsNotifier\Test\Dummy\Notifications\AnotherNotification; -use Okipa\LaravelStuckJobsNotifier\Test\FailedJobsNotifierTestCase; +use Okipa\LaravelStuckJobsNotifier\Test\TestCase; -class StuckJobsMonitoringTest extends FailedJobsNotifierTestCase +class StuckJobsMonitoringTest extends TestCase { - public function setUp(): void + protected function setUp(): void { parent::setUp(); NotificationFacade::fake(); } - public function testAllowedToRunWithWrongValue(): void + /** @test */ + public function it_can_check_if_allowed_to_run_with_wrong_config_value(): void { config()->set('stuck-jobs-notifier.allowed_to_run', 'test'); $this->expectException(InvalidAllowedToRun::class); app(StuckJobsNotifier::class)->isAllowedToRun(); } - public function testAllowedToRunWithBoolean(): void + /** @test */ + public function it_can_check_if_allowed_to_run_with_boolean_value(): void { config()->set('stuck-jobs-notifier.allowed_to_run', false); $allowedToRun = app(StuckJobsNotifier::class)->isAllowedToRun(); self::assertFalse($allowedToRun); } - public function testAllowedToRunWithCallable(): void + /** @test */ + public function it_can_check_if_allowed_to_run_with_callable_value(): void { - config()->set('stuck-jobs-notifier.allowed_to_run', function () { - return true; - }); + config()->set('stuck-jobs-notifier.allowed_to_run', fn() => true); $allowedToRun = app(StuckJobsNotifier::class)->isAllowedToRun(); self::assertTrue($allowedToRun); } - public function testFailedJobTableDoesNotExists(): void + /** @test */ + public function it_can_fetch_failed_jobs_when_table_does_not_exist(): void { Schema::drop('failed_jobs'); $this->expectException(InexistentFailedJobsTable::class); app(StuckJobsNotifier::class)->checkFailedJobsTableExists(); } - public function testSetDaysLimitWithWrongValue(): void + /** @test */ + public function it_cant_get_hours_limit_with_wrong_config_value(): void { config()->set('stuck-jobs-notifier.hours_limit', 'test'); $this->expectException(InvalidHoursLimit::class); app(StuckJobsNotifier::class)->getHoursLimit(); } - public function testSetDaysLimitWithInt(): void + /** @test */ + public function it_can_get_hours_limit_with_int_config_value(): void { config()->set('stuck-jobs-notifier.hours_limit', 5); $hoursLimit = app(StuckJobsNotifier::class)->getHoursLimit(); self::assertEquals(5, $hoursLimit); } - public function testGetStuckFailedJobs(): void + /** @test */ + public function it_can_get_stuck_failed_jobs(): void { $failedAtDates = [ Carbon::now()->subHours(6)->startOfHour(), @@ -101,28 +106,32 @@ public function testGetStuckFailedJobs(): void } } - public function testSetCustomNotifiable(): void + /** @test */ + public function it_can_set_custom_notifiable(): void { config()->set('stuck-jobs-notifier.notifiable', AnotherNotifiable::class); $notifiable = app(StuckJobsNotifier::class)->getNotifiable(); self::assertInstanceOf(AnotherNotifiable::class, $notifiable); } - public function testSetCustomNotification(): void + /** @test */ + public function it_can_set_custom_notification(): void { config()->set('stuck-jobs-notifier.notification', AnotherNotification::class); $notification = app(StuckJobsNotifier::class)->getNotification(collect()); self::assertInstanceOf(AnotherNotification::class, $notification); } - public function testSetCustomCallback(): void + /** @test */ + public function it_can_set_custom_callback(): void { config()->set('stuck-jobs-notifier.callback', AnotherCallback::class); $callback = app(StuckJobsNotifier::class)->getCallback(); self::assertInstanceOf(AnotherCallback::class, $callback); } - public function setNothingHappensWhenNotAllowed(): void + /** @test */ + public function it_cant_send_notification_when_not_allowed_to_run(): void { DB::table('failed_jobs')->insert([ 'connection' => 'whatever', @@ -136,7 +145,8 @@ public function setNothingHappensWhenNotAllowed(): void NotificationFacade::assertNothingSent(); } - public function testNotificationIsSentWhenJobsAreStuck(): void + /** @test */ + public function it_can_send_notification_when_jobs_are_stuck(): void { DB::table('failed_jobs')->insert([ 'connection' => 'whatever', @@ -151,7 +161,8 @@ public function testNotificationIsSentWhenJobsAreStuck(): void NotificationFacade::assertSentTo(new Notifiable(), JobsAreStuck::class); } - public function testCallbackIsTriggeredWhenHobsAreStuck(): void + /** @test */ + public function it_can_trigger_callback_when_jobs_are_stuck(): void { DB::table('failed_jobs')->insert([ 'connection' => 'whatever', @@ -165,7 +176,8 @@ public function testCallbackIsTriggeredWhenHobsAreStuck(): void $this->artisan('queue:stuck:notify')->assertExitCode(0); } - public function testDefaultProcessesAreDownNotificationSingularMessage(): void + /** @test */ + public function it_can_send_default_processes_are_down_singular_notification_message(): void { $date = Carbon::now()->subHours(4); $stuckJobs = collect([ @@ -177,7 +189,7 @@ public function testDefaultProcessesAreDownNotificationSingularMessage(): void NotificationFacade::assertSentTo( new Notifiable(), JobsAreStuck::class, - function ($notification, $channels) use ($date) { + static function ($notification, $channels) use ($date) { self::assertEquals(config('stuck-jobs-notifier.channels'), $channels); // Mail $mailData = $notification->toMail($channels)->toArray(); @@ -214,7 +226,8 @@ function ($notification, $channels) use ($date) { ); } - public function testDefaultProcessesAreDownNotificationPluralMessage(): void + /** @test */ + public function it_can_send_default_processes_are_down_plural_notification_message(): void { $date = Carbon::now()->subHours(4); $stuckJobs = collect([ @@ -227,7 +240,7 @@ public function testDefaultProcessesAreDownNotificationPluralMessage(): void NotificationFacade::assertSentTo( new Notifiable(), JobsAreStuck::class, - function ($notification, $channels) use ($date) { + static function ($notification, $channels) use ($date) { self::assertEquals(config('stuck-jobs-notifier.channels'), $channels); // Mail $mailData = $notification->toMail($channels)->toArray(); @@ -264,7 +277,8 @@ function ($notification, $channels) use ($date) { ); } - public function testDefaultDownProcessesCallbackExceptionSingularMessage(): void + /** @test */ + public function it_can_send_default_down_processed_callback_exception_singular_message(): void { $date = Carbon::now()->subHours(4); $stuckJobs = collect([ @@ -276,7 +290,8 @@ public function testDefaultDownProcessesCallbackExceptionSingularMessage(): void $callback($stuckJobs); } - public function testDefaultDownProcessesCallbackExceptionPluralMessage(): void + /** @test */ + public function it_can_send_default_down_processed_callback_exception_plural_message(): void { $date = Carbon::now()->subHours(4); $stuckJobs = collect([ @@ -289,7 +304,8 @@ public function testDefaultDownProcessesCallbackExceptionPluralMessage(): void $callback($stuckJobs); } - public function testSimulationNotification(): void + /** @test */ + public function it_can_simulate_stuck_jobs_notification(): void { config()->set('stuck-jobs-notifier.callback', null); $this->artisan(SimulateStuckJobs::class); @@ -313,7 +329,8 @@ function ($notification, $channels) { ); } - public function testSimulationCallback(): void + /** @test */ + public function it_can_simulate_stuck_jobs_exception(): void { $this->expectExceptionMessage('Exception test: '); $this->artisan(SimulateStuckJobs::class); diff --git a/tests/database/migrations/2014_10_12_200000_create_failed_jobs_table.php b/tests/database/migrations/2014_10_12_200000_create_failed_jobs_table.php index d432dff..ee07e6a 100644 --- a/tests/database/migrations/2014_10_12_200000_create_failed_jobs_table.php +++ b/tests/database/migrations/2014_10_12_200000_create_failed_jobs_table.php @@ -1,19 +1,13 @@ bigIncrements('id'); $table->text('connection'); $table->text('queue'); @@ -23,13 +17,8 @@ public function up() }); } - /** - * Reverse the migrations. - * - * @return void - */ - public function down() + public function down(): void { Schema::dropIfExists('failed_jobs'); } -} +};