Skip to content

Commit

Permalink
Create ExtractException for behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
sashabeton authored and Horat1us committed Oct 26, 2018
1 parent 1965007 commit 6911db5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Exceptions/Behavior/ExtractException.php
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;
}
}
26 changes: 26 additions & 0 deletions tests/Exceptions/Behavior/ExtractExceptionTest.php
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()
);
}
}

0 comments on commit 6911db5

Please sign in to comment.