1
0
mirror of synced 2025-01-19 09:21:43 +03:00
woocommerce-module/tests/test-wc-retailcrm-customers.php

368 lines
14 KiB
PHP
Raw Normal View History

<?php
2021-12-20 11:41:41 +03:00
use datasets\DataCustomersRetailCrm;
2022-01-10 12:53:00 +03:00
/**
* PHP version 5.6
*
* Class WC_Retailcrm_Customers_Test - Testing WC_Retailcrm_Customers.
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $apiMock;
protected $responseMock;
protected $customer;
public function setUp()
{
2021-12-20 11:41:41 +03:00
$this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
->disableOriginalConstructor()
->setMethods(array(
'isSuccessful',
'offsetExists'
))
->getMock();
$this->responseMock->setResponse(array('id' => 1));
$this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Proxy')
2021-12-20 11:41:41 +03:00
->disableOriginalConstructor()
->setMethods(array(
'ordersGet',
'ordersCreate',
'ordersEdit',
'customersGet',
'customersCreate',
'customersEdit',
'getCorporateEnabled',
'customersCorporateCreate',
'customersCorporateAddressesCreate',
'customersCorporateCompaniesCreate',
'getSingleSiteForKey',
'customersCorporateAddresses',
'customersList'
2021-12-20 11:41:41 +03:00
))
->getMock();
$this->setMockResponse($this->responseMock, 'isSuccessful', true);
$this->setMockResponse($this->responseMock, 'offsetExists', true);
$this->setMockResponse($this->apiMock, 'getCorporateEnabled', true);
$this->setMockResponse($this->apiMock, 'getSingleSiteForKey', 'test');
$this->setMockResponse($this->apiMock, 'customersCreate', $this->responseMock);
$this->setMockResponse($this->apiMock, 'customersCorporateCreate', $this->responseMock);
$this->setMockResponse($this->apiMock, 'customersCorporateAddressesCreate', $this->responseMock);
$this->setMockResponse($this->apiMock, 'customersCorporateCompaniesCreate', $this->responseMock);
$this->setMockResponse($this->apiMock, 'customersCorporateCreate', true);
2019-01-17 15:58:13 +03:00
$this->customer = new WC_Customer();
$this->customer->set_first_name('Tester');
$this->customer->set_last_name('Tester');
$this->customer->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com');
2020-04-13 16:09:44 +03:00
$this->customer->set_billing_email($this->customer->get_email());
$this->customer->set_password('password');
$this->customer->set_billing_phone('89000000000');
2021-12-20 11:41:41 +03:00
$this->customer->set_billing_company('test_company');
$this->customer->set_billing_state('test_state');
$this->customer->set_billing_postcode('123456');
$this->customer->set_billing_city('test_city');
$this->customer->set_billing_address_1('test_address_line');
2020-11-19 18:02:04 +03:00
$this->customer->set_date_created(date('Y-m-d H:i:s'));
$this->customer->save();
}
/**
* @param retailcrm
2021-12-20 11:41:41 +03:00
*
* @dataProvider dataProviderApiClient
*/
public function test_wc_customer_get($retailcrm)
{
$wc_customer = new WC_Customer($this->customer->get_id());
2021-12-20 11:41:41 +03:00
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
2021-12-20 11:41:41 +03:00
$this->assertEquals($wc_customer, $retailcrmCustomer->wcCustomerGet($this->customer->get_id()));
}
/**
* @param $retailcrm
2021-12-20 11:41:41 +03:00
*
* @dataProvider dataProviderApiClient
*/
public function test_create_customer($retailcrm)
{
2021-12-20 11:41:41 +03:00
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$id = $retailcrmCustomer->createCustomer($this->customer->get_id());
$customer = $retailcrmCustomer->getCustomer();
if ($retailcrm) {
2021-12-20 11:41:41 +03:00
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertNotEmpty($customer['externalId']);
$this->assertNotEmpty($customer['createdAt']);
$this->assertNotEmpty($customer['firstName']);
$this->assertNotEmpty($customer['email']);
$this->assertEquals($customer['firstName'], $this->customer->get_first_name());
$this->assertEquals($customer['email'], $this->customer->get_email());
} else {
2019-01-17 15:58:13 +03:00
$this->assertEquals(null, $id);
2021-12-20 11:41:41 +03:00
$this->assertEquals(array(), $customer);
}
}
/**
* @param $retailcrm
*
* @dataProvider dataProviderApiClient
*/
public function test_create_customer_registration($retailcrm)
{
if ($retailcrm) {
$this->buildResponseCustomersList($retailcrm, DataCustomersRetailCrm::getEmptyCustomersList());
}
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$id = $retailcrmCustomer->registerCustomer($this->customer->get_id());
$customer = $retailcrmCustomer->getCustomer();
if ($retailcrm) {
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertNotEmpty($customer['externalId']);
$this->assertNotEmpty($customer['createdAt']);
$this->assertNotEmpty($customer['firstName']);
$this->assertNotEmpty($customer['email']);
$this->assertEquals($customer['firstName'], $this->customer->get_first_name());
$this->assertEquals($customer['email'], $this->customer->get_email());
} else {
$this->assertEquals(null, $id);
$this->assertEquals(array(), $customer);
}
}
2021-12-20 11:41:41 +03:00
public function test_create_customer_empty_data()
{
$retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock);
$id = $retailcrmCustomer->createCustomer(null);
$customer = $retailcrmCustomer->getCustomer();
$this->assertEquals(null, $id);
$this->assertEquals(array(), $customer);
}
/**
* @param $retailcrm
2021-12-20 11:41:41 +03:00
*
* @dataProvider dataProviderApiClient
*/
public function test_update_customer_registration($retailcrm)
{
if ($retailcrm) {
$this->buildResponseCustomersList($retailcrm, DataCustomersRetailCrm::getCustomersList());
}
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$wcCustomer = $retailcrmCustomer->registerCustomer($this->customer->get_id());
$customer = $retailcrmCustomer->getCustomer();
if ($retailcrm) {
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertNotEmpty($customer['externalId']);
$this->assertNotEmpty($customer['createdAt']);
$this->assertNotEmpty($customer['firstName']);
$this->assertNotEmpty($customer['email']);
} else {
$this->assertEquals(null, $wcCustomer);
$this->assertEquals(array(), $customer);
}
}
/**
* @param $retailcrm
*
* @dataProvider dataProviderApiClient
* @throws Exception
*/
public function test_update_customer($retailcrm)
{
2021-12-20 11:41:41 +03:00
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$wcCustomer = $retailcrmCustomer->updateCustomer($this->customer->get_id());
$customer = $retailcrmCustomer->getCustomer();
if ($retailcrm) {
2021-12-20 11:41:41 +03:00
$this->assertInstanceOf('WC_Customer', $wcCustomer);
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertNotEmpty($customer['externalId']);
$this->assertNotEmpty($customer['createdAt']);
$this->assertNotEmpty($customer['firstName']);
$this->assertNotEmpty($customer['email']);
} else {
2021-12-20 11:41:41 +03:00
$this->assertEquals(null, $wcCustomer);
$this->assertEquals(array(), $customer);
}
}
/**
* @param $retailcrm
*
* @dataProvider dataProviderApiClient
*/
2021-12-20 11:41:41 +03:00
public function test_update_customer_by_id($retailcrm)
{
2021-12-20 11:41:41 +03:00
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$wcCustomer = $retailcrmCustomer->updateCustomerById($this->customer->get_id(), '12345');
$customer = $retailcrmCustomer->getCustomer();
2021-12-20 11:41:41 +03:00
if ($retailcrm) {
$this->assertInstanceOf('WC_Customer', $wcCustomer);
$this->assertArrayHasKey('externalId', $customer);
$this->assertArrayHasKey('firstName', $customer);
$this->assertArrayHasKey('createdAt', $customer);
$this->assertArrayHasKey('email', $customer);
$this->assertNotEmpty($customer['externalId']);
$this->assertNotEmpty($customer['createdAt']);
$this->assertNotEmpty($customer['firstName']);
$this->assertNotEmpty($customer['email']);
} else {
$this->assertEquals(null, $wcCustomer);
$this->assertEquals(array(), $customer);
}
}
/**
* @param $retailcrm
*
* @dataProvider dataProviderApiClient
*/
public function test_is_corparate_enabled($retailcrm)
{
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$isCorporate = $retailcrmCustomer->isCorporateEnabled();
if ($retailcrm) {
$this->assertEquals(true, $isCorporate);
} else {
$this->assertEquals(false, $isCorporate);
}
}
/**
* @param $retailcrm
*
* @dataProvider dataProviderApiClient
*/
public function test_create_customer_corporate($retailcrm)
{
$retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm);
$id = $retailcrmCustomer->createCorporateCustomerForOrder(777, $this->customer->get_id(), new WC_Order());
$customer = $retailcrmCustomer->getCorporateCustomer();
if ($retailcrm) {
$this->assertArrayHasKey('customerContacts', $customer);
foreach ($customer['customerContacts'] as $customerCorporate) {
$this->assertArrayHasKey('isMain', $customerCorporate);
$this->assertArrayHasKey('customer', $customerCorporate);
$this->assertEquals($customerCorporate['isMain'], true);
$this->assertEquals($customerCorporate['customer']['id'], 777);
}
} else {
$this->assertEquals(null, $id);
$this->assertEquals(array(), $customer);
}
}
public function test_create_customer_corporate_empty_data()
{
$retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock);
$id = $retailcrmCustomer->createCorporateCustomerForOrder(777, null, new WC_Order());
$customer = $retailcrmCustomer->getCorporateCustomer();
$this->assertEquals(null, $id);
2021-12-20 11:41:41 +03:00
$this->assertEquals(array(), $customer);
}
public function test_fill_corporate_address()
{
$retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock);
// Mock response for get customer address
$responseCustomerAddress = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
->disableOriginalConstructor()
->setMethods(array('isSuccessful'))
->getMock();
$this->setMockResponse($responseCustomerAddress, 'isSuccessful', true);
$responseCustomerAddress->setResponse(DataCustomersRetailCrm::getCustomerAddress());
//Set responseCustomerAddress mock for apiMock
$this->setMockResponse($this->apiMock, 'customersCorporateAddresses', $responseCustomerAddress);
$addressFound = $retailcrmCustomer->fillCorporateAddress($this->customer->get_id(), $this->customer);
$this->assertEquals(true, $addressFound);
}
public function dataProviderApiClient()
{
$this->setUp();
return array(
array(
'retailcrm' => $this->apiMock
),
array(
'retailcrm' => false
)
);
}
2019-03-20 15:51:31 +03:00
/**
* @param $retailcrm
*
* @return WC_Retailcrm_Customers
*/
private function getRetailcrmCustomer($retailcrm)
{
return new WC_Retailcrm_Customers(
$retailcrm,
$this->getOptions(),
new WC_Retailcrm_Customer_Address()
);
}
/**
* @param $retailcrm
* @param $response
*
* @return void
*/
private function buildResponseCustomersList($retailcrm, $response)
{
// Mock response for get customers list
$responseCustomersList = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')
->disableOriginalConstructor()
->setMethods(array('isSuccessful'))
->getMock();
$this->setMockResponse($responseCustomersList, 'isSuccessful', true);
$responseCustomersList->setResponse($response);
//Set responseCustomersList mock for apiMock
$this->setMockResponse($retailcrm, 'customersList', $responseCustomersList);
}
2019-03-20 15:51:31 +03:00
}