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

Does not capture WC logger in WC < 3.5

parent f66e8992
No related branches found
No related tags found
1 merge request!8Feature/broken wc logger
Pipeline #6277 failed
## [1.4.0] - 2019-01-21
### Changed
- WC integration now considers broken WC_Logger implementation
- Does not capture WC logger in WC < 3.5
## [1.3.1] - 2018-10-30
### Changed
......
......@@ -15,6 +15,9 @@ class WooCommerceCapture
const WOOCOMMERCE_LOGGER_FILTER = 'woocommerce_logging_class';
const WOOCOMMERCE_AFTER_IS_LOADED_ACTION = 'woocommerce_loaded';
/** @var string Minimal version of WooCommerce supported by logger capture */
const SUPPORTED_WC_VERSION = '3.5';
/**
* Is logger filter captured by library.
*
......@@ -71,11 +74,22 @@ class WooCommerceCapture
}
}
/**
* Is this version of WooCommerce is supported by logger capture
*
* @return bool
*/
public static function isSupportedWCVersion()
{
return version_compare(WC_VERSION, self::SUPPORTED_WC_VERSION, '>=') >= 0;
}
/**
* Capture WooCommerce logger and inject our decorated Logger
*/
public function captureWcLogger()
{
if (self::isSupportedWCVersion()) {
if ($this->isCaptured) {
throw new WCLoggerAlreadyCaptured('Try to free wc logger first.');
}
......@@ -93,6 +107,10 @@ class WooCommerceCapture
} else {
$this->monolog->alert('Cannot capture WC - WordPress is not available.');
}
} else {
$this->monolog->alert('Cannot capture WC - WooCommerce version is not supported.');
}
}
/**
......
......@@ -3,6 +3,7 @@
use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use PHPUnit\Framework\Error\Notice;
use WPDesk\Logger\WC\WooCommerceCapture;
use WPDesk\Logger\WC\WooCommerceMonologPlugin;
use WPDesk\Logger\WPDeskLoggerFactory;
......@@ -30,8 +31,12 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testWCLoggingIsCapturedByOurLogs()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
(new WPDeskLoggerFactory())->createWPDeskLogger();
$this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured.");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
}
//
// This test won't work in PHPUnit 5.x. Enable when support for PHP 5.6 is dropped
......@@ -77,11 +82,15 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testOurLogsGetAllMessagesLoggedToWC()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
$someMessage = 'whatever';
$logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
$logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage($someMessage));
wc_get_logger()->debug($someMessage);
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
}
public function testLoggerWorksAndCanLogInGeneral()
......@@ -110,6 +119,7 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testAllLoggedMessagesAreWrittenToWC()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
$mockWcLogger = $this->createMock(WC_Logger::class);
$mockWcLogger
->expects($this->atLeastOnce())
......@@ -124,6 +134,10 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
$logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
$logger->debug($someMessage);
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5 so we cant mock for this test");
}
}
public function testDisable() {
......
......@@ -9,14 +9,19 @@ class TestWooCommerceCapture extends WP_UnitTestCase
{
public function testIfCanCaptureWcLogger()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger();
$this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured.");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
}
public function testIfCanFreeWcLogger()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger();
$wcCapture->freeWcLogger();
......@@ -28,14 +33,21 @@ class TestWooCommerceCapture extends WP_UnitTestCase
$wcCapture->freeWcLogger();
$this->assertNotInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(),
"Logger should be restored to original - twice");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
}
public function testIfCantCaptureTwice()
{
if (WooCommerceCapture::isSupportedWCVersion()) {
$this->expectException(WCLoggerAlreadyCaptured::class);
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger();
$wcCapture->captureWcLogger();
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment