diff --git a/tests/Constraints/InstanceOfAtLeastOne.php b/tests/Constraints/InstanceOfAtLeastOne.php new file mode 100644 index 0000000..57f1da4 --- /dev/null +++ b/tests/Constraints/InstanceOfAtLeastOne.php @@ -0,0 +1,44 @@ +expectedClass = $expectedClass; + } + + /** + * @param $other + * @return bool + */ + public function matches($other): bool + { + foreach ($other as $item) { + if ($item instanceof $this->expectedClass) { + return true; + } + } + return false; + } + + /** + * @return string + */ + public function toString(): string + { + return "contains at least one instance of '{$this->expectedClass}'"; + } +} \ No newline at end of file diff --git a/tests/Unit/LogTraitTest.php b/tests/Unit/LogTraitTest.php index 45c42a0..3b9773f 100644 --- a/tests/Unit/LogTraitTest.php +++ b/tests/Unit/LogTraitTest.php @@ -10,6 +10,7 @@ use Ae3\LaravelLogsLayer\app\Services\DiscordLogService; use Ae3\LaravelLogsLayer\app\Services\EmailLogService; use Ae3\LaravelLogsLayer\app\Services\LogstashLogService; use Ae3\LaravelLogsLayer\app\Traits\LogTrait; +use Ae3\LaravelLogsLayer\Tests\Constraints\InstanceOfAtLeastOne; use Ae3\LaravelLogsLayer\Tests\TestCase; use Exception; use Mockery; @@ -74,7 +75,7 @@ class LogTraitTest extends TestCase * @return void * @throws ReflectionException */ - public function testLogServiceReturnsEmailLogServiceWhenDefaultIsDaily(): void + public function testLogServiceReturnsEmailLogServiceWhenDefaultIsEmail(): void { config(['logging.default' => 'email']); config(['logging.channels.stack.channels' => ['email']]); @@ -85,6 +86,85 @@ class LogTraitTest extends TestCase $this->assertContainsOnlyInstancesOf(EmailLogService::class, $traitInstance->getLogServices()); } + /** + * @return void + * @throws ReflectionException + */ + public function testLogServiceReturnsAllLogServicesWhenDefaultIsStack(): void + { + config(['logging.default' => 'stack']); + config(['logging.channels.stack.channels' => ['logstash', 'daily', 'discord', 'email']]); + + $traitInstance = $this->getMockForTrait(LogTrait::class); + $traitInstance->initializeLogServices(); + + $this->assertContainsInstanceOf(LogstashLogService::class, $traitInstance->getLogServices()); + $this->assertContainsInstanceOf(DailyLogService::class, $traitInstance->getLogServices()); + $this->assertContainsInstanceOf(DiscordLogService::class, $traitInstance->getLogServices()); + $this->assertContainsInstanceOf(EmailLogService::class, $traitInstance->getLogServices()); + } + + /** + * @return void + * @throws ReflectionException + */ + public function testLogServiceReturnsOnlyLogstashLogServiceWhenStackContainsOnlyLogstash(): void + { + config(['logging.default' => 'stack']); + config(['logging.channels.stack.channels' => ['logstash']]); + + $traitInstance = $this->getMockForTrait(LogTrait::class); + $traitInstance->initializeLogServices(); + + $this->assertContainsOnlyInstancesOf(LogstashLogService::class, $traitInstance->getLogServices()); + } + + /** + * @return void + * @throws ReflectionException + */ + public function testLogServiceReturnsOnlyDailyLogServiceWhenStackContainsOnlyDaily(): void + { + config(['logging.default' => 'stack']); + config(['logging.channels.stack.channels' => ['daily']]); + + $traitInstance = $this->getMockForTrait(LogTrait::class); + $traitInstance->initializeLogServices(); + + $this->assertContainsOnlyInstancesOf(DailyLogService::class, $traitInstance->getLogServices()); + } + + /** + * @return void + * @throws ReflectionException + */ + public function testLogServiceReturnsOnlyDiscordLogServiceWhenStackContainsOnlyDiscord(): void + { + config(['logging.default' => 'stack']); + config(['logging.channels.stack.channels' => ['discord']]); + + $traitInstance = $this->getMockForTrait(LogTrait::class); + $traitInstance->initializeLogServices(); + + $this->assertContainsOnlyInstancesOf(DiscordLogService::class, $traitInstance->getLogServices()); + } + + /** + * @return void + * @throws ReflectionException + */ + public function testLogServiceReturnsOnlyEmailLogServiceWhenStackContainsOnlyEmail(): void + { + config(['logging.default' => 'stack']); + config(['logging.channels.stack.channels' => ['email']]); + + $traitInstance = $this->getMockForTrait(LogTrait::class); + $traitInstance->initializeLogServices(); + + $this->assertContainsOnlyInstancesOf(EmailLogService::class, $traitInstance->getLogServices()); + } + + /** * @return void * @throws InvalidArgumentException @@ -155,4 +235,19 @@ class LogTraitTest extends TestCase // Verificando se o conteúdo esperado está presente no arquivo $this->assertStringContainsString($expectedContent, $logContent); } + + /** + * @param string $className + * @param iterable $haystack + * @param string $message + * @return void + */ + public static function assertContainsInstanceOf(string $className, iterable $haystack, string $message = ''): void + { + static::assertThat( + $haystack, + new InstanceOfAtLeastOne($className), + $message + ); + } } \ No newline at end of file