Skip to content

Commit

Permalink
Fix php spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrzytki committed Apr 11, 2024
1 parent 24eb2c0 commit b56ff7d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 98 deletions.
19 changes: 13 additions & 6 deletions spec/CommandHandler/Wishlist/CreateNewWishlistHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use BitBag\SyliusWishlistPlugin\Exception\WishlistNameIsTakenException;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
Expand All @@ -34,15 +35,17 @@ public function let(
WishlistFactoryInterface $wishlistFactory,
WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
ChannelRepositoryInterface $channelRepository,
WishlistNameCheckerInterface $wishlistNameChecker
WishlistNameCheckerInterface $wishlistNameChecker,
TokenUserResolverInterface $tokenUserResolver
): void {
$this->beConstructedWith(
$wishlistRepository,
$tokenStorage,
$wishlistFactory,
$wishlistCookieTokenResolver,
$channelRepository,
$wishlistNameChecker
$wishlistNameChecker,
$tokenUserResolver
);
}

Expand All @@ -62,12 +65,13 @@ public function it_creates_new_wishlist_for_user(
ShopUserInterface $shopUser,
WishlistInterface $wishlist,
WishlistInterface $existingWishlist,
ChannelInterface $channel
ChannelInterface $channel,
TokenUserResolverInterface $tokenUserResolver
): void {
$wishlists = [$existingWishlist];

$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$tokenUserResolver->resolve($token)->willReturn($shopUser);

$wishlistCookieTokenResolver->resolve()->willReturn('token');
$wishlistFactory->createForUser($shopUser)->willReturn($wishlist);
Expand Down Expand Up @@ -99,8 +103,10 @@ public function it_creates_new_wishlist_for_guest(
ChannelRepositoryInterface $channelRepository,
WishlistInterface $newWishlist,
ChannelInterface $channel,
TokenUserResolverInterface $tokenUserResolver
): void {
$tokenStorage->getToken()->willReturn(null);
$tokenUserResolver->resolve(null)->willReturn(null);

$wishlistCookieTokenResolver->resolve()->willReturn('token');
$wishlistFactory->createNew()->willReturn($newWishlist);
Expand Down Expand Up @@ -129,12 +135,13 @@ public function it_doesnt_add_duplicated_wishlist_name_for_user(
ShopUserInterface $shopUser,
WishlistInterface $wishlist,
WishlistInterface $existingWishlist,
ChannelInterface $channel
ChannelInterface $channel,
TokenUserResolverInterface $tokenUserResolver
): void {
$wishlists = [$existingWishlist];

$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($shopUser);
$tokenUserResolver->resolve($token)->willReturn($shopUser);

$wishlistCookieTokenResolver->resolve()->willReturn('token');
$wishlistFactory->createForUser($shopUser)->willReturn($wishlist);
Expand Down
41 changes: 34 additions & 7 deletions spec/CommandHandler/Wishlist/CreateWishlistHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\ShopUserWishlistResolverInterface;
use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface;
use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
Expand All @@ -31,14 +35,19 @@ public function let(
WishlistFactoryInterface $wishlistFactory,
ShopUserWishlistResolverInterface $shopUserWishlistResolver,
ObjectManager $wishlistManager,
ChannelRepositoryInterface $channelRepository
ChannelRepositoryInterface $channelRepository,
TokenUserResolverInterface $tokenUserResolver,
RequestStack $requestStack,
): void {
$this->beConstructedWith(
$tokenStorage,
$wishlistFactory,
$shopUserWishlistResolver,
$wishlistManager,
$channelRepository
$channelRepository,
$tokenUserResolver,
$requestStack,
'token'
);
}

Expand All @@ -56,18 +65,27 @@ public function it_creates_new_wishlist_for_user(
ShopUserWishlistResolverInterface $shopUserWishlistResolver,
ChannelRepositoryInterface $channelRepository,
ChannelInterface $channel,
ObjectManager $wishlistManager
ObjectManager $wishlistManager,
TokenUserResolverInterface $tokenUserResolver,
RequestStack $requestStack,
Request $request,
ParameterBag $attributes
): void {
$tokenValue = 'test_token_value';
$channelCode = 'test_channel_code';

$createWishlist = new CreateWishlist($tokenValue, $channelCode);

$tokenStorage->getToken()->willReturn($token);
$token->getUser()->willReturn($user);
$tokenUserResolver->resolve($token)->willReturn($user);

$wishlistFactory->createNew()->willReturn($wishlist);
$shopUserWishlistResolver->resolve($user)->willReturn($wishlist);
$wishlist->setName('Wishlist')->shouldBeCalledOnce();
$wishlist->setToken($tokenValue)->shouldBeCalledOnce();

$requestStack->getMainRequest()->willReturn($request)->shouldBeCalledOnce();
$request->attributes = $attributes;

$channelRepository->findOneByCode($channelCode)->willReturn($channel);

Expand All @@ -88,20 +106,29 @@ public function it_creates_new_wishlist_for_guest_with_missing_channel(
ShopUserWishlistResolverInterface $shopUserWishlistResolver,
ChannelRepositoryInterface $channelRepository,
ChannelInterface $channel,
ObjectManager $wishlistManager
ObjectManager $wishlistManager,
TokenUserResolverInterface $tokenUserResolver,
RequestStack $requestStack,
Request $request,
ParameterBag $attributes
): void {
$tokenValue = 'test_token_value';
$channelCode = null;

$createWishlist = new CreateWishlist($tokenValue, $channelCode);

$tokenStorage->getToken()->willReturn(null);
$token->getUser()->shouldNotBeCalled();
$tokenUserResolver->resolve(null)->willReturn(null);

$wishlistFactory->createNew()->willReturn($wishlist);
$shopUserWishlistResolver->resolve($user)->shouldNotBeCalled();
$wishlist->setName('Wishlist')->shouldBeCalledOnce();
$wishlist->setToken($tokenValue)->shouldBeCalledOnce();

$requestStack->getMainRequest()->willReturn($request)->shouldBeCalledOnce();
$request->attributes = $attributes;

$channelRepository->findOneByCode('test')->shouldNotBeCalled();
$channelRepository->findOneByCode('test')->willReturn(null);

$wishlist->setChannel($channel)->shouldNotBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public function it_export_wishlist_to_csv(
AddToCartCommandInterface $addToCartCommand,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
WishlistItemInterface $wishlistItem,
WishlistProductInterface $wishlistProduct,
ProductInterface $product,
WishlistItemInterface $wishlistItem,
\SplFileObject $file,
CsvWishlistProductFactoryInterface $csvWishlistProductFactory,
CsvWishlistProductInterface $csvWishlistProduct,
CsvSerializerFactoryInterface $csvSerializerFactory,
Serializer $serializer
): void {
$wishlistProducts = new ArrayCollection([$wishlistItem]);
$wishlistProducts = new ArrayCollection([$wishlistItem->getWrappedObject()]);

$headers = [
'variantId',
Expand Down Expand Up @@ -90,7 +90,7 @@ public function it_export_wishlist_to_csv(

$file->fputcsv(['serializer_result'])->shouldBeCalled();

$exportWishlistToCsv = new ExportWishlistToCsv($wishlistProducts, $file);
$exportWishlistToCsv = new ExportWishlistToCsv($wishlistProducts, $file->getWrappedObject());

$this->__invoke($exportWishlistToCsv);
}
Expand Down
80 changes: 0 additions & 80 deletions spec/CommandHandler/Wishlist/ImportWishlistFromCsvHandlerSpec.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ public function it_throws_exception_when_product_not_found(
public function it_throws_exception_when_wishlist_not_found(
ProductRepositoryInterface $productRepository,
WishlistRepositoryInterface $wishlistRepository,
ProductInterface $product
ProductInterface $product,
WishlistProductInterface $wishlistProduct,
RepositoryInterface $wishlistProductRepository
): void {
$removeProductCommand = new RemoveProductFromWishlist(1, 'wishlist_token');

$productRepository->find(1)->willReturn($product);
$wishlistProductRepository->findOneBy([
'product' => $product,
'wishlist' => null,
])->willReturn($wishlistProduct);
$wishlistRepository->findByToken('wishlist_token')->willReturn(null);

$this->shouldThrow(WishlistNotFoundException::class)->during('__invoke', [$removeProductCommand]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ public function it_throws_exception_when_wishlist_not_found(
ProductVariantRepositoryInterface $productVariantRepository,
WishlistRepositoryInterface $wishlistRepository,
RepositoryInterface $wishlistProductRepository,
ProductVariantInterface $variant
ProductVariantInterface $variant,
WishlistProductInterface $wishlistProduct,
): void {
$removeProductVariantCommand = new RemoveProductVariantFromWishlist(1, 'wishlist_token');

$productVariantRepository->find(1)->willReturn($variant);
$wishlistProductRepository->findOneBy(['variant' => $variant])->willReturn($wishlistProduct);
$wishlistRepository->findByToken('wishlist_token')->willReturn(null);

$this->shouldThrow(WishlistNotFoundException::class)->during('__invoke', [$removeProductVariantCommand]);
Expand Down

0 comments on commit b56ff7d

Please sign in to comment.