diff --git a/src/include/class-wc-retailcrm-plugin.php b/src/include/class-wc-retailcrm-plugin.php index 3769a7e..5bb3232 100644 --- a/src/include/class-wc-retailcrm-plugin.php +++ b/src/include/class-wc-retailcrm-plugin.php @@ -180,18 +180,6 @@ class WC_Retailcrm_Plugin { } } - /** - * Generates placeholder email - * - * @param string $name - * - * @return string - */ - public static function createPlaceholderEmail($name) - { - return substr(md5($name), 0, 15) . '@example.com'; - } - /** * Check running history * diff --git a/src/include/components/class-wc-retailcrm-logger.php b/src/include/components/class-wc-retailcrm-logger.php index 46d1f22..c9ae532 100644 --- a/src/include/components/class-wc-retailcrm-logger.php +++ b/src/include/components/class-wc-retailcrm-logger.php @@ -21,6 +21,11 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')): */ private static $instance; + /** + * @var array $additionalHandlers + */ + private static $additionalHandlers; + /** * WC_Retailcrm_Logger constructor. */ @@ -34,12 +39,20 @@ if (!class_exists('WC_Retailcrm_Logger') && class_exists('WC_Log_Levels')): private static function getInstance() { if (empty(static::$instance)) { - static::$instance = new WC_Logger(); + static::$instance = new WC_Logger(self::$additionalHandlers); } return static::$instance; } + /** + * @param array $additionalHandlers + */ + public static function setAdditionalHandlers($additionalHandlers) + { + self::$additionalHandlers = $additionalHandlers; + } + /** * Regular logging * diff --git a/src/retailcrm.php b/src/retailcrm.php index 4937dfe..1c29aeb 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -41,26 +41,7 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : $this->load_plugin_textdomain(); if (class_exists( 'WC_Integration' )) { - require_once(dirname(__FILE__) . '/include/interfaces/class-wc-retailcrm-builder-interface.php'); - require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-state.php'); - require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-result.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-logger.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-history-assembler.php'); - require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-paginated-request.php'); - require_once(dirname(__FILE__) . '/include/components/class-wc-retailcrm-customer-switcher.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstract-builder.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php'); - require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-payment.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php'); - require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php'); - require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-corporate-address.php'); - require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php'); - require_once(dirname(__FILE__ ) . '/include/functions.php'); + self::load_module(); add_filter('woocommerce_integrations', array( $this, 'add_integration')); } else { add_action('admin_notices', array($this, 'woocommerce_missing_notice')); @@ -110,6 +91,33 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : return $integrations; } + /** + * Loads module classes. + */ + public static function load_module() + { + require_once(dirname(__FILE__) . '/include/interfaces/class-wc-retailcrm-builder-interface.php'); + require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-state.php'); + require_once(dirname(__FILE__) . '/include/models/class-wc-retailcrm-customer-switcher-result.php'); + require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-logger.php'); + require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-history-assembler.php'); + require_once(dirname(__FILE__ ) . '/include/components/class-wc-retailcrm-paginated-request.php'); + require_once(dirname(__FILE__) . '/include/components/class-wc-retailcrm-customer-switcher.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstract-builder.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php'); + require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php'); + require_once(dirname(__FILE__ ) . '/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-payment.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php'); + require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php'); + require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php'); + require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-corporate-address.php'); + require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php'); + require_once(dirname(__FILE__ ) . '/include/functions.php'); + } + /** * Returns true if WooCommerce was found in plugin cache * diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5f32196..bb7b0c8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -32,6 +32,11 @@ if (file_exists($_wcOldBootstrap)) { } $plugin_dir = dirname(dirname(__FILE__)) . '/'; + // helpers +require $plugin_dir . 'src/include/components/class-wc-retailcrm-logger.php'; require $plugin_dir . 'tests/helpers/class-wc-retailcrm-response-helper.php'; require $plugin_dir . 'tests/helpers/class-wc-retailcrm-test-case-helper.php'; +require $plugin_dir . 'tests/helpers/class-wc-retailcrm-log-handler-stdout.php'; + +WC_Retailcrm_Logger::setAdditionalHandlers(array(new WC_Retailcrm_Log_Handler_Stdout())); \ No newline at end of file diff --git a/tests/helpers/class-wc-retailcrm-log-handler-stdout.php b/tests/helpers/class-wc-retailcrm-log-handler-stdout.php new file mode 100644 index 0000000..213d463 --- /dev/null +++ b/tests/helpers/class-wc-retailcrm-log-handler-stdout.php @@ -0,0 +1,117 @@ +add($entry, $handle); + } + + /** + * @param int $timestamp + * @param string $level + * @param string $message + * @param array $context + * + * @return string + */ + protected static function format_entry($timestamp, $level, $message, $context) + { + if (isset($context['_legacy'] ) && true === $context['_legacy']) { + if (isset($context['source']) && $context['source']) { + $handle = $context['source']; + } else { + $handle = 'log'; + } + + $message = apply_filters('woocommerce_logger_add_message', $message, $handle); + $time = date_i18n('m-d-Y @ H:i:s'); + $entry = sprintf('%s - %s', $time, $message); + } else { + $entry = parent::format_entry( $timestamp, $level, $message, $context ); + } + + return $entry; + } + + /** + * @param string $entry Log entry text. + * @param string $handle Log entry handle. + * + * @return bool True if write was successful. + */ + protected function add($entry, $handle) + { + $result = false; + + if (is_resource(STDOUT)) { + $result = fwrite(STDOUT, $entry . PHP_EOL); + } else { + $this->cache_log($entry, $handle); + } + + return false !== $result; + } + + /** + * Cache log to write later. + * + * @param string $entry Log entry text. + * @param string $handle Log entry handle. + */ + protected function cache_log($entry, $handle) + { + $this->cached_logs[] = array( + 'entry' => $entry, + 'handle' => $handle, + ); + } + + /** + * Write cached logs. + */ + public function write_cached_logs() + { + foreach ($this->cached_logs as $log) { + $this->add($log['entry'], $log['handle']); + } + } +} diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 3063153..69dd9d8 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -15,7 +15,7 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case $options = array( 'api_url' => 'https://example.retailcrm.ru', 'api_key' => 'dhsHJGYdjkHHJKJSGjhasjhgajsgJGHsg', - 'api_version' => $apiVersion, + 'api_version' => 'v5', 'p_draft' => 'no', 'p_pending' => 'no', 'p_private' => 'no', diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 0f6ec7c..be57a2a 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -41,13 +41,9 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper parent::setUp(); } - /** - * @dataProvider dataProvider - * @param $api_version - */ - public function test_history_order_create($api_version) + public function test_history_order_create() { - $this->setOptions($api_version); + $this->setOptions(); if (!add_option('retailcrm_orders_history_since_id', 0)) { update_option('retailcrm_orders_history_since_id', 0); @@ -61,7 +57,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper ->method('isSuccessful') ->willReturn(true); - $this->customersHistoryResponse->setResponse(array('success' => true, 'history' => array())); + $this->customersHistoryResponse->setResponse($this->empty_history()); $this->ordersHistoryResponse->expects($this->any()) ->method('isSuccessful') @@ -124,19 +120,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper } } - /** - * @dataProvider dataProvider - * @param $api_version - */ - public function test_history_order_add_product($api_version) + public function test_history_order_add_product() { - $this->setOptions($api_version); + $this->setOptions(); $this->customersHistoryResponse->expects($this->any()) ->method('isSuccessful') ->willReturn(true); - $this->customersHistoryResponse->setResponse(array('success' => true, 'history' => array())); + $this->customersHistoryResponse->setResponse($this->empty_history()); $this->ordersHistoryResponse->expects($this->any()) ->method('isSuccessful') @@ -164,19 +156,15 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals($product->get_id(), $order_updated_item->get_product()->get_id()); } - /** - * @dataProvider dataProvider - * @param $api_version - */ - public function test_history_order_update($api_version) + public function test_history_order_update() { - $this->setOptions($api_version); + $this->setOptions(); $this->customersHistoryResponse->expects($this->any()) ->method('isSuccessful') ->willReturn(true); - $this->customersHistoryResponse->setResponse(array('success' => true, 'history' => array())); + $this->customersHistoryResponse->setResponse($this->empty_history()); $this->ordersHistoryResponse->expects($this->any()) ->method('isSuccessful') @@ -185,7 +173,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $order = WC_Helper_Order::create_order(0); $this->ordersHistoryResponse->setResponse( - $this->get_history_data_update($order->get_id(), $api_version) + $this->get_history_data_update($order->get_id()) ); $this->apiMock->expects($this->any())->method('customersHistory')->willReturn($this->customersHistoryResponse); @@ -201,14 +189,16 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('payment2', $options[$order_updated->get_payment_method()]); } - public function dataProvider() + private function empty_history() { return array( - array( - 'api_version' => 'v4' - ), - array( - 'api_version' => 'v5' + 'success' => true, + 'history' => array(), + "pagination" => array( + "limit" => 100, + "totalCount" => 0, + "currentPage" => 1, + "totalPageCount" => 0 ) ); } @@ -347,6 +337,12 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'uploadedToExternalStoreSystem' => false ) ) + ), + "pagination" => array( + "limit" => 100, + "totalCount" => 1, + "currentPage" => 1, + "totalPageCount" => 1 ) ); } @@ -401,11 +397,17 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'purchasePrice' => 500 ) ) + ), + "pagination" => array( + "limit" => 100, + "totalCount" => 1, + "currentPage" => 1, + "totalPageCount" => 1 ) ); } - private function get_history_data_update($order_id, $api_version) + private function get_history_data_update($order_id) { $history = array( 'success' => true, @@ -432,6 +434,12 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'status' => self::STATUS_2 ) ) + ), + "pagination" => array( + "limit" => 100, + "totalCount" => 1, + "currentPage" => 1, + "totalPageCount" => 1 ) ); @@ -459,36 +467,9 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'type' => 'payment2', "amount" => 100 ) - ); - - $payment_v4 = array( - 'id' => 4, - 'createdAt' => '2018-01-01 00:03:00', - 'source' => 'user', - 'user' => array( - 'id' => 1 - ), - 'field' => 'payment_type', - 'oldValue' => null, - 'newValue' => array( - 'code' => 'payment2' - ), - 'order' => array( - 'id' => 2, - 'externalId' => $order_id, - 'managerId' => 6, - 'site' => 'test-com', - 'status' => self::STATUS_2 - ), ); - if ($api_version == 'v4') { - array_push($history['history'], $payment_v4); - } - - if ($api_version == 'v5') { - array_push($history['history'], $payment_v5); - } + array_push($history['history'], $payment_v5); return $history; } diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 1679e45..8366976 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -114,16 +114,11 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('WooCity', $order_send['delivery']['address']['city']); $this->assertEquals('delivery', $order_send['delivery']['code']); - if ($apiVersion == 'v4') { - $this->assertArrayHasKey('paymentType', $order_send); - $this->assertEquals('payment1', $order_send['paymentType']); - } elseif ($apiVersion == 'v5') { - $this->assertArrayHasKey('payments', $order_send); - $this->assertInternalType('array', $order_send['payments']); - $this->assertArrayHasKey('type', $order_send['payments'][0]); - $this->assertArrayHasKey('externalId', $order_send['payments'][0]); - $this->assertEquals('payment1', $order_send['payments'][0]['type']); - } + $this->assertArrayHasKey('payments', $order_send); + $this->assertInternalType('array', $order_send['payments']); + $this->assertArrayHasKey('type', $order_send['payments'][0]); + $this->assertArrayHasKey('externalId', $order_send['payments'][0]); + $this->assertEquals('payment1', $order_send['payments'][0]['type']); } else { $this->assertEquals(null, $order); } @@ -200,24 +195,19 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('WooCity', $order_send['delivery']['address']['city']); $this->assertEquals('delivery', $order_send['delivery']['code']); - if ($apiVersion == 'v4') { - $this->assertArrayHasKey('paymentType', $order_send); - $this->assertEquals('payment1', $order_send['paymentType']); - } elseif ($apiVersion == 'v5') { - $payment = $retailcrm_orders->getPayment(); - $this->assertInternalType('array', $payment); + $payment = $retailcrm_orders->getPayment(); + $this->assertInternalType('array', $payment); - if (!empty($payment)) { - $this->assertArrayHasKey('type', $payment); - $this->assertArrayHasKey('order', $payment); - $this->assertArrayHasKey('externalId', $payment); - $this->assertEquals('payment1', $payment['type']); + if (!empty($payment)) { + $this->assertArrayHasKey('type', $payment); + $this->assertArrayHasKey('order', $payment); + $this->assertArrayHasKey('externalId', $payment); + $this->assertEquals('payment1', $payment['type']); - if (!empty($this->options['send_payment_amount']) && $this->options['send_payment_amount'] == 'yes') { - $this->assertArrayHasKey('amount', $payment); - } else { - $this->assertArrayNotHasKey('amount', $payment); - } + if (!empty($this->options['send_payment_amount']) && $this->options['send_payment_amount'] == 'yes') { + $this->assertArrayHasKey('amount', $payment); + } else { + $this->assertArrayNotHasKey('amount', $payment); } } } else { @@ -249,26 +239,6 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper 'is_successful' => false, 'retailcrm' => $this->apiMock, 'api_version' => 'v5' - ), - array( - 'is_successful' => false, - 'retailcrm' => $this->apiMock, - 'api_version' => 'v4' - ), - array( - 'is_successful' => true, - 'retailcrm' => $this->apiMock, - 'api_version' => 'v4' - ), - array( - 'is_successful' => false, - 'retailcrm' => false, - 'api_version' => 'v4' - ), - array( - 'is_successful' => true, - 'retailcrm' => false, - 'api_version' => 'v4' ) ); } @@ -278,14 +248,6 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->setUp(); return array( - array( - 'retailcrm' => $this->apiMock, - 'api_version' => 'v4' - ), - array( - 'retailcrm' => false, - 'api_version' => 'v4' - ), array( 'retailcrm' => $this->apiMock, 'api_version' => 'v5'