diff --git a/phpunit-integration.xml b/phpunit-integration.xml index 0910a38fa734aa21bd419940d7f87f34fdcf2085..a79673c82aca17806f7503fa3fa242e139e1c265 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -1,5 +1,9 @@ <phpunit bootstrap="tests/integration/bootstrap.php" backupGlobals="false" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + strict="true" > <testsuites> <testsuite> diff --git a/tests/integration/TestWPDeskLoggerFactory.php b/tests/integration/TestWPDeskLoggerFactory.php index a0744e0a80454074d68754fd35ae8ca4ee504fa2..30afb5864c0b5800d0c2f633ce38e85d2cd49f71 100644 --- a/tests/integration/TestWPDeskLoggerFactory.php +++ b/tests/integration/TestWPDeskLoggerFactory.php @@ -1,6 +1,8 @@ <?php +use Monolog\Handler\AbstractHandler; use Monolog\Logger; +use PHPUnit\Framework\Error\Notice; use WPDesk\Logger\WC\WooCommerceMonologPlugin; use WPDesk\Logger\WPDeskLoggerFactory; @@ -9,6 +11,11 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase public function setUp() { parent::setUp(); + $this->setWordpressOptionsForWPDeskLoggerToWork(); + } + + private function setWordpressOptionsForWPDeskLoggerToWork() + { update_option('wpdesk_helper_options', [ 'debug_log' => '1' ]); @@ -27,36 +34,105 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase $this->assertTrue($factory->isWPDeskLogWorking()); } - public function testWCIsCaptured() + public function testWCIsNotCorruptedByOtherTests() + { + $this->assertInstanceOf(WC_Logger::class, wc_get_logger(), "Logger should be NOT captured by default."); + } + + public function testWCLoggingIsCapturedByOurLogs() { (new WPDeskLoggerFactory())->createWPDeskLogger(); $this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured."); } -// public function testErrorsAreCaptured() -// { -// -// } -// -// -// public function testLoggerCanLogWC() -// { -// -// } -// -// public function testLoggerCanLogDirectly() -// { -// -// } -// -// public function testHandleToFile() -// { -// -// } -// -// public function testHandleToWc() -// { -// -// } + public function testOurLogsGetAllErrors() + { + $someMessage = 'whatever'; + $errorType = E_USER_NOTICE; + + $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(); + $logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage('E_USER_NOTICE: ' . $someMessage)); + $this->expectException(Notice::class); + trigger_error($someMessage, $errorType); + } + + /** + * Prepares listener that check if logger gets sent message + * + * @param $message + * @return AbstractHandler + */ + private function prepareListenHandleThatIsWaitingForMessage($message) + { + $listenHandle = $this->createMock(AbstractHandler::class); + + $listenHandle + ->expects($this->atLeastOnce()) + ->method('handle') + ->with($this->callback(function ($record) use ($message) { + $this->assertEquals($message, $record['message'], "Monolog should get message sent to logger"); + return $record['message'] === $message; + })) + ->willReturn(true); + + $listenHandle + ->expects($this->atLeastOnce()) + ->method('isHandling') + ->willReturn(true); + + /** @var $listenHandle AbstractHandler */ + return $listenHandle; + } + + public function testOurLogsGetAllMessagesLoggedToWC() + { + $someMessage = 'whatever'; + $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(); + $logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage($someMessage)); + + wc_get_logger()->debug($someMessage); + } + + public function testLoggerWorksAndCanLogInGeneral() + { + $someMessage = 'whatever'; + $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(); + $logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage($someMessage)); + + $logger->debug($someMessage); + } + + public function testAllLoggedMessagesAreWrittenToWPDeskFile() + { + $someMessage = 'whatever'; + + $factory = new WPDeskLoggerFactory(); + $logFilename = $factory->getWPDeskFileName(); + + unlink($logFilename); + $this->assertFileNotExists($logFilename); + + $logger = $factory->createWPDeskLogger(); + $logger->debug($someMessage); + $this->assertFileExists($logFilename); + } + + public function testAllLoggedMessagesAreWrittenToWC() + { + $mockWcLogger = $this->createMock(WC_Logger::class); + $mockWcLogger + ->expects($this->atLeastOnce()) + ->method('log') + ->willReturn(true); + + add_filter('woocommerce_logging_class', function () use ($mockWcLogger) { + return $mockWcLogger; + }, 0, 100); + + $someMessage = 'whatever'; + $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(); + + $logger->debug($someMessage); + } }