Skip to content

Commit

Permalink
OP-291: Add tests for the new command
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Jul 29, 2024
1 parent 6a82668 commit d53fe47
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
35 changes: 35 additions & 0 deletions features/removing_guest_wishlists.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@cli_wishlist
Feature: Removing guest wishlists
In order to clean guest wishlists
As a developer
I want to be able to delete wishlists created by anonymous customers by running a CLI command

Background:
Given the store operates on a single channel in "United States"
And the store has a product "Jack Daniels Gentleman" priced at "$10.00"
And all store products appear under a main taxonomy
And I add this product to wishlist
And there is 1 wishlist in the database

@cli
Scenario: Removing all guest wishlists
Given there is a user "[email protected]"
And user "[email protected]" has a wishlist
And there are 2 wishlists in the database
When I run delete guest wishlists command
Then the command should succeed
And there is 1 wishlist in the database

@cli
Scenario: Removing guest wishlists with date
Given there is a guest wishlist which has been inactive for a week
And there are 2 wishlists in the database
When I run delete guests wishlists command to delete wishlists inactive for more than 5 days
Then the command should succeed
And there is 1 wishlist in the database

@cli
Scenario: Removing guest wishlists with invalid date
When I run delete guests wishlists command with invalid date
Then the command should fail
And there is 1 wishlist in the database
86 changes: 86 additions & 0 deletions tests/Behat/Context/Cli/WishlistContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusWishlistPlugin\Behat\Context\Cli;

use Behat\Behat\Context\Context;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Webmozart\Assert\Assert;

final class WishlistContext implements Context
{
public const REMOVE_GUEST_WISHLISTS_COMMAND = 'bitbag:wishlist:remove-guest-wishlists';

private Application $application;

private ?CommandTester $commandTester = null;

public function __construct(
KernelInterface $kernel,
) {
$this->application = new Application($kernel);
}

/**
* @When I run delete guest wishlists command
*/
public function runRemoveGuestWishlistsCommand(): void
{
$command = $this->application->find(self::REMOVE_GUEST_WISHLISTS_COMMAND);

$this->commandTester = new CommandTester($command);
$this->commandTester->execute([]);
}

/**
* @When I run delete guests wishlists command with invalid date
*/
public function runRemoveGuestWishlistsCommandWithInvalidDate(): void
{
$command = $this->application->find(self::REMOVE_GUEST_WISHLISTS_COMMAND);
$this->commandTester = new CommandTester($command);
$this->commandTester->execute(['--date' => 'invalidDate']);
}

/**
* @When the command should succeed
*/
public function theCommandShouldSucceed(): void
{
Assert::isInstanceOf($this->commandTester, CommandTester::class);
Assert::same($this->commandTester->getStatusCode(), 0);
}

/**
* @When the command should fail
*/
public function theCommandShouldFail(): void
{
Assert::isInstanceOf($this->commandTester, CommandTester::class);
Assert::same($this->commandTester->getStatusCode(), 1);
}

/**
* @When I run delete guests wishlists command to delete wishlists inactive for more than 5 days
*/
public function runRemoveGuestWishlistsCommandWithDate5DaysAgo(): void
{
$date = new \DateTime();
$date->modify('-5 days');
$date = $date->format('d-m-Y');

$command = $this->application->find(self::REMOVE_GUEST_WISHLISTS_COMMAND);
$this->commandTester = new CommandTester($command);
$this->commandTester->execute(['--date' => $date]);
}
}
32 changes: 32 additions & 0 deletions tests/Behat/Context/Common/WishlistContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusWishlistPlugin\Behat\Context\Common;

use Behat\Behat\Context\Context;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use Webmozart\Assert\Assert;

final class WishlistContext implements Context
{
public function __construct(private WishlistRepositoryInterface $wishlistRepository)
{
}

/**
* @When there are :count wishlists in the database
* @When there is :count wishlist in the database
*/
public function thereAreWishlistsInTheDatabase(int $count): void
{
Assert::same(count($this->wishlistRepository->findAll()), $count);
}
}
37 changes: 37 additions & 0 deletions tests/Behat/Context/Setup/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,41 @@ public function userHasAWishlistNamedWithToken(string $email, string $name, stri
$this->wishlistManager->persist($wishlist);
$this->wishlistManager->flush();
}

/**
* @When user :email has a wishlist
*/
public function userHasAWishlist(string $email): void
{
/** @var ?ShopUserInterface $user */
$user = $this->userRepository->findOneByEmail($email);
Assert::notNull($user);

$wishlist = new Wishlist();
$channel = $this->channelRepository->findOneByCode('WEB-US');

$wishlist->setChannel($channel);
$wishlist->setShopUser($user);

$this->wishlistManager->persist($wishlist);
$this->wishlistManager->flush();
}

/**
* @When there is a guest wishlist which has been inactive for a week
*/
public function thereIsAGuestWishlistInactiveForAWeek(): void
{
$wishlist = new Wishlist();
$channel = $this->channelRepository->findOneByCode('WEB-US');

$updatedAt = new \DateTime();
$updatedAt->modify('-7 days');

$wishlist->setChannel($channel);
$wishlist->setUpdatedAt($updatedAt);

$this->wishlistManager->persist($wishlist);
$this->wishlistManager->flush();
}
}
10 changes: 10 additions & 0 deletions tests/Behat/Resources/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ services:
- "@bitbag_wishlist_plugin.behat.page.wishlist.index_page"
- "@bitbag_wishlist_plugin.behat.page.wishlist.chosen_show_page"

bitbag_wishlist_plugin.behat.context.cli.wishlist:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Context\Cli\WishlistContext
arguments:
- '@kernel'

bitbag_wishlist_plugin.behat.context.common.wishlist:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Context\Common\WishlistContext
arguments:
- '@bitbag_sylius_wishlist_plugin.repository.wishlist'

bitbag_sylius_cms_plugin.behat.page.shop.wishlist:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\WishlistPage
parent: sylius.behat.symfony_page
Expand Down
1 change: 1 addition & 0 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
imports:
- suites/ui/ui_wishlist.yml
- suites/api/api_wishlist.yml
- suites/cli/cli_wishlist.yml
17 changes: 17 additions & 0 deletions tests/Behat/Resources/suites/cli/cli_wishlist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default:
suites:
cli_wishlist:
contexts:
- sylius.behat.context.hook.doctrine_orm
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.product
- sylius.behat.context.setup.user
- sylius.behat.context.transform.lexical

- bitbag_wishlist_plugin.behat.context.cli.wishlist
- bitbag_wishlist_plugin.behat.context.common.wishlist
- bitbag_sylius_cms_plugin.behat.context.ui.wishlist
- bitbag_sylius_cms_plugin.behat.context.setup.wishlist

filters:
tags: "@cli_wishlist&&@cli"

0 comments on commit d53fe47

Please sign in to comment.