Completed
Push — master ( f9a038...cc9983 )
by Kamil
22:34
created

iAmLoggedInAsAnAdministrator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Domain;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
16
use Sylius\Component\Core\Model\PromotionInterface;
17
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
18
use Sylius\Component\Promotion\Model\CouponInterface;
19
use Sylius\Component\Promotion\Repository\CouponRepositoryInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
final class PromotionCouponContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var CouponRepositoryInterface
34
     */
35
    private $couponRepository;
36
37
    /**
38
     * @param SharedStorageInterface $sharedStorage
39
     * @param CouponRepositoryInterface $couponRepository
40
     */
41
    public function __construct(SharedStorageInterface $sharedStorage, CouponRepositoryInterface $couponRepository)
42
    {
43
        $this->sharedStorage = $sharedStorage;
44
        $this->couponRepository = $couponRepository;
45
    }
46
47
    /**
48
     * @When /^I try to delete ("[^"]+" coupon) related to (this promotion)$/
49
     */
50
    public function iTryToDeleteCoupon(CouponInterface $coupon, PromotionInterface $promotion)
51
    {
52
        try {
53
            $promotion->removeCoupon($coupon);
54
            $this->couponRepository->remove($coupon);
55
56
            throw new \Exception(sprintf('Coupon "%s" has been removed, but it should not.', $coupon->getCode()));
57
        } catch(ForeignKeyConstraintViolationException $exception) {
58
            $this->sharedStorage->set('last_exception', $exception);
59
        }
60
    }
61
62
    /**
63
     * @When /^I delete ("[^"]+" coupon) related to (this promotion)$/
64
     */
65
    public function iDeleteCouponRelatedTo(CouponInterface $coupon, PromotionInterface $promotion)
66
    {
67
        $this->sharedStorage->set('coupon', $coupon);
68
        $this->couponRepository->remove($coupon);
69
        $promotion->removeCoupon($coupon);
70
    }
71
72
    /**
73
     * @Then /^(this coupon) should no longer exist in the coupon registry$/
74
     */
75
    public function couponShouldNotExistInTheRegistry(CouponInterface $coupon)
76
    {
77
        Assert::null(
78
            $this->couponRepository->findOneBy(['code' => $coupon->getCode()]),
79
            sprintf('The coupon with code %s should not exist', $coupon->getCode())
80
        );
81
    }
82
83
    /**
84
     * @Then I should be notified that it is in use and cannot be deleted
85
     */
86
    public function iShouldBeNotifiedOfFailure()
87
    {
88
        expect($this->sharedStorage->get('last_exception'))
89
            ->toBeAnInstanceOf(ForeignKeyConstraintViolationException::class)
90
        ;
91
    }
92
93
    /**
94
     * @Then /^([^"]+) should still exist in the registry$/
95
     */
96
    public function couponShouldStillExistInTheRegistry(CouponInterface $coupon)
97
    {
98
        Assert::notNull(
99
            $this->couponRepository->find($coupon->getId()),
100
            sprintf('The coupon with id %s should exist', $coupon->getId())
101
        );
102
    }
103
104
    /**
105
     * @Then I should be notified that it has been successfully deleted
106
     */
107
    public function iShouldBeNotifiedOfSuccess()
108
    {
109
        // Not applicable in the domain scope
110
    }
111
112
    /**
113
     * @Given I am logged in as an administrator
114
     */
115
    public function iAmLoggedInAsAnAdministrator()
116
    {
117
        // Not applicable in the domain scope
118
    }
119
}
120