Skip to content

Commit

Permalink
create testcase, use stub and mock
Browse files Browse the repository at this point in the history
  • Loading branch information
littlebookboy committed Jul 25, 2021
1 parent 80657d6 commit 2a42dac
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/Interfaces/INotify.php
@@ -0,0 +1,10 @@
<?php


namespace App\Interfaces;


interface INotify
{
public function notify(\Exception $exception): string;
}
15 changes: 13 additions & 2 deletions app/Listeners/WriteSumToLog.php
Expand Up @@ -4,21 +4,28 @@

use App\Events\StringWasAdded;
use App\Interfaces\ILoggerService;
use App\Interfaces\INotify;

class WriteSumToLog
{
/**
* @var ILoggerService
*/
private $loggerService;
/**
* @var INotify
*/
private $notifyService;

/**
* WriteSumToLog constructor.
* @param ILoggerService $loggerService
* @param INotify $notifyService
*/
public function __construct(ILoggerService $loggerService)
public function __construct(ILoggerService $loggerService, INotify $notifyService)
{
$this->loggerService = $loggerService;
$this->notifyService = $notifyService;
}

/**
Expand All @@ -29,6 +36,10 @@ public function __construct(ILoggerService $loggerService)
*/
public function handle(StringWasAdded $event)
{
$this->loggerService->write($event->sum);
try {
$this->loggerService->write($event->sum);
} catch (\Exception $exception) {
$this->notifyService->notify($exception);
}
}
}
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Expand Up @@ -3,7 +3,9 @@
namespace App\Providers;

use App\Interfaces\ILoggerService;
use App\Interfaces\INotify;
use App\Services\LaravelLoggerService;
use App\Services\LoggerNotifyService;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -16,6 +18,7 @@ class AppServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton(ILoggerService::class, LaravelLoggerService::class);
$this->app->singleton(INotify::class, LoggerNotifyService::class);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions app/Services/LoggerNotifyService.php
@@ -0,0 +1,15 @@
<?php


namespace App\Services;


use App\Interfaces\INotify;

class LoggerNotifyService implements INotify
{
public function notify(\Exception $exception): string
{
return $exception->getMessage();
}
}
1 change: 0 additions & 1 deletion app/Services/StringCalculatorService.php
Expand Up @@ -2,7 +2,6 @@

namespace App\Services;

use App\Events\StringCalculatorAdded;
use App\Events\StringWasAdded;
use App\Exceptions\StringCalculatorServiceException;

Expand Down
40 changes: 38 additions & 2 deletions tests/Services/StringCalculatorServiceTest.php
Expand Up @@ -5,9 +5,12 @@
use App\Events\StringWasAdded;
use App\Exceptions\StringCalculatorServiceException;
use App\Interfaces\ILoggerService;
use App\Interfaces\INotify;
use App\Services\StringCalculatorService;
use Exception;
use Illuminate\Support\Facades\Event;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;

/**
Expand Down Expand Up @@ -198,6 +201,25 @@ public function longerMultipleDelimitersProvider()
];
}

/**
* @test
* 嘗試不同的測試案例命名方式
*/
public function CallAdd_LogFailAndThrowException_GetExceptionMessageLogFail()
{
// stub: When calling Add() and the logger throws an exception
$this->instance(ILoggerService::class, new StubLoggerService());
$stubLoggerService = $this->app->make(ILoggerService::class);
$stubLoggerService->toThrow = true;

// mock: call notify() when writing log fail.
$this->mock(INotify::class)
->shouldReceive('notify')
->once();

$this->stringCalculatorService->add('');
}

/**
* @test
*/
Expand Down Expand Up @@ -251,10 +273,24 @@ protected function tearDown(): void
}

/**
* @return Mockery\MockInterface
* @return MockInterface
*/
private function mockLoggerInterface(): Mockery\MockInterface
private function mockLoggerInterface(): MockInterface
{
return $this->mock(ILoggerService::class);
}
}

class StubLoggerService implements ILoggerService
{
public $toThrow = false;

/**
* @param string $log
* @param string $level
*/
public function write(string $log, string $level = 'info'): void
{
throw_if($this->toThrow, Exception::class);
}
}

0 comments on commit 2a42dac

Please sign in to comment.