Skip to content

Commit

Permalink
Full test for Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
KartaviK authored and Horat1us committed Feb 14, 2019
1 parent 7604fb2 commit 78102c4
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions tests/Unit/Web/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Horat1us\Yii\Monitoring\Tests\Unit\Web;

use Horat1us\Yii\Monitoring\Web\Controller;
use Horat1us\Yii\Monitoring;
use PHPUnit\Framework\TestCase;
use yii\base\Module;

Expand All @@ -19,7 +19,93 @@ class ControllerTest extends TestCase
*/
public function testFailedFindControl(): void
{
$controller = new Controller('id', $this->createMock(Module::class));
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$controller->actionControl(1);
}

/**
* @expectedException \yii\web\HttpException
* @expectedExceptionMessage Control queue has invalid configuration
* @expectedExceptionCode -1
*/
public function testInvalidControlConfiguration(): void
{
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$controller->controls = [
'queue' => 'invalidControl'
];
$controller->actionControl('queue');
}

public function testFailedActionControl(): void
{
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$queue = $this->createMock(Monitoring\Control\Queue::class);
$queue->expects($this->once())
->method('execute')
->willThrowException(new \RuntimeException('Some exception'));
$controller->controls = [
'queue' => $queue
];
$view = $controller->actionControl('queue');

$this->assertEquals(Monitoring\Web\View::STATE_ERROR, $view['state']);
}

public function testSuccessActionControl(): void
{
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$queue = $this->createMock(Monitoring\Control\Queue::class);
$queue->expects($this->once())
->method('execute')
->willReturn(['test' => 'test']);
$controller->controls = [
'queue' => $queue
];
$view = $controller->actionControl('queue');

$this->assertEquals(Monitoring\Web\View::STATE_OK, $view['state']);
}

public function testActionFullWithErroredControl(): void
{
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$queue = $this->createMock(Monitoring\Control\Queue::class);
$queue->expects($this->once())
->method('execute')
->willReturn(['test' => 'test']);
$cache = $this->createMock(Monitoring\Control\Cache::class);
$cache->expects($this->once())
->method('execute')
->willThrowException(new \RuntimeException('Some exception'));
$controller->controls = [
'queue' => $queue,
'cache' => $cache,
];
$result = $controller->actionFull();

$this->assertEquals(Monitoring\Web\View::STATE_ERROR, $result['state']);
$this->assertArrayHasKey('ms', $result);
$this->assertEquals(Monitoring\Exception::class, $result['error']['type']);
}

public function testSuccessActionFull(): void
{
$controller = new Monitoring\Web\Controller('id', $this->createMock(Module::class));
$queue = $this->createMock(Monitoring\Control\Queue::class);
$queue->expects($this->once())
->method('execute')
->willReturn(['test' => 'test']);
$cache = $this->createMock(Monitoring\Control\Cache::class);
$cache->expects($this->once())
->method('execute')
->willReturn(['test' => 'test']);
$controller->controls = [
'queue' => $queue,
'cache' => $cache,
];
$result = $controller->actionFull();

$this->assertEquals(Monitoring\Web\View::STATE_OK, $result['state']);
}
}

0 comments on commit 78102c4

Please sign in to comment.