Completed
Push — master ( 1671b1...4e9462 )
by Kamil
23:39
created

PromotionContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 13
c 4
b 2
f 1
lcom 1
cbo 5
dl 0
loc 128
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A iTryToDeleteCoupon() 0 10 2
A iDeleteCoupon() 0 5 1
A couponShouldNotExistInTheRegistry() 0 4 1
A iShouldBeNotifiedOfFailure() 0 6 1
A couponShouldStillExistInTheRegistry() 0 4 1
A iTryToDeletePromotion() 0 10 2
A iDeletePromotion() 0 5 1
A promotionShouldNotExistInTheRegistry() 0 4 1
A promotionShouldStillExistInTheRegistry() 0 4 1
A iShouldBeNotifiedOfSuccess() 0 4 1
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\Test\Services\SharedStorageInterface;
17
use Sylius\Component\Promotion\Model\CouponInterface;
18
use Sylius\Component\Promotion\Model\PromotionInterface;
19
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
22
/**
23
 * @author Jan Góralski <[email protected]>
24
 */
25
final class PromotionContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var PromotionRepositoryInterface
34
     */
35
    private $promotionRepository;
36
37
    /**
38
     * @var RepositoryInterface
39
     */
40
    private $couponRepository;
41
42
    /**
43
     * @param SharedStorageInterface $sharedStorage
44
     * @param PromotionRepositoryInterface $promotionRepository
45
     * @param RepositoryInterface $couponRepository
46
     */
47
    public function __construct(
48
        SharedStorageInterface $sharedStorage,
49
        PromotionRepositoryInterface $promotionRepository,
50
        RepositoryInterface $couponRepository
51
    ) {
52
        $this->sharedStorage = $sharedStorage;
53
        $this->promotionRepository = $promotionRepository;
54
        $this->couponRepository = $couponRepository;
55
    }
56
57
    /**
58
     * @When /^I try to delete ("([^"]+)" coupon)$/
59
     */
60
    public function iTryToDeleteCoupon(CouponInterface $coupon)
61
    {
62
        try {
63
            $this->couponRepository->remove($coupon);
64
65
            throw new \Exception(sprintf('Coupon "%s" has been removed, but it should not.', $coupon->getCode()));
66
        } catch(ForeignKeyConstraintViolationException $exception) {
67
            $this->sharedStorage->set('last_exception', $exception);
68
        }
69
    }
70
71
    /**
72
     * @When /^I delete ("([^"]+)" coupon)$/
73
     */
74
    public function iDeleteCoupon(CouponInterface $coupon)
75
    {
76
        $this->sharedStorage->set('coupon_id', $coupon->getId());
77
        $this->couponRepository->remove($coupon);
78
    }
79
80
    /**
81
     * @Then /^([^"]+ should no longer) exist in the coupon registry$/
82
     */
83
    public function couponShouldNotExistInTheRegistry($couponId)
84
    {
85
        expect($this->couponRepository->find($couponId))->toBe(null);
86
    }
87
88
    /**
89
     * @Then I should be notified that it is in use and cannot be deleted
90
     */
91
    public function iShouldBeNotifiedOfFailure()
92
    {
93
        expect($this->sharedStorage->get('last_exception'))
94
            ->toBeAnInstanceOf(ForeignKeyConstraintViolationException::class)
95
        ;
96
    }
97
98
    /**
99
     * @Then /^([^"]+) should still exist in the registry$/
100
     */
101
    public function couponShouldStillExistInTheRegistry(CouponInterface $coupon)
102
    {
103
        expect($this->couponRepository->find($coupon->getId()))->toNotBe(null);
104
    }
105
106
    /**
107
     * @When /^I try to delete (promotion "([^"]+)")$/
108
     */
109
    public function iTryToDeletePromotion(PromotionInterface $promotion)
110
    {
111
        try {
112
            $this->promotionRepository->remove($promotion);
113
114
            throw new \Exception(sprintf('Promotion "%s" has been removed, but it should not.', $promotion->getName()));
115
        } catch (ForeignKeyConstraintViolationException $exception) {
116
            $this->sharedStorage->set('last_exception', $exception);
117
        }
118
    }
119
120
    /**
121
     * @When /^I delete (promotion "([^"]+)")$/
122
     */
123
    public function iDeletePromotion(PromotionInterface $promotion)
124
    {
125
        $this->sharedStorage->set('promotion_id', $promotion->getId());
126
        $this->promotionRepository->remove($promotion);
127
    }
128
129
    /**
130
     * @Then /^([^"]+ should no longer) exist in the promotion registry$/
131
     */
132
    public function promotionShouldNotExistInTheRegistry($promotionId)
133
    {
134
        expect($this->promotionRepository->find($promotionId))->toBe(null);
135
    }
136
137
    /**
138
     * @Then promotion :promotion should still exist in the registry
139
     */
140
    public function promotionShouldStillExistInTheRegistry(PromotionInterface $promotion)
141
    {
142
        expect($this->promotionRepository->find($promotion->getId()))->toNotBe(null);
143
    }
144
145
    /**
146
     * @Then I should be notified that it has been successfully deleted
147
     */
148
    public function iShouldBeNotifiedOfSuccess()
149
    {
150
        // Not applicable in the domain scope
151
    }
152
}
153