From 78102c4bdcfc568119f024c7b739403a3f11eb98 Mon Sep 17 00:00:00 2001 From: Kartvaik Date: Thu, 14 Feb 2019 19:07:47 +0200 Subject: [PATCH] Full test for Controller --- tests/Unit/Web/ControllerTest.php | 90 ++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Web/ControllerTest.php b/tests/Unit/Web/ControllerTest.php index f0c794e..4dcbf32 100644 --- a/tests/Unit/Web/ControllerTest.php +++ b/tests/Unit/Web/ControllerTest.php @@ -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; @@ -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']); + } }