-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ExtractException for behaviors
- Loading branch information
1 parent
1965007
commit 6911db5
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Horat1us\Yii\Exceptions\Behavior; | ||
|
||
use Throwable; | ||
use yii\base; | ||
|
||
/** | ||
* Class ExtractException | ||
* @package Horat1us\Yii\Exceptions\Behavior | ||
*/ | ||
class ExtractException extends base\InvalidConfigException | ||
{ | ||
/** @var base\Behavior */ | ||
protected $behavior; | ||
|
||
/** @var string */ | ||
protected $targetClass; | ||
|
||
public function __construct( | ||
base\Behavior $behavior, | ||
string $targetClass, | ||
int $code = 0, | ||
Throwable $previous = null | ||
) { | ||
$message = get_class($behavior) . " can be applied only to {$targetClass}"; | ||
parent::__construct($message, $code, $previous); | ||
|
||
$this->behavior = $behavior; | ||
$this->targetClass = $targetClass; | ||
} | ||
|
||
public function getBehavior(): base\Behavior | ||
{ | ||
return $this->behavior; | ||
} | ||
|
||
public function getTargetClass(): string | ||
{ | ||
return $this->targetClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Horat1us\Yii\Tests\Exceptions\Behavior; | ||
|
||
use Horat1us\Yii\Exceptions\Behavior\ExtractException; | ||
use Horat1us\Yii\Tests\AbstractTestCase; | ||
use yii\base; | ||
|
||
/** | ||
* Class ExtractExceptionTest | ||
* @package Horat1us\Yii\Tests\Exceptions\Behavior | ||
*/ | ||
class ExtractExceptionTest extends AbstractTestCase | ||
{ | ||
public function testException(): void | ||
{ | ||
$behavior = new base\Behavior(); | ||
$exception = new ExtractException($behavior, base\Model::class); | ||
$this->assertEquals($behavior, $exception->getBehavior()); | ||
$this->assertEquals(base\Model::class, $exception->getTargetClass()); | ||
$this->assertEquals( | ||
'yii\\base\\Behavior can be applied only to yii\\base\\Model', | ||
$exception->getMessage() | ||
); | ||
} | ||
} |