Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-385: Fixed error handling when composer files are corrupted #94

Merged
merged 10 commits into from
May 28, 2021
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
4 changes: 2 additions & 2 deletions Resources/translations/dashboard.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@
<note>key: dashboard.ez_version.non_stable_ee</note>
</trans-unit>
<trans-unit id="c1fb5ba9fd547e30a49a4e1895822e69cd4a9dbc" resname="dashboard.ez_version.release_not_determined">
<source><![CDATA[The system could not find your <code>composer.lock</code> file. It's needed to determine information about
<source><![CDATA[The system could not find your <code>composer.lock</code> or <code>composer.json</code> file, or the files are invalid. They are needed to determine information about
your eZ install, and recommended to be kept on project development to make sure same package versions are used across all environments.]]></source>
<target state="new"><![CDATA[The system could not find your <code>composer.lock</code> file. It's needed to determine information about
<target state="new"><![CDATA[The system could not find your <code>composer.lock</code> or <code>composer.json</code> file or it's not valid. It's needed to determine information about
your eZ install, and recommended to be kept on project development to make sure same package versions are used across all environments.]]></target>
<note>key: dashboard.ez_version.release_not_determined</note>
</trans-unit>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/themes/admin/dashboard/block/ez.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% set severity = 1 %}
<div class="alert alert-warning mb-0 mt-3" role="alert">
{{ 'dashboard.ez_version.release_not_determined'|trans
|desc("The system could not find your <code>composer.lock</code> file. It's needed to determine information about
|desc("The system could not find your <code>composer.lock</code> or <code>composer.json</code> file or it's not valid. It's needed to determine information about
your eZ installation. It is recommended to keep it during project development to make sure the same package versions are
used across all environments.")
|raw }}
Expand Down
3 changes: 2 additions & 1 deletion SystemInfo/Collector/EzSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Collector;

use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerFileValidationException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\EzSystemInfo;
use DateTime;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function __construct(SystemInfoCollector $composerCollector, $debug = fal
{
try {
$this->composerInfo = $composerCollector->collect();
} catch (ComposerLockFileNotFoundException $e) {
} catch (ComposerLockFileNotFoundException | ComposerFileValidationException $e) {
// do nothing
}
$this->debug = $debug;
Expand Down
9 changes: 9 additions & 0 deletions SystemInfo/Collector/JsonComposerLockSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct($lockFile, $jsonFile)
* Collects information about installed composer packages.
*
* @throws Exception\ComposerLockFileNotFoundException if the composer.lock file was not found.
* @throws Exception\ComposerFileValidationException if composer.lock of composer.json are not valid.
*
* @return Value\ComposerSystemInfo
*/
Expand All @@ -74,6 +75,14 @@ public function collect()
$lockData = json_decode(file_get_contents($this->lockFile), true);
$jsonData = json_decode(file_get_contents($this->jsonFile), true);

if (!is_array($lockData)) {
throw new Exception\ComposerFileValidationException($this->lockFile);
}

if (!is_array($jsonData)) {
throw new Exception\ComposerFileValidationException($this->jsonFile);
}

return $this->value = new Value\ComposerSystemInfo([
'packages' => $this->extractPackages($lockData),
'repositoryUrls' => $this->extractRepositoryUrls($jsonData),
Expand Down
19 changes: 19 additions & 0 deletions SystemInfo/Exception/ComposerFileValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Exception;

use Exception;

final class ComposerFileValidationException extends Exception implements SystemInfoException
{
public function __construct(string $path, $code = 0, Exception $previous = null)
{
$message = sprintf('Composer file %s is not valid.', $path);

parent::__construct($message, $code, $previous);
}
}
2 changes: 1 addition & 1 deletion SystemInfo/Exception/ComposerJsonFileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Exception;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as BaseNotFoundException;

class ComposerJsonFileNotFoundException extends BaseNotFoundException
class ComposerJsonFileNotFoundException extends BaseNotFoundException implements SystemInfoException
{
public function __construct(string $path, Exception $previous = null)
{
Expand Down
2 changes: 1 addition & 1 deletion SystemInfo/Exception/ComposerLockFileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Exception;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as BaseNotFoundException;

class ComposerLockFileNotFoundException extends BaseNotFoundException
class ComposerLockFileNotFoundException extends BaseNotFoundException implements SystemInfoException
{
public function __construct($path, Exception $previous = null)
{
Expand Down
11 changes: 11 additions & 0 deletions SystemInfo/Exception/SystemInfoException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Exception;

interface SystemInfoException
{
}
22 changes: 22 additions & 0 deletions SystemInfo/Value/InvalidSystemInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Value;

use eZ\Publish\API\Repository\Values\ValueObject;

/**
* Invalid value for system info used in case of any errors occur while collecting data.
*/
final class InvalidSystemInfo extends ValueObject implements SystemInfo
{
/**
* Error message shown in the System info tab.
*
* @var string
*/
public $errorMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace EzSystems\EzSupportToolsBundle\Tests\SystemInfo\Collector;

use EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector;
use EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerFileValidationException;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerPackage;
use EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo;
use PHPUnit\Framework\TestCase;
Expand All @@ -18,7 +19,7 @@ class JsonComposerLockSystemInfoCollectorTest extends TestCase
/**
* @covers \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::collect()
*/
public function testCollect()
public function testCollect(): void
{
$expected = new ComposerSystemInfo([
'packages' => [
Expand Down Expand Up @@ -80,7 +81,7 @@ public function testCollect()
*
* @expectedException \EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException
*/
public function testCollectLockFileNotFound()
public function testCollectLockFileNotFound(): void
{
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/snafu.lock', __DIR__ . '/_fixtures/composer.json');
$composerCollectorNotFound->collect();
Expand All @@ -91,9 +92,37 @@ public function testCollectLockFileNotFound()
*
* @expectedException \EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerJsonFileNotFoundException
*/
public function testCollectJsonFileNotFound()
public function testCollectJsonFileNotFound(): void
{
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/snafu.json');
$composerCollectorNotFound->collect();
}

/**
* @covers \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::collect()
*/
public function testCollectLockFileCorrupted(): void
{
$composerCollectorCorrupted = new JsonComposerLockSystemInfoCollector(
__DIR__ . '/_fixtures/corrupted_composer.lock',
__DIR__ . '/_fixtures/composer.json'
);

$this->expectException(ComposerFileValidationException::class);
$composerCollectorCorrupted->collect();
}

/**
* @covers \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::collect()
*/
public function testCollectJsonFileCorrupted(): void
{
$composerCollectorCorrupted = new JsonComposerLockSystemInfoCollector(
__DIR__ . '/_fixtures/composer.lock',
__DIR__ . '/_fixtures/corrupted_composer.json'
);

$this->expectException(ComposerFileValidationException::class);
$composerCollectorCorrupted->collect();
}
}
147 changes: 147 additions & 0 deletions Tests/SystemInfo/Collector/_fixtures/corrupted_composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"name": "ezsystems/ezplatform-ee",,,,,
"description": "eZ Platform Enterprise Edition distribution",
"homepage": "https://github.com/ezsystems/ezplatform-ee",
"license": "proprietary",
"type": "project",
"authors": [
{
"name": "eZ dev-team & eZ Community",
"homepage": "https://github.com/ezsystems/ezplatform-ee/contributors"
}
],
"repositories": [
{ "type": "composer", "url": "https://updates.ez.no/bul" }
],
"replace": {
"ezsystems/ezstudio": "*",
"ezsystems/ezpublish-community": "*"
},
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" },
"files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
},
"require": {
"php": "^7.1.3",
"doctrine/doctrine-bundle": "^1.9.1",
"doctrine/orm": "^2.6.3",
"ezsystems/date-based-publisher": "^3.2",
"ezsystems/doctrine-dbal-schema": "^0.1",
"ezsystems/ez-support-tools": "^1.0",
"ezsystems/ezplatform-admin-ui": "^1.5",
"ezsystems/ezplatform-admin-ui-assets": "^4.1.0",
"ezsystems/ezplatform-admin-ui-modules": "^1.5",
"ezsystems/ezplatform-core": "^1.0",
"ezsystems/ezplatform-cron": "^2.0",
"ezsystems/ezplatform-design-engine": "^2.0",
"ezsystems/ezplatform-ee-installer": "^2.5",
"ezsystems/ezplatform-form-builder": "^1.2",
"ezsystems/ezplatform-graphql": "^1.0",
"ezsystems/ezplatform-http-cache": "^0.9",
"ezsystems/ezplatform-http-cache-fastly": "^1.1",
"ezsystems/ezplatform-matrix-fieldtype": "^1.0",
"ezsystems/ezplatform-page-builder": "^1.3",
"ezsystems/ezplatform-page-fieldtype": "^1.3",
"ezsystems/ezplatform-richtext": "^1.1",
"ezsystems/ezplatform-solr-search-engine": "^1.6",
"ezsystems/ezplatform-standard-design": "^0.2",
"ezsystems/ezplatform-user": "^1.0",
"ezsystems/ezplatform-workflow": "^1.1",
"ezsystems/ezpublish-kernel": "^7.5",
"ezsystems/flex-workflow": "^3.2",
"ezsystems/repository-forms": "^2.5",
"ezsystems/symfony-tools": "~1.0.0",
"friendsofsymfony/jsrouting-bundle": "^1.6.3",
"gregwar/captcha-bundle": "^2.0",
"incenteev/composer-parameter-handler": "^2.1.3",
"knplabs/knp-menu-bundle": "^2.2.1",
"scssphp/scssphp": "^1.0",
"overblog/graphql-bundle": "^0.11.11",
"sensio/distribution-bundle": "^5.0.23",
"sensiolabs/security-checker": "^5.0",
"symfony/assetic-bundle": "^2.8.2",
"symfony/monolog-bundle": "^3.3.1",
"symfony/swiftmailer-bundle": "^3.2.4",
"symfony/symfony": "^3.4.18",
"symfony/thanks": "^1.1.0",
"symfony/webpack-encore-bundle": "^1.0.0",
"twig/extensions": "^1.5.3",
"twig/twig": "^2.5",
"white-october/pagerfanta-bundle": "^1.2.2",
"willdurand/js-translation-bundle": "^2.6.6"
},
"require-dev": {
"behat/behat": "^3.5.0",
"behat/mink-extension": "^2.3.1",
"behat/mink-goutte-driver": "^1.2.1",
"behat/mink-selenium2-driver": "^1.3.1",
"behat/symfony2-extension": "^2.1.5",
"bex/behat-screenshot": "^1.2.7",
"ezsystems/behat-screenshot-image-driver-cloudinary": "^1.1.0",
"ezsystems/behatbundle": "^6.5.4",
"overblog/graphiql-bundle": "^0.1.2",
"phpunit/phpunit": "^6.5.13",
"sensio/generator-bundle": "^3.1.7",
"symfony/phpunit-bridge": "^3.4.18",
"liuggio/fastest": "^1.6"
},
"conflict": {
"symfony/symfony": "3.4.9||3.4.12||3.4.16",
"doctrine/dbal": "2.7.0",
"twig/twig": "2.6.1",
"symfony/webpack-encore-bundle": "1.2.0||1.2.1"
},
"scripts": {
"symfony-scripts": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"eZ\\Bundle\\EzPublishCoreBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"@php bin/console bazinga:js-translation:dump web/assets --merge-domains",
"@php bin/console assetic:dump",
"yarn install",
"EzSystems\\EzPlatformEncoreBundle\\Composer\\ScriptHandler::compileAssets",
"@php bin/security-checker security:check"
],
"post-install-cmd": [
"@symfony-scripts"
],
"post-update-cmd": [
"@symfony-scripts"
],
"post-create-project-cmd": [
"eZ\\Bundle\\EzPublishCoreBundle\\Composer\\ScriptHandler::installWelcomeText"
],
"ezplatform-install": [
"@php bin/console ezplatform:install ezplatform-ee-clean"
]
},
"config": {
"bin-dir": "bin",
"sort-packages": true,
"preferred-install": {
"ezsystems/*": "dist"
}
},
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin",
"symfony-var-dir": "var",
"symfony-web-dir": "web",
"symfony-tests-dir": "tests",
"symfony-assets-install": "relative",
"incenteev-parameters": {
"keep-outdated": true,
"file": "app/config/parameters.yml"
},
"branch-alias": {
"dev-master": "2.5.x-dev"
}
}
}
Loading