Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging of failure on Customer removal #355

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Plugin/Customer/Model/ResourceModel/CustomerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Magento\Framework\Exception\LocalizedException;
use Taxjar\SalesTax\Model\Client;
use Taxjar\SalesTax\Model\ClientFactory;
use Taxjar\SalesTax\Model\Configuration;
use Taxjar\SalesTax\Model\Logger;

class CustomerRepository
{
Expand All @@ -14,12 +16,19 @@ class CustomerRepository
*/
private $clientFactory;

/**
* @var Logger $logger
*/
protected $logger;

/**
* @param ClientFactory $clientFactory
* @param Logger $logger
*/
public function __construct(ClientFactory $clientFactory)
public function __construct(ClientFactory $clientFactory, Logger $logger)
{
$this->clientFactory = $clientFactory;
$this->logger = $logger;
}

/**
Expand All @@ -38,7 +47,7 @@ public function beforeDeleteById(
$this->getClient()->deleteResource('customers', $customerId);
} catch (LocalizedException $e) {
$message = 'Could not delete customer #' . $customerId . ": " . $e->getMessage();
$this->logger->log($message, 'error');
$this->getLogger()->log($message, 'error');
}

return $customerId;
Expand All @@ -55,4 +64,16 @@ private function getClient(): Client
$client->showResponseErrors(true);
return $client;
}

/**
* Returns our Logger instance configured to write to the TaxJar customer log.
*
* @return Logger
*/
private function getLogger(): Logger
{
$logger = $this->logger;
$logger->setFilename(Configuration::TAXJAR_CUSTOMER_LOG);
return $logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Taxjar\SalesTax\Test\Unit\Plugin\Customer\Model\ResourceModel;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use PHPUnit\Framework\MockObject\MockObject;
use Taxjar\SalesTax\Model\Client;
use Taxjar\SalesTax\Model\ClientFactory;
use Taxjar\SalesTax\Model\Configuration;
use Taxjar\SalesTax\Model\Logger;
use Taxjar\SalesTax\Plugin\Customer\Model\ResourceModel\CustomerRepository;
use Taxjar\SalesTax\Test\Unit\UnitTestCase;

Expand All @@ -21,13 +24,22 @@ class CustomerRepositoryInterfaceTest extends UnitTestCase
*/
private $clientFactoryMock;

/**
* @var MockObject|Logger
*/
private $loggerMock;

protected function setUp(): void
{
parent::setUp();

$this->clientFactoryMock = $this->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();

$this->loggerMock = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
->getMock();
}

public function testBeforeDeleteById()
Expand All @@ -54,10 +66,38 @@ public function testBeforeDeleteById()
static::assertSame(123, $this->sut->beforeDeleteById($subjectMock, 123));
}

public function testBeforeDeleteByIdFailureLogsError()
{
$expectedMessage = 'Could not delete customer #123: Failed to initialize client';

$mockException = new LocalizedException(__('Failed to initialize client'));

$subjectMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->clientFactoryMock->expects(static::once())
->method('create')
->willThrowException($mockException);

$this->loggerMock->expects(static::once())
->method('setFilename')
->with(Configuration::TAXJAR_CUSTOMER_LOG);

$this->loggerMock->expects(static::once())
->method('log')
->with($expectedMessage);

$this->setExpectations();

static::assertSame(123, $this->sut->beforeDeleteById($subjectMock, 123));
}

protected function setExpectations()
{
$this->sut = new CustomerRepository(
$this->clientFactoryMock
$this->clientFactoryMock,
$this->loggerMock
);
}
}