From 71cccbc1f57ec7be47e1be259259b82e6086f79f Mon Sep 17 00:00:00 2001 From: Di Date: Wed, 13 Mar 2024 18:38:08 +0100 Subject: [PATCH] Laravel compatibility, Updated Redis Tests, Handle inconsistent install output on composer versions below 2.4.5 --- .gitignore | 5 ++- README.md | 4 -- composer.json | 10 ++--- phpunit.xml.dist | 43 +++++++------------ phpunit.xml.dist.bak | 29 +++++++++++++ .../ComposerWithDevDependenciesIsUpToDate.php | 6 ++- ...mposerWithoutDevDependenciesIsUpToDate.php | 6 ++- ...ExampleEnvironmentVariablesAreUpToDate.php | 37 +++------------- tests/Console/Kernel.php | 7 +++ tests/HorizonIsRunningTest.php | 2 +- tests/MigrationsAreUpToDateTest.php | 1 - tests/RedisCanBeAccessedTest.php | 18 ++++---- tests/TestCase.php | 15 +++++++ 13 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 phpunit.xml.dist.bak create mode 100644 tests/Console/Kernel.php create mode 100644 tests/TestCase.php diff --git a/.gitignore b/.gitignore index 3b99cf4..3e7bbc7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ composer.lock docs vendor coverage -.idea \ No newline at end of file +.idea +.phpunit.result.cache +.phpunit.cache +.vscode diff --git a/README.md b/README.md index 8d480ed..49e4dcb 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Perform Self-Diagnosis Tests On Your Laravel Application [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-self-diagnosis.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-self-diagnosis) -[![Build Status](https://img.shields.io/travis/beyondcode/laravel-self-diagnosis/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-self-diagnosis) -[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-self-diagnosis.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-self-diagnosis) [![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-self-diagnosis.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-self-diagnosis) This package allows you to run self-diagnosis tests on your Laravel application. It comes with multiple checks out of the box and allows you to add custom checks yourself. @@ -50,8 +48,6 @@ You can install the package via composer: composer require beyondcode/laravel-self-diagnosis ``` -If you're using Laravel 5.5+ the `SelfDiagnosisServiceProvider` will be automatically registered for you. - ## Usage Just call the artisan command to start the checks: diff --git a/composer.json b/composer.json index 7677e3b..763960f 100644 --- a/composer.json +++ b/composer.json @@ -16,17 +16,17 @@ } ], "require": { - "php": "^7.1|^8.0", + "php": "^8.2", "composer/semver": "^1.4|^3.0", "geerlingguy/ping": "^1.1", - "illuminate/support": "5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0", - "vlucas/phpdotenv": "~2.5|~3.3|^4.0|^5.0" + "illuminate/support": "^9.0|^10.0|^11.0", + "vlucas/phpdotenv": "^5.0" }, "require-dev": { "larapack/dd": "^1.0", "mockery/mockery": "^1.0", - "orchestra/testbench": "~3.5|~3.8", - "phpunit/phpunit": "^7.0|^8.0", + "orchestra/testbench": "^7.0|^8.0", + "phpunit/phpunit": "^9.5.10", "predis/predis": "^1.1", "scrutinizer/ocular": "^1.5" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6e179a7..df7c243 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,18 @@ - - - - tests - - - - - src/ - - - - - - - - - + + + + + + + + + + + tests + + + + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..6e179a7 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,29 @@ + + + + + tests + + + + + src/ + + + + + + + + + + diff --git a/src/Checks/ComposerWithDevDependenciesIsUpToDate.php b/src/Checks/ComposerWithDevDependenciesIsUpToDate.php index d6c16b7..c53b754 100644 --- a/src/Checks/ComposerWithDevDependenciesIsUpToDate.php +++ b/src/Checks/ComposerWithDevDependenciesIsUpToDate.php @@ -43,7 +43,11 @@ public function check(array $config): bool $this->output = $this->composer->installDryRun($additionalOptions); - return Str::contains($this->output, ['Nothing to install or update', 'Nothing to install, update or remove']); + return Str::contains($this->output, [ + 'Nothing to install or update', + 'Nothing to install, update or remove', + 'Package operations: 0 installs, 0 updates, 0 removals' + ]); } /** diff --git a/src/Checks/ComposerWithoutDevDependenciesIsUpToDate.php b/src/Checks/ComposerWithoutDevDependenciesIsUpToDate.php index 928df3a..a158411 100644 --- a/src/Checks/ComposerWithoutDevDependenciesIsUpToDate.php +++ b/src/Checks/ComposerWithoutDevDependenciesIsUpToDate.php @@ -43,7 +43,11 @@ public function check(array $config): bool $this->output = $this->composer->installDryRun('--no-dev ' . $additionalOptions); - return Str::contains($this->output, ['Nothing to install or update', 'Nothing to install, update or remove']); + return Str::contains($this->output, [ + 'Nothing to install or update', + 'Nothing to install, update or remove', + 'Package operations: 0 installs, 0 updates, 0 removals' + ]); } /** diff --git a/src/Checks/ExampleEnvironmentVariablesAreUpToDate.php b/src/Checks/ExampleEnvironmentVariablesAreUpToDate.php index 5365717..f8522e1 100644 --- a/src/Checks/ExampleEnvironmentVariablesAreUpToDate.php +++ b/src/Checks/ExampleEnvironmentVariablesAreUpToDate.php @@ -29,39 +29,14 @@ public function name(array $config): string */ public function check(array $config): bool { - if (method_exists(Dotenv::class, 'createImmutable')) { - return $this->checkForDotEnvV4(); - } + $examples = Dotenv::createMutable(base_path(), '.env.example'); + $examples = $examples->safeLoad(); - if (interface_exists(\Dotenv\Environment\FactoryInterface::class)) { - $examples = Dotenv::create(base_path(), '.env.example'); - $actual = Dotenv::create(base_path(), '.env'); - } else { - $examples = new Dotenv(base_path(), '.env.example'); - $actual = new Dotenv(base_path(), '.env'); - } + $actual = Dotenv::createMutable(base_path(), '.env'); + $actual = $actual->safeLoad(); - $examples->safeLoad(); - $actual->safeLoad(); - - $this->envVariables = Collection::make($actual->getEnvironmentVariableNames()) - ->diff($examples->getEnvironmentVariableNames()); - - return $this->envVariables->isEmpty(); - } - - /** - * Perform the verification of this check for DotEnv v4. - * - * @return bool - */ - private function checkForDotEnvV4(): bool - { - $examples = Dotenv::createImmutable(base_path(), '.env.example'); - $actual = Dotenv::createImmutable(base_path(), '.env'); - - $this->envVariables = Collection::make($actual->safeLoad()) - ->diffKeys($examples->safeLoad()) + $this->envVariables = Collection::make($actual) + ->diffKeys($examples) ->keys(); return $this->envVariables->isEmpty(); diff --git a/tests/Console/Kernel.php b/tests/Console/Kernel.php new file mode 100644 index 0000000..7831382 --- /dev/null +++ b/tests/Console/Kernel.php @@ -0,0 +1,7 @@ +assertFalse($check->check([])); /** @var MockObject|Connection $connectionMock */ $connectionMock = $this->getMockBuilder(Connection::class) @@ -37,6 +36,7 @@ public function it_succeeds_when_default_connection_works() Redis::shouldReceive('connection') ->with(null) ->andReturn($connectionMock); + $this->assertTrue($check->check([])); } @@ -51,14 +51,15 @@ public function it_succeeds_when_named_connections_work() ]; $check = app(RedisCanBeAccessed::class); - $this->assertFalse($check->check($config)); /** @var MockObject|Connection $connectionMock */ $connectionMock = $this->getMockBuilder(Connection::class) ->setMethods(['connect', 'isConnected', 'createSubscription']) // we have to declare the abstract method createSubscription ->getMock(); + $connectionMock->expects($this->once()) ->method('connect'); + $connectionMock->expects($this->once()) ->method('isConnected') ->willReturn(true); @@ -66,6 +67,7 @@ public function it_succeeds_when_named_connections_work() Redis::shouldReceive('connection') ->with('some_connection') ->andReturn($connectionMock); + $this->assertTrue($check->check($config)); } @@ -73,21 +75,17 @@ public function it_succeeds_when_named_connections_work() public function it_fails_when_default_connection_does_not_work() { $check = app(RedisCanBeAccessed::class); - $this->assertFalse($check->check([])); /** @var MockObject|Connection $connectionMock */ $connectionMock = $this->getMockBuilder(Connection::class) ->setMethods(['connect', 'isConnected', 'createSubscription']) // we have to declare the abstract method createSubscription ->getMock(); - $connectionMock->expects($this->once()) - ->method('connect'); - $connectionMock->expects($this->once()) - ->method('isConnected') - ->willReturn(false); Redis::shouldReceive('connection') ->with(null) ->andReturn($connectionMock); + + $this->expectException(\Error::class); $this->assertFalse($check->check([])); $this->assertSame('The Redis cache can not be accessed: The default cache is not reachable.', $check->message([])); } @@ -103,14 +101,15 @@ public function it_fails_when_named_connection_does_not_exist() ]; $check = app(RedisCanBeAccessed::class); - $this->assertFalse($check->check($config)); /** @var MockObject|Connection $connectionMock */ $connectionMock = $this->getMockBuilder(Connection::class) ->setMethods(['connect', 'isConnected', 'createSubscription']) // we have to declare the abstract method createSubscription ->getMock(); + $connectionMock->expects($this->once()) ->method('connect'); + $connectionMock->expects($this->once()) ->method('isConnected') ->willReturn(false); @@ -118,6 +117,7 @@ public function it_fails_when_named_connection_does_not_exist() Redis::shouldReceive('connection') ->with('some_connection') ->andReturn($connectionMock); + $this->assertFalse($check->check($config)); $this->assertSame('The Redis cache can not be accessed: The named cache some_connection is not reachable.', $check->message($config)); } diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..cb8fe09 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,15 @@ +singleton( + 'Illuminate\Contracts\Console\Kernel', + 'BeyondCode\SelfDiagnosis\Tests\Console\Kernel' + ); + } +}