Skip to content

Commit ec45a8e

Browse files
committed
Add subject to InvalidException
1 parent 7d23bca commit ec45a8e

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

src/InvalidException.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,26 @@
1111
*/
1212
class InvalidException extends LogicException
1313
{
14+
/**
15+
* @var array|object
16+
*/
17+
private $subject;
18+
19+
/**
20+
* @param array|object $subject
21+
*/
22+
public function setSubject($subject)
23+
{
24+
$this->subject = $subject;
25+
26+
return $this;
27+
}
28+
29+
/**
30+
* @return array|object
31+
*/
32+
public function getSubject()
33+
{
34+
return $this->subject;
35+
}
1436
}

src/ValidateTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public function validate()
5454
public function assertValid()
5555
{
5656
if (! $this->validate()) {
57-
throw new InvalidException(sprintf('Has errors: %s', $this->getErrors()));
57+
$exception = new InvalidException(sprintf('Has errors: %s', $this->getErrors()));
58+
$exception->setSubject($this);
59+
60+
throw $exception;
5861
}
5962

6063
return $this;

tests/src/InvalidExceptionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Harp\Validate\Test;
4+
5+
use Harp\Validate\InvalidException;
6+
use Exception;
7+
8+
/**
9+
* @coversDefaultClass Harp\Validate\InvalidException
10+
*/
11+
class InvalidExceptionTest extends AbstractTestCase
12+
{
13+
/**
14+
* @covers ::setSubject
15+
* @covers ::getSubject
16+
*/
17+
public function testSubject()
18+
{
19+
$previous = new Exception();
20+
$model = new Model();
21+
22+
$exception = new InvalidException('message', 123, $previous);
23+
$exception->setSubject($model);
24+
25+
$this->assertSame($previous, $exception->getPrevious());
26+
$this->assertSame(123, $exception->getCode());
27+
$this->assertSame($model, $exception->getSubject());
28+
}
29+
}

tests/src/ValidateTraitTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Harp\Validate\Test;
44

55
use Harp\Validate\Error;
6+
use Harp\Validate\InvalidException;
67

78
/**
8-
* @group asserts
99
* @coversDefaultClass Harp\Validate\ValidateTrait
1010
*/
1111
class ValidateTraitTest extends AbstractTestCase
@@ -54,8 +54,12 @@ public function testAssertValid()
5454

5555
$model->test = null;
5656

57-
$this->setExpectedException('Harp\Validate\InvalidException', 'Has errors: test must be present');
58-
59-
$model->assertValid();
57+
try {
58+
$model->assertValid();
59+
$this->fail('Should Throw an exception');
60+
} catch (InvalidException $exception) {
61+
$this->assertEquals('Has errors: test must be present', $exception->getMessage());
62+
$this->assertSame($model, $exception->getSubject());
63+
}
6064
}
6165
}

0 commit comments

Comments
 (0)