Skip to content

Commit fab2036

Browse files
committed
add tests for exceptions
1 parent c305d0a commit fab2036

File tree

7 files changed

+142
-10
lines changed

7 files changed

+142
-10
lines changed

src/Exceptions/ConfigException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ConfigException extends Exception
2020
* @param null $config
2121
* @param string $message
2222
*/
23-
public function __construct($config = null, $message = 'Config Exception')
23+
public function __construct($message = 'Config Exception', $config = null)
2424
{
2525
parent::__construct($message);
2626

src/Exceptions/MissingPropertyException.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,28 @@ class MissingPropertyException extends ConfigException
1313
protected $property;
1414

1515
/**
16-
* MissingConfigException constructor.
16+
* MissingPropertyException constructor.
1717
*
18-
* @param mixed $config
19-
* @param string $property
18+
* @param null|string $message
19+
* @param null|string $property
2020
*/
21-
public function __construct($config = null, $property = null)
21+
public function __construct(?string $message = null, ?string $property = null)
2222
{
2323
$this->property = $property;
24-
parent::__construct($config, sprintf('Missing property "%s" in config', $this->property));
24+
parent::__construct($message ?? ($this->getName() . ($this->property ? ': "' . $this->property . '"' : '')));
2525
}
2626

27+
/**
28+
* @return string
29+
*/
30+
public function getName()
31+
{
32+
return 'Missing property';
33+
}
34+
35+
/**
36+
* @return null|string
37+
*/
2738
public function getProperty(): ?string
2839
{
2940
return $this->property;

src/Exceptions/UnknownMethodException.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ class UnknownMethodException extends \BadMethodCallException
1818
* @param null|string $method
1919
* @param null|string $message
2020
*/
21-
public function __construct(?string $method = null, ?string $message = null)
21+
public function __construct(?string $message = null, ?string $method = null)
2222
{
2323
$this->method = $method;
24-
parent::__construct($message ?? sprintf('Missing method "%s" ', $this->method));
24+
parent::__construct($message ?? ($this->getName() . ($this->method ? ': "'.$this->method.'"': '')));
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getName()
31+
{
32+
return 'Unknown method';
2533
}
2634

2735
/**

src/Exceptions/UnknownPropertyException.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ class UnknownPropertyException extends Exception
1818
* @param null|string $property
1919
* @param null|string $message
2020
*/
21-
public function __construct(?string $property = null, ?string $message = null)
21+
public function __construct(?string $message = null, ?string $property = null)
2222
{
2323
$this->property = $property;
24-
parent::__construct($message ?? sprintf('Missing property "%s" ', $this->property));
24+
parent::__construct($message ?? ($this->getName() . ($this->property ? ': "'.$this->property.'"': '')));
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getName()
31+
{
32+
return 'Unknown property';
2533
}
2634

2735
/**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Php\Support\Exceptions\MissingPropertyException;
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MissingPropertyTest extends TestCase
8+
{
9+
public function testThrow()
10+
{
11+
try {
12+
throw new MissingPropertyException('Invalid Arg');
13+
} catch (\Throwable $e) {
14+
$this->assertInstanceOf(MissingPropertyException::class, $e);
15+
$this->assertSame('Invalid Arg', $e->getMessage());
16+
}
17+
18+
try {
19+
throw new MissingPropertyException(null, 'test');
20+
} catch (\Throwable $e) {
21+
$this->assertInstanceOf(MissingPropertyException::class, $e);
22+
$this->assertSame('Missing property', $e->getName());
23+
$this->assertSame('Missing property: "test"', $e->getMessage());
24+
$this->assertSame('test', $e->getProperty());
25+
}
26+
27+
try {
28+
throw new MissingPropertyException();
29+
} catch (\Throwable $e) {
30+
$this->assertInstanceOf(MissingPropertyException::class, $e);
31+
$this->assertSame('Missing property', $e->getName());
32+
$this->assertSame('Missing property', $e->getMessage());
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Php\Support\Exceptions\UnknownMethodException;
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class UnknownMethodTest extends TestCase
8+
{
9+
public function testThrow()
10+
{
11+
try {
12+
throw new UnknownMethodException('Invalid Arg');
13+
} catch (\Throwable $e) {
14+
$this->assertInstanceOf(UnknownMethodException::class, $e);
15+
$this->assertSame('Invalid Arg', $e->getMessage());
16+
}
17+
18+
try {
19+
throw new UnknownMethodException(null, 'test');
20+
} catch (\Throwable $e) {
21+
$this->assertInstanceOf(UnknownMethodException::class, $e);
22+
$this->assertSame('Unknown method', $e->getName());
23+
$this->assertSame('Unknown method: "test"', $e->getMessage());
24+
$this->assertSame('test', $e->getMethod());
25+
}
26+
27+
try {
28+
throw new UnknownMethodException();
29+
} catch (\Throwable $e) {
30+
$this->assertInstanceOf(UnknownMethodException::class, $e);
31+
$this->assertSame('Unknown method', $e->getName());
32+
$this->assertSame('Unknown method', $e->getMessage());
33+
}
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Php\Support\Exceptions\UnknownPropertyException;
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class UnknownPropertyTest extends TestCase
8+
{
9+
public function testThrow()
10+
{
11+
try {
12+
throw new UnknownPropertyException('Invalid Arg');
13+
} catch (\Throwable $e) {
14+
$this->assertInstanceOf(UnknownPropertyException::class, $e);
15+
$this->assertSame('Invalid Arg', $e->getMessage());
16+
}
17+
18+
try {
19+
throw new UnknownPropertyException(null, 'test');
20+
} catch (\Throwable $e) {
21+
$this->assertInstanceOf(UnknownPropertyException::class, $e);
22+
$this->assertSame('Unknown property', $e->getName());
23+
$this->assertSame('Unknown property: "test"', $e->getMessage());
24+
$this->assertSame('test', $e->getProperty());
25+
}
26+
27+
try {
28+
throw new UnknownPropertyException();
29+
} catch (\Throwable $e) {
30+
$this->assertInstanceOf(UnknownPropertyException::class, $e);
31+
$this->assertSame('Unknown property', $e->getName());
32+
$this->assertSame('Unknown property', $e->getMessage());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)