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

PromotionContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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\PromotionInterface;
18
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface;
19
20
/**
21
 * @author Jan Góralski <[email protected]>
22
 */
23
final class PromotionContext implements Context
24
{
25
    /**
26
     * @var SharedStorageInterface
27
     */
28
    private $sharedStorage;
29
30
    /**
31
     * @var PromotionRepositoryInterface
32
     */
33
    private $promotionRepository;
34
35
    /**
36
     * @param SharedStorageInterface $sharedStorage
37
     * @param PromotionRepositoryInterface $promotionRepository
38
     */
39
    public function __construct(
40
        SharedStorageInterface $sharedStorage,
41
        PromotionRepositoryInterface $promotionRepository
42
    ) {
43
        $this->sharedStorage = $sharedStorage;
44
        $this->promotionRepository = $promotionRepository;
45
    }
46
47
    /**
48
     * @When /^I try to delete a ("([^"]+)" promotion)$/
49
     */
50
    public function iTryToDeletePromotion(PromotionInterface $promotion)
51
    {
52
        try {
53
            $this->promotionRepository->remove($promotion);
54
55
            throw new \Exception(sprintf('Promotion "%s" has been removed, but it should not.', $promotion->getName()));
56
        } catch (ForeignKeyConstraintViolationException $exception) {
57
            $this->sharedStorage->set('last_exception', $exception);
58
        }
59
    }
60
61
    /**
62
     * @When /^I delete a ("([^"]+)" promotion)$/
63
     */
64
    public function iDeletePromotion(PromotionInterface $promotion)
65
    {
66
        $this->sharedStorage->set('promotion', $promotion);
67
        $this->promotionRepository->remove($promotion);
68
    }
69
70
    /**
71
     * @Then /^(this promotion) should no longer exist in the promotion registry$/
72
     */
73
    public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion)
74
    {
75
        expect($this->promotionRepository->findOneBy(['code' => $promotion->getCode()]))->toBe(null);
76
    }
77
78
    /**
79
     * @Then promotion :promotion should still exist in the registry
80
     */
81
    public function promotionShouldStillExistInTheRegistry(PromotionInterface $promotion)
82
    {
83
        expect($this->promotionRepository->find($promotion->getId()))->toNotBe(null);
84
    }
85
86
    /**
87
     * @Then I should be notified that it is in use and cannot be deleted
88
     */
89
    public function iShouldBeNotifiedOfFailure()
90
    {
91
        expect($this->sharedStorage->get('last_exception'))
92
            ->toBeAnInstanceOf(ForeignKeyConstraintViolationException::class)
93
        ;
94
    }
95
96
    /**
97
     * @Then I should be notified that it has been successfully deleted
98
     */
99
    public function iShouldBeNotifiedOfSuccess()
100
    {
101
        // Not applicable in the domain scope
102
    }
103
104
    /**
105
     * @Given I am logged in as an administrator
106
     */
107
    public function iAmLoggedInAsAnAdministrator()
108
    {
109
        // Not applicable in the domain scope
110
    }
111
}
112