Skip to content
Snippets Groups Projects
Commit 9f68dc1f authored by dyszczo's avatar dyszczo
Browse files

tests our factory

parent 4a8c2d13
Branches
Tags
2 merge requests!3Devel,!2Feature/tests
<phpunit bootstrap="tests/integration/bootstrap.php" <phpunit bootstrap="tests/integration/bootstrap.php"
backupGlobals="false" backupGlobals="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
strict="true"
> >
<testsuites> <testsuites>
<testsuite> <testsuite>
......
<?php <?php
use Monolog\Handler\AbstractHandler;
use Monolog\Logger; use Monolog\Logger;
use PHPUnit\Framework\Error\Notice;
use WPDesk\Logger\WC\WooCommerceMonologPlugin; use WPDesk\Logger\WC\WooCommerceMonologPlugin;
use WPDesk\Logger\WPDeskLoggerFactory; use WPDesk\Logger\WPDeskLoggerFactory;
...@@ -9,6 +11,11 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -9,6 +11,11 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->setWordpressOptionsForWPDeskLoggerToWork();
}
private function setWordpressOptionsForWPDeskLoggerToWork()
{
update_option('wpdesk_helper_options', [ update_option('wpdesk_helper_options', [
'debug_log' => '1' 'debug_log' => '1'
]); ]);
...@@ -27,36 +34,105 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -27,36 +34,105 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
$this->assertTrue($factory->isWPDeskLogWorking()); $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(); (new WPDeskLoggerFactory())->createWPDeskLogger();
$this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured."); $this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured.");
} }
// public function testErrorsAreCaptured() public function testOurLogsGetAllErrors()
// { {
// $someMessage = 'whatever';
// } $errorType = E_USER_NOTICE;
//
// $logger = (new WPDeskLoggerFactory())->createWPDeskLogger();
// public function testLoggerCanLogWC() $logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage('E_USER_NOTICE: ' . $someMessage));
// { $this->expectException(Notice::class);
// trigger_error($someMessage, $errorType);
// } }
//
// public function testLoggerCanLogDirectly() /**
// { * Prepares listener that check if logger gets sent message
// *
// } * @param $message
// * @return AbstractHandler
// public function testHandleToFile() */
// { private function prepareListenHandleThatIsWaitingForMessage($message)
// {
// } $listenHandle = $this->createMock(AbstractHandler::class);
//
// public function testHandleToWc() $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);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment