CartHandlerTest.php in Mailchimp E-Commerce 8
File
tests/src/Unit/CartHandlerTest.php
View source
<?php
namespace Drupal\Tests\mailchimp_ecommerce\Unit;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\mailchimp_ecommerce\CartHandler;
use Drupal\mailchimp_ecommerce\MailchimpEcommerceHelper;
use Drupal\Tests\UnitTestCase;
use Mailchimp\MailchimpEcommerce;
require_once __DIR__ . '/../../require.php';
class CartHandlerTest extends UnitTestCase {
protected $handler;
protected $messenger;
protected $helper;
protected $mcEcommerce;
protected $errors;
protected function setUp() {
parent::setUp();
$this->mcEcommerce = $this
->createMock(MailchimpEcommerce::class);
$this->helper = $this
->createMock(MailchimpEcommerceHelper::class);
$this->messenger = $this
->createMock(MessengerInterface::class);
$this->handler = new CartHandler($this->mcEcommerce, $this->helper, $this->messenger);
$this->errors = [];
$this->messenger
->method('addError')
->willReturnCallback(function ($error) {
$this->errors[] = $error;
});
}
public function testGetCart() {
$this->helper
->expects($this
->at(0))
->method('getStoreId')
->willReturn('my_store');
$cart = new \stdClass();
$cart->foo = 'bar';
$this->mcEcommerce
->expects($this
->at(0))
->method('getCart')
->willReturn($cart);
$this
->assertTrue($this->handler
->cartExists('my_cart'));
$this->helper
->expects($this
->at(0))
->method('getStoreId')
->willReturn('my_store');
$this->mcEcommerce
->expects($this
->at(0))
->method('getCart')
->willReturn(NULL);
$this
->assertFalse($this->handler
->cartExists('my_cart'));
$this->helper
->expects($this
->at(0))
->method('getStoreId')
->willReturn('');
$this->handler
->cartExists('my_cart');
$this
->assertContains('Cannot get the requested cart without a store ID.', $this->errors);
}
public function testAddOrUpdateCart() {
$this->helper
->method('getStoreId')
->willReturn('my_store');
$this->helper
->method('validateCustomer')
->willReturn(TRUE);
$this->helper
->method('getCampaignId')
->willReturn(FALSE);
$cart = new \stdClass();
$cart->foo = 'bar';
$this->mcEcommerce
->expects($this
->at(0))
->method('getCart')
->willReturn($cart);
$this->mcEcommerce
->expects($this
->once())
->method('updateCart');
$this->handler
->addOrUpdateCart('my_cart', [], []);
$exception = new \Exception('', 404);
$this->mcEcommerce
->expects($this
->at(0))
->method('getCart')
->will($this
->throwException($exception));
$this->mcEcommerce
->expects($this
->once())
->method('addCart');
$this->handler
->addOrUpdateCart('my_cart', [], []);
}
public function testDeleteCart() {
$this->helper
->method('getStoreId')
->willReturn('my_store');
$this->mcEcommerce
->expects($this
->at(0))
->method('deleteCart');
$this->handler
->deleteCart('my_cart');
$exception = new \Exception('Error!', 404);
$this->mcEcommerce
->expects($this
->at(0))
->method('deleteCart')
->will($this
->throwException($exception));
$this->handler
->deleteCart('my_cart');
$this
->assertEmpty($this->errors);
$exception = new \Exception('Error!', 500);
$this->mcEcommerce
->expects($this
->at(0))
->method('deleteCart')
->will($this
->throwException($exception));
$this->handler
->deleteCart('my_cart');
$this
->assertContains('Error!', $this->errors);
}
public function testUpdateCartLine() {
$this->helper
->method('getStoreId')
->willReturn('my_store');
$this->mcEcommerce
->expects($this
->at(0))
->method('updateCartLine');
$this->handler
->updateCartLine('my_cart', 1, []);
$exception = new \Exception('', 404);
$this->mcEcommerce
->expects($this
->at(0))
->method('updateCartLine')
->will($this
->throwException($exception));
$this->mcEcommerce
->expects($this
->once())
->method('addCartLine');
$this->handler
->updateCartLine('my_cart', 1, []);
$exception = new \Exception('Error!', 500);
$this->mcEcommerce
->expects($this
->at(0))
->method('updateCartLine')
->will($this
->throwException($exception));
$this->handler
->updateCartLine('my_cart', 1, []);
$this
->assertContains('Error!', $this->errors);
}
public function testDeleteCartLine() {
$this->helper
->method('getStoreId')
->willReturn('my_store');
$this->mcEcommerce
->expects($this
->at(0))
->method('deleteCartLine');
$this->handler
->deleteCartLine('my_cart', 1);
$exception = new \Exception('Error!');
$this->mcEcommerce
->expects($this
->at(0))
->method('deleteCartLine')
->will($this
->throwException($exception));
$this->handler
->deleteCartLine('my_cart', 1, []);
$this
->assertContains('Error!', $this->errors);
}
}