Skip to content
Snippets Groups Projects
Commit 0eaf62bf authored by Dyszczo's avatar Dyszczo
Browse files

Merge branch 'feature/broken-wc-logger' into 'master'

Feature/broken wc logger

See merge request !8
parents 47744abb c323c128
No related branches found
No related tags found
1 merge request!8Feature/broken wc logger
Pipeline #6409 failed
.git/ export-ignore
tests/ export-ignore
vendor/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.gitlab-ci.yml export-ignore
.idea export-ignore
apigen.neon export-ignore
phpcs.xml.dist export-ignore
phpunit-integration.xml export-ignore
phpunit-unit.xml export-ignore
variables: variables:
PHP_ERROR_REPORTING: E_ALL DISABLE_FUNCTIONAL: 1
COMPOSER_ALLOW_SUPERUSER: 1 DISABLE_ACCEPTANCE: 1
GIT_STRATEGY: fetch IS_LIBRARY: 1
MYSQL_ROOT_PASSWORD: mysql DISABLE_PHP_5_5: 1
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
MYSQL_INNODB_LOG_BUFFER_SIZE: 32M
PHP_ERROR_REPORTING: E_ALL
COMPOSER_ALLOW_SUPERUSER: 1
GIT_STRATEGY: fetch
include: 'https://gitlab.com/wpdesk/gitlab-ci/raw/master/gitlab-ci-1.2.yml'
stages:
- tools
- tests
.template: &job-test-template
stage: tests
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
.template: &job-test-unit-template
<<: *job-test-template
script:
- echo ${WPDESK_CI_VERSION}
- ls -l
- php --version
- cat /tmp/wordpress-develop/src/wp-includes/version.php
- cat /tmp/woocommerce/woocommerce.php
- composer update --no-progress
- vendor/bin/phpunit --configuration phpunit-unit.xml --coverage-text --colors=never
.template: &job-test-integration-template
<<: *job-test-template
services:
- mysql:5.6
script:
- echo ${WPDESK_CI_VERSION}
- ls -l
- php --version
- cat /tmp/wordpress-develop/src/wp-includes/version.php
- cat /tmp/woocommerce/woocommerce.php
- composer update --no-progress
- if [[ -f tests/integration/prepare.sh ]]; then sh tests/integration/prepare.sh; fi
- vendor/bin/phpunit --configuration phpunit-integration.xml --coverage-text --colors=never
before_script:
- cd ${CI_PROJECT_DIR}
phpmetric metrics:
stage: tools
image: wpdesknet/phpunit-woocommerce:0-0
allow_failure: true
when: manual
artifacts:
when: always
expire_in: 1 month
name: "metrics"
paths:
- ${CI_PROJECT_DIR}/phpmetric
script:
- echo ${WPDESK_CI_VERSION}
- composer require phpmetrics/phpmetrics
- composer update --no-progress
- php ./vendor/bin/phpmetrics --report-html=phpmetric .
churn metrics:
stage: tools
image: wpdesknet/phpunit-woocommerce:0-0
allow_failure: true
when: manual
script:
- echo ${WPDESK_CI_VERSION}
- composer require bmitch/churn-php
- composer update --no-progress
- vendor/bin/churn run classes inc
#unit test lastest:
# <<: *job-test-unit-template
# image: wpdesknet/phpunit-woocommerce:0-0
integration test lastest:
<<: *job-test-integration-template
image: wpdesknet/phpunit-woocommerce:0-0
## [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 ## [1.3.1] - 2018-10-30
### Changed ### Changed
- setDisableLog changes to disableLog - setDisableLog changes to disableLog
......
...@@ -12,10 +12,9 @@ ...@@ -12,10 +12,9 @@
"monolog/monolog": "^1.23" "monolog/monolog": "^1.23"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^6", "phpunit/phpunit": "^5",
"wp-coding-standards/wpcs": "^0.14.1", "wp-coding-standards/wpcs": "^0.14.1",
"squizlabs/php_codesniffer": "^3.0.2", "squizlabs/php_codesniffer": "^3.0.2",
"10up/wp_mock": "^0.3",
"wimg/php-compatibility": "^8" "wimg/php-compatibility": "^8"
}, },
"autoload": { "autoload": {
......
...@@ -15,6 +15,9 @@ class WooCommerceCapture ...@@ -15,6 +15,9 @@ class WooCommerceCapture
const WOOCOMMERCE_LOGGER_FILTER = 'woocommerce_logging_class'; const WOOCOMMERCE_LOGGER_FILTER = 'woocommerce_logging_class';
const WOOCOMMERCE_AFTER_IS_LOADED_ACTION = 'woocommerce_loaded'; const WOOCOMMERCE_AFTER_IS_LOADED_ACTION = 'woocommerce_loaded';
/** @var string Minimal version of WooCommerce supported by logger capture */
const SUPPORTED_WC_VERSION = '3.5.0';
/** /**
* Is logger filter captured by library. * Is logger filter captured by library.
* *
...@@ -65,17 +68,28 @@ class WooCommerceCapture ...@@ -65,17 +68,28 @@ class WooCommerceCapture
if ($this->captureHookFunction === null) { if ($this->captureHookFunction === null) {
$this->captureHookFunction = function () use ($monolog) { $this->captureHookFunction = function () use ($monolog) {
return new WooCommerceMonologPlugin($monolog); return new WooCommerceMonologPlugin($monolog, $this->originalWCLogger);
}; };
$this->monolog->pushHandler(new WooCommerceHandler($this->originalWCLogger)); $this->monolog->pushHandler(new WooCommerceHandler($this->originalWCLogger));
} }
} }
/**
* Is this version of WooCommerce is supported by logger capture
*
* @return bool
*/
public static function isSupportedWCVersion()
{
return version_compare(\WooCommerce::instance()->version, self::SUPPORTED_WC_VERSION, '>=');
}
/** /**
* Capture WooCommerce logger and inject our decorated Logger * Capture WooCommerce logger and inject our decorated Logger
*/ */
public function captureWcLogger() public function captureWcLogger()
{ {
if (self::isSupportedWCVersion()) {
if ($this->isCaptured) { if ($this->isCaptured) {
throw new WCLoggerAlreadyCaptured('Try to free wc logger first.'); throw new WCLoggerAlreadyCaptured('Try to free wc logger first.');
} }
...@@ -93,6 +107,10 @@ class WooCommerceCapture ...@@ -93,6 +107,10 @@ class WooCommerceCapture
} else { } else {
$this->monolog->alert('Cannot capture WC - WordPress is not available.'); $this->monolog->alert('Cannot capture WC - WordPress is not available.');
} }
} else {
$this->monolog->alert('Cannot capture WC - WooCommerce version is not supported.');
}
} }
/** /**
......
...@@ -12,13 +12,37 @@ use WC_Log_Levels; ...@@ -12,13 +12,37 @@ use WC_Log_Levels;
* *
* @package WPDesk\Logger * @package WPDesk\Logger
*/ */
class WooCommerceMonologPlugin implements \WC_Logger_Interface { class WooCommerceMonologPlugin implements \WC_Logger_Interface
{
/** @var Logger */ /** @var Logger */
private $monolog; private $monolog;
public function __construct( Logger $monolog ) { /** @var \WC_Logger */
private $originalWCLogger;
public function __construct(Logger $monolog, \WC_Logger $originalLogger)
{
$this->monolog = $monolog; $this->monolog = $monolog;
$this->originalWCLogger = $originalLogger;
}
/**
* Method added for compatibility with \WC_Logger
*
* @param string $source
*/
public function clear($source = '')
{
$this->originalWCLogger->clear($source);
}
/**
* Method added for compatibility with \WC_Logger
*/
public function clear_expired_logs()
{
$this->originalWCLogger->clear_expired_logs();
} }
/** /**
...@@ -31,7 +55,8 @@ class WooCommerceMonologPlugin implements \WC_Logger_Interface { ...@@ -31,7 +55,8 @@ class WooCommerceMonologPlugin implements \WC_Logger_Interface {
* *
* @deprecated * @deprecated
*/ */
public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) { public function add($handle, $message, $level = WC_Log_Levels::NOTICE)
{
$this->log($message, $level); $this->log($message, $level);
} }
...@@ -48,7 +73,8 @@ class WooCommerceMonologPlugin implements \WC_Logger_Interface { ...@@ -48,7 +73,8 @@ class WooCommerceMonologPlugin implements \WC_Logger_Interface {
$this->log(LogLevel::EMERGENCY, $message, $context); $this->log(LogLevel::EMERGENCY, $message, $context);
} }
public function log( $level, $message, $context = [] ) { public function log($level, $message, $context = [])
{
$this->monolog->log($level, $message, $context); $this->monolog->log($level, $message, $context);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use Monolog\Handler\AbstractHandler; use Monolog\Handler\AbstractHandler;
use Monolog\Logger; use Monolog\Logger;
use PHPUnit\Framework\Error\Notice; use PHPUnit\Framework\Error\Notice;
use WPDesk\Logger\WC\WooCommerceCapture;
use WPDesk\Logger\WC\WooCommerceMonologPlugin; use WPDesk\Logger\WC\WooCommerceMonologPlugin;
use WPDesk\Logger\WPDeskLoggerFactory; use WPDesk\Logger\WPDeskLoggerFactory;
...@@ -30,20 +31,26 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -30,20 +31,26 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testWCLoggingIsCapturedByOurLogs() public function testWCLoggingIsCapturedByOurLogs()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
(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.");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
} }
public function testOurLogsGetAllErrors()
{
$someMessage = 'whatever';
$errorType = E_USER_NOTICE;
$logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
$logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage('E_USER_NOTICE: ' . $someMessage));
$this->expectException(Notice::class);
trigger_error($someMessage, $errorType);
} }
//
// This test won't work in PHPUnit 5.x. Enable when support for PHP 5.6 is dropped
//
// public function testOurLogsGetAllErrors()
// {
// $someMessage = 'whatever';
// $errorType = E_USER_NOTICE;
//
// $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
// $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 * Prepares listener that check if logger gets sent message
...@@ -75,11 +82,15 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -75,11 +82,15 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testOurLogsGetAllMessagesLoggedToWC() public function testOurLogsGetAllMessagesLoggedToWC()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
$someMessage = 'whatever'; $someMessage = 'whatever';
$logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME); $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
$logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage($someMessage)); $logger->pushHandler($this->prepareListenHandleThatIsWaitingForMessage($someMessage));
wc_get_logger()->debug($someMessage); wc_get_logger()->debug($someMessage);
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
} }
public function testLoggerWorksAndCanLogInGeneral() public function testLoggerWorksAndCanLogInGeneral()
...@@ -108,6 +119,7 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -108,6 +119,7 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
public function testAllLoggedMessagesAreWrittenToWC() public function testAllLoggedMessagesAreWrittenToWC()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
$mockWcLogger = $this->createMock(WC_Logger::class); $mockWcLogger = $this->createMock(WC_Logger::class);
$mockWcLogger $mockWcLogger
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
...@@ -122,6 +134,10 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase ...@@ -122,6 +134,10 @@ class TestWPDeskLoggerFactory extends WP_UnitTestCase
$logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME); $logger = (new WPDeskLoggerFactory())->createWPDeskLogger(self::LOGGER_NAME);
$logger->debug($someMessage); $logger->debug($someMessage);
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5 so we cant mock for this test");
}
} }
public function testDisable() { public function testDisable() {
......
...@@ -9,14 +9,19 @@ class TestWooCommerceCapture extends WP_UnitTestCase ...@@ -9,14 +9,19 @@ class TestWooCommerceCapture extends WP_UnitTestCase
{ {
public function testIfCanCaptureWcLogger() public function testIfCanCaptureWcLogger()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class)); $wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger(); $wcCapture->captureWcLogger();
$this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured."); $this->assertInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), "Logger should be captured.");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
} }
public function testIfCanFreeWcLogger() public function testIfCanFreeWcLogger()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class)); $wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger(); $wcCapture->captureWcLogger();
$wcCapture->freeWcLogger(); $wcCapture->freeWcLogger();
...@@ -28,14 +33,21 @@ class TestWooCommerceCapture extends WP_UnitTestCase ...@@ -28,14 +33,21 @@ class TestWooCommerceCapture extends WP_UnitTestCase
$wcCapture->freeWcLogger(); $wcCapture->freeWcLogger();
$this->assertNotInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(), $this->assertNotInstanceOf(WooCommerceMonologPlugin::class, wc_get_logger(),
"Logger should be restored to original - twice"); "Logger should be restored to original - twice");
} else {
$this->markTestSkipped("Cant capture logger in WC < 3.5");
}
} }
public function testIfCantCaptureTwice() public function testIfCantCaptureTwice()
{ {
if (WooCommerceCapture::isSupportedWCVersion()) {
$this->expectException(WCLoggerAlreadyCaptured::class); $this->expectException(WCLoggerAlreadyCaptured::class);
$wcCapture = new WooCommerceCapture($this->createMock(Logger::class)); $wcCapture = new WooCommerceCapture($this->createMock(Logger::class));
$wcCapture->captureWcLogger(); $wcCapture->captureWcLogger();
$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