-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from Jibbarth/feature/new-composer-insights
Add ComposerMustBeValid and ComposerLockMustBeFresh insights
- Loading branch information
Showing
19 changed files
with
686 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
.idea/* | ||
.env | ||
.phpunit.result.cache | ||
composer.lock | ||
/composer.lock | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\PhpInsights\Domain; | ||
|
||
use Composer\Composer; | ||
use Composer\Factory; | ||
use Composer\IO\NullIO; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ComposerLoader | ||
{ | ||
public static function getInstance(Collector $collector): Composer | ||
{ | ||
$io = new NullIO(); | ||
return Factory::create($io, ComposerFinder::getPath($collector)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\PhpInsights\Domain\Insights\Composer; | ||
|
||
use NunoMaduro\PhpInsights\Domain\ComposerLoader; | ||
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails; | ||
use NunoMaduro\PhpInsights\Domain\Exceptions\ComposerNotFound; | ||
use NunoMaduro\PhpInsights\Domain\Insights\Insight; | ||
|
||
final class ComposerLockMustBeFresh extends Insight implements HasDetails | ||
{ | ||
public function hasIssue(): bool | ||
{ | ||
try { | ||
$composer = ComposerLoader::getInstance($this->collector); | ||
|
||
return ! $composer->getLocker()->isFresh(); | ||
} catch (ComposerNotFound $exception) { | ||
return true; | ||
} | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return 'The lock file is not up to date with the latest changes in composer.json'; | ||
} | ||
|
||
public function getDetails(): array | ||
{ | ||
return ['You may be getting outdated dependencies. Run update to update them.']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\PhpInsights\Domain\Insights\Composer; | ||
|
||
use Composer\IO\NullIO; | ||
use Composer\Util\ConfigValidator; | ||
use NunoMaduro\PhpInsights\Domain\ComposerFinder; | ||
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails; | ||
use NunoMaduro\PhpInsights\Domain\Insights\Insight; | ||
|
||
final class ComposerMustBeValid extends Insight implements HasDetails | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private $errors; | ||
/** | ||
* @var array<string> | ||
*/ | ||
private $publishErrors; | ||
/** | ||
* @var array<string> | ||
*/ | ||
private $warnings; | ||
|
||
public function hasIssue(): bool | ||
{ | ||
$validator = new ConfigValidator(new NullIO()); | ||
[$this->errors, $this->publishErrors, $this->warnings] = $validator->validate(ComposerFinder::getPath($this->collector)); | ||
|
||
return \count(\array_merge($this->errors, $this->publishErrors, $this->warnings)) > 0; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return 'Composer.json is not valid'; | ||
} | ||
|
||
public function getDetails(): array | ||
{ | ||
$details = []; | ||
|
||
foreach (array_merge($this->errors, $this->publishErrors, $this->warnings) as $issue) { | ||
if (strpos($issue, ' : ') !== false) { | ||
$issue = explode(' : ', $issue)[1]; | ||
} | ||
$details[] = 'composer.json: ' . $issue; | ||
} | ||
|
||
return $details; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Domain; | ||
|
||
use NunoMaduro\PhpInsights\Domain\Collector; | ||
use NunoMaduro\PhpInsights\Domain\ComposerFinder; | ||
use NunoMaduro\PhpInsights\Domain\Exceptions\ComposerNotFound; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ComposerFinderTest extends TestCase | ||
{ | ||
public function testGetComposerPath(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Insights/Composer/Fixtures/Valid'); | ||
$path = ComposerFinder::getPath($collector); | ||
|
||
self::assertEquals( | ||
__DIR__ . '/Insights/Composer/Fixtures/Valid/composer.json', | ||
$path | ||
); | ||
} | ||
|
||
public function testGetComposerPathThrowExceptionIfNoComposerJsonExist(): void | ||
{ | ||
self::expectException(ComposerNotFound::class); | ||
|
||
$collector = new Collector(__DIR__ . '/Insights/Composer/Fixtures'); | ||
ComposerFinder::getPath($collector); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Domain; | ||
|
||
use Composer\Composer; | ||
use NunoMaduro\PhpInsights\Domain\Collector; | ||
use NunoMaduro\PhpInsights\Domain\ComposerLoader; | ||
use NunoMaduro\PhpInsights\Domain\Exceptions\ComposerNotFound; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ComposerLoaderTest extends TestCase | ||
{ | ||
public function testGetInstance(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Insights/Composer/Fixtures/Valid'); | ||
$composer = ComposerLoader::getInstance($collector); | ||
|
||
self::assertEquals(Composer::class, get_class($composer)); | ||
} | ||
|
||
public function testGetExceptionOnFolderWithoutComposerJson(): void | ||
{ | ||
self::expectException(ComposerNotFound::class); | ||
|
||
$collector = new Collector(__DIR__ . '/Insights/Composer/Fixtures'); | ||
ComposerLoader::getInstance($collector); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Domain/Insights/Composer/ComposerLockMustBeFreshTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Domain\Insights\Composer; | ||
|
||
use NunoMaduro\PhpInsights\Domain\Collector; | ||
use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerLockMustBeFresh; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ComposerLockMustBeFreshTest extends TestCase | ||
{ | ||
public function testHasIssueWhenComposerLockWasntUpdateFromComposerJson(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Fixtures/Unfresh'); | ||
$insight = new ComposerLockMustBeFresh($collector, []); | ||
|
||
self::assertTrue($insight->hasIssue()); | ||
} | ||
|
||
public function testHasNoIssueWhenComposerLockUpToDate(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Fixtures/Fresh'); | ||
$insight = new ComposerLockMustBeFresh($collector, []); | ||
|
||
self::assertFalse($insight->hasIssue()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Domain/Insights/Composer/ComposerMustBeValidTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Domain\Insights\Composer; | ||
|
||
use NunoMaduro\PhpInsights\Domain\Collector; | ||
use NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerMustBeValid; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ComposerMustBeValidTest extends TestCase | ||
{ | ||
public function testComposerIsNotValid(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Fixtures/Fresh'); | ||
$insight = new ComposerMustBeValid($collector, []); | ||
|
||
self::assertTrue($insight->hasIssue()); | ||
self::assertIsArray($insight->getDetails()); | ||
self::assertContains('composer.json: The property name is required', $insight->getDetails()); | ||
self::assertContains('composer.json: The property description is required', $insight->getDetails()); | ||
self::assertContains('composer.json: No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.', $insight->getDetails()); | ||
} | ||
|
||
public function testComposerIsValid(): void | ||
{ | ||
$collector = new Collector(__DIR__ . '/Fixtures/Valid'); | ||
$insight = new ComposerMustBeValid($collector, []); | ||
|
||
self::assertFalse($insight->hasIssue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"symfony/var-dumper": "^4.3" | ||
} | ||
} |
Oops, something went wrong.