Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/N98/Util/ProjectComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public function isComposerJsonFile()
*/
public function getComposerLockPackages()
{
$lockFilePath = $this->getComposerLockPath();
if (!file_exists($lockFilePath)) {
return [];
}

try {
$composerLockContent = json_decode(
file_get_contents($this->getComposerLockPath()),
file_get_contents($lockFilePath),
true,
512,
JSON_THROW_ON_ERROR
Expand Down
40 changes: 40 additions & 0 deletions tests/N98/Util/ProjectComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace N98\Util;

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

class ProjectComposerTest extends TestCase
Expand Down Expand Up @@ -62,4 +63,43 @@ public function itShouldReturnEmptyArrayIfLockFileIsMalformed()
$this->assertIsArray($returnedPackages);
$this->assertEmpty($returnedPackages);
}

/**
* @test
*/
public function itShouldReturnEmptyArrayIfLockFileDoesNotExist()
{
vfsStream::setup('root');
$projectComposer = new ProjectComposer(vfsStream::url('root'));
$returnedPackages = $projectComposer->getComposerLockPackages();

$this->assertIsArray($returnedPackages);
$this->assertEmpty($returnedPackages);
}

/**
* @test
*/
public function itShouldReturnEmptyArrayIfLockFileContainsInvalidJson()
{
vfsStream::setup('root', null, ['composer.lock' => '{invalid}']);
$projectComposer = new ProjectComposer(vfsStream::url('root'));
$returnedPackages = $projectComposer->getComposerLockPackages();

$this->assertIsArray($returnedPackages);
$this->assertEmpty($returnedPackages);
}

/**
* @test
*/
public function itShouldHandleMissingPackagesKeys()
{
vfsStream::setup('root', null, ['composer.lock' => '{}']);
$projectComposer = new ProjectComposer(vfsStream::url('root'));
$returnedPackages = $projectComposer->getComposerLockPackages();

$this->assertIsArray($returnedPackages);
$this->assertEmpty($returnedPackages);
}
}