Skip to content

Commit

Permalink
test: Introduce an UnparsableSpec exception (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Apr 14, 2024
1 parent e15b7e2 commit 2deb977
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
22 changes: 3 additions & 19 deletions tests/Scoper/PhpScoperSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

namespace Humbug\PhpScoper\Scoper;

use Error;
use Humbug\PhpScoper\Configuration\SymbolsConfiguration;
use Humbug\PhpScoper\Container;
use Humbug\PhpScoper\PhpParser\TraverserFactory;
use Humbug\PhpScoper\Scoper\Spec\SpecFinder;
use Humbug\PhpScoper\Scoper\Spec\SpecFormatter;
use Humbug\PhpScoper\Scoper\Spec\SpecNormalizer;
use Humbug\PhpScoper\Scoper\Spec\SpecParser;
use Humbug\PhpScoper\Scoper\Spec\UnparsableSpec;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use Humbug\PhpScoper\Symbol\Reflector;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
Expand Down Expand Up @@ -109,15 +109,7 @@ public function test_can_scope_valid_files(
$error,
);
} catch (Throwable $throwable) {
throw new Error(
sprintf(
'Could not parse the spec %s: %s',
$spec,
$throwable->getMessage().$throwable->getTraceAsString(),
),
0,
$throwable,
);
throw UnparsableSpec::create($spec, $throwable);
}

$specMessage = SpecFormatter::createSpecMessage(
Expand Down Expand Up @@ -204,15 +196,7 @@ private static function handlePhpParserError(
PhpParserError $error,
): never {
if (!str_starts_with($error->getMessage(), 'Syntax error,')) {
throw new Error(
sprintf(
'Could not parse the spec %s: %s',
$spec,
$error->getMessage(),
),
0,
$error,
);
throw UnparsableSpec::create($spec, $error);
}

$lines = array_values(array_filter(explode("\n", $contents)));
Expand Down
36 changes: 36 additions & 0 deletions tests/Scoper/Spec/UnparsableSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Humbug\PhpScoper\Scoper\Spec;

use DomainException;
use Throwable;
use function sprintf;

final class UnparsableSpec extends DomainException
{
public static function create(
string $specTitle,
Throwable $throwable,
): self {
return new self(
sprintf(
'Could not parse the spec "%s": %s',
$specTitle,
$throwable->getMessage(),
),
previous: $throwable,
);
}
}

0 comments on commit 2deb977

Please sign in to comment.