Skip to content

Commit d109565

Browse files
committed
[tests] Rework of errors for Args helper.
1 parent abb060c commit d109565

17 files changed

+214
-70
lines changed

packages/testing/src/Constraints/Json/JsonMatchesSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
5353
protected function matches($other): bool {
5454
$helper = null;
5555
$loader = $this->schema->getLoader();
56-
$schema = new Schema(Args::getJson($this->schema->getSchema()) ?? Args::invalidJson());
56+
$schema = new Schema(Args::getJson($this->schema->getSchema()));
5757
$this->result = (new Validator($helper, $loader))->schemaValidation($other, $schema);
5858
$matches = $this->result->isValid();
5959

packages/testing/src/Constraints/Json/JsonSchemaLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function loadSchema(string $uri): ISchema|null {
4848
$schema = null;
4949

5050
if ($file) {
51-
$schema = new Schema(Args::getJson($file) ?? Args::invalidJson());
51+
$schema = new Schema(Args::getJson($file));
5252
}
5353

5454
// Return

packages/testing/src/Constraints/Response/ContentTypeTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LastDragon_ru\LaraASP\Testing\Constraints\Response;
44

55
use GuzzleHttp\Psr7\Response;
6-
use InvalidArgumentException;
6+
use LastDragon_ru\LaraASP\Testing\Exceptions\InvalidArgumentResponse;
77
use PHPUnit\Framework\TestCase;
88
use stdClass;
99

@@ -16,9 +16,7 @@ class ContentTypeTest extends TestCase {
1616
* @covers ::evaluate
1717
*/
1818
public function testEvaluate(): void {
19-
$this->expectExceptionObject(new InvalidArgumentException(
20-
'It is not a `Psr\Http\Message\ResponseInterface` instance.',
21-
));
19+
$this->expectExceptionObject(new InvalidArgumentResponse('$response', new stdClass()));
2220

2321
$this->assertFalse((new ContentType(''))->evaluate(new stdClass()));
2422
}

packages/testing/src/Constraints/Response/HeaderTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LastDragon_ru\LaraASP\Testing\Constraints\Response;
44

55
use GuzzleHttp\Psr7\Response;
6-
use InvalidArgumentException;
6+
use LastDragon_ru\LaraASP\Testing\Exceptions\InvalidArgumentResponse;
77
use PHPUnit\Framework\Constraint\IsEqual;
88
use PHPUnit\Framework\TestCase;
99
use stdClass;
@@ -17,9 +17,7 @@ class HeaderTest extends TestCase {
1717
* @covers ::evaluate
1818
*/
1919
public function testEvaluate(): void {
20-
$this->expectExceptionObject(new InvalidArgumentException(
21-
'It is not a `Psr\Http\Message\ResponseInterface` instance.',
22-
));
20+
$this->expectExceptionObject(new InvalidArgumentResponse('$response', new stdClass()));
2321

2422
$this->assertFalse((new Header('Test'))->evaluate(new stdClass()));
2523
}

packages/testing/src/Constraints/Response/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getConstraints(): array {
4646
*/
4747
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool {
4848
return parent::evaluate(
49-
Args::getResponse($other) ?? Args::invalidResponse(),
49+
Args::getResponse($other),
5050
$description,
5151
$returnResult,
5252
);

packages/testing/src/Constraints/Response/StatusCodeTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace LastDragon_ru\LaraASP\Testing\Constraints\Response;
44

55
use GuzzleHttp\Psr7\Response;
6-
use InvalidArgumentException;
6+
use LastDragon_ru\LaraASP\Testing\Exceptions\InvalidArgumentResponse;
77
use PHPUnit\Framework\TestCase;
88
use stdClass;
99

@@ -16,9 +16,7 @@ class StatusCodeTest extends TestCase {
1616
* @covers ::evaluate
1717
*/
1818
public function testEvaluate(): void {
19-
$this->expectExceptionObject(new InvalidArgumentException(
20-
'It is not a `Psr\Http\Message\ResponseInterface` instance.',
21-
));
19+
$this->expectExceptionObject(new InvalidArgumentResponse('$response', new stdClass()));
2220

2321
$this->assertFalse((new StatusCode(200))->evaluate(new stdClass()));
2422
}

packages/testing/src/Constraints/Xml/XmlMatchesSchema.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class XmlMatchesSchema extends Constraint {
3030
protected array $errors = [];
3131

3232
public function __construct(SplFileInfo $schema) {
33-
$this->schema = Args::getFile($schema) ?? Args::invalidFile();
33+
$this->schema = Args::getFile($schema);
3434
}
3535

3636
// <editor-fold desc="\PHPUnit\Framework\Constraint\Constraint">
@@ -39,11 +39,13 @@ public function __construct(SplFileInfo $schema) {
3939
* @inheritdoc
4040
*/
4141
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool {
42-
return parent::evaluate(
43-
Args::getFile($other) ?? Args::getDomDocument($other) ?? Args::invalidXml(),
44-
$description,
45-
$returnResult,
46-
);
42+
if ($other instanceof SplFileInfo) {
43+
$other = Args::getFile($other);
44+
} else {
45+
$other = Args::getDomDocument($other);
46+
}
47+
48+
return parent::evaluate($other, $description, $returnResult);
4749
}
4850

4951
/**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Testing\Exceptions;
4+
5+
use InvalidArgumentException;
6+
use LastDragon_ru\LaraASP\Testing\PackageException;
7+
8+
use function gettype;
9+
use function is_object;
10+
11+
abstract class InvalidArgument extends InvalidArgumentException implements PackageException {
12+
protected function getType(mixed $value): string {
13+
return is_object($value) ? $value::class : gettype($value);
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Testing\Exceptions;
4+
5+
use Throwable;
6+
7+
use function sprintf;
8+
9+
class InvalidArgumentJson extends InvalidArgument {
10+
public function __construct(
11+
protected string $argument,
12+
protected mixed $value,
13+
Throwable $previous = null,
14+
) {
15+
parent::__construct(sprintf(
16+
'Argument `%1$s` must be a valid JSON.',
17+
$this->argument,
18+
), 0, $previous);
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Testing\Exceptions;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Throwable;
7+
8+
use function sprintf;
9+
10+
class InvalidArgumentResponse extends InvalidArgument {
11+
public function __construct(
12+
protected string $argument,
13+
protected mixed $value,
14+
Throwable $previous = null,
15+
) {
16+
parent::__construct(sprintf(
17+
'Argument `%1$s` must be instance of `%2$s`, `%3$s` given.',
18+
$this->argument,
19+
ResponseInterface::class,
20+
$this->getType($this->value),
21+
), 0, $previous);
22+
}
23+
}

0 commit comments

Comments
 (0)