WishlistProviderTest.php in Commerce Wishlist 8.3
File
tests/src/Kernel/WishlistProviderTest.php
View source
<?php
namespace Drupal\Tests\commerce_wishlist\Kernel;
use Drupal\commerce_wishlist\Entity\WishlistInterface;
use Drupal\commerce_wishlist\Exception\DuplicateWishlistException;
class WishlistProviderTest extends WishlistKernelTestBase {
protected $anonymousUser;
protected $authenticatedUser;
protected $entityTypeManager;
protected $wishlistProvider;
protected function setUp() : void {
parent::setUp();
$this->anonymousUser = $this
->createUser([
'uid' => 0,
'name' => '',
'status' => 0,
]);
$this->authenticatedUser = $this
->createUser();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->wishlistProvider = $this->container
->get('commerce_wishlist.wishlist_provider');
}
public function testCreateAnonymousWishlist() {
$wishlist_type = 'default';
$wishlist = $this->wishlistProvider
->createWishlist($wishlist_type, $this->anonymousUser);
$this
->assertInstanceOf(WishlistInterface::class, $wishlist);
$this
->expectException(DuplicateWishlistException::class);
$this->wishlistProvider
->createWishlist($wishlist_type, $this->anonymousUser);
}
public function testGetAnonymousWishlist() {
$this->wishlistProvider
->createWishlist('default', $this->anonymousUser);
$wishlist = $this->wishlistProvider
->getWishlist('default', $this->anonymousUser);
$this
->assertInstanceOf(WishlistInterface::class, $wishlist);
$wishlist_id = $this->wishlistProvider
->getWishlistId('default', $this->anonymousUser);
$this
->assertEquals(1, $wishlist_id);
$wishlists = $this->wishlistProvider
->getWishlists($this->anonymousUser);
$this
->assertContainsOnlyInstancesOf(WishlistInterface::class, $wishlists);
$wishlist_ids = $this->wishlistProvider
->getWishlistIds($this->anonymousUser);
$this
->assertContains(1, $wishlist_ids);
}
public function testCreateAuthenticatedWishlist() {
$wishlist = $this->wishlistProvider
->createWishlist('default', $this->authenticatedUser);
$this
->assertInstanceOf(WishlistInterface::class, $wishlist);
$this
->expectException(DuplicateWishlistException::class);
$this->wishlistProvider
->createWishlist('default', $this->authenticatedUser);
}
public function testGetAuthenticatedWishlist() {
$this->wishlistProvider
->createWishlist('default', $this->authenticatedUser);
$wishlist = $this->wishlistProvider
->getWishlist('default', $this->authenticatedUser);
$this
->assertInstanceOf(WishlistInterface::class, $wishlist);
$wishlist_id = $this->wishlistProvider
->getWishlistId('default', $this->authenticatedUser);
$this
->assertEquals(1, $wishlist_id);
$wishlists = $this->wishlistProvider
->getWishlists($this->authenticatedUser);
$this
->assertContainsOnlyInstancesOf(WishlistInterface::class, $wishlists);
$wishlist_ids = $this->wishlistProvider
->getWishlistIds($this->authenticatedUser);
$this
->assertContains(1, $wishlist_ids);
}
}