Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

ContactContext   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 4
dl 0
loc 121
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A iWantToRequestContact() 0 4 1
A iSpecifyTheEmail() 0 4 1
A iSpecifyTheMessage() 0 4 1
A iSendIt() 0 4 1
A iShouldBeNotifiedThatTheContactRequestHasBeenSubmittedSuccessfully() 0 7 1
A iShouldBeNotifiedThatElementIsRequired() 0 8 1
A iShouldBeNotifiedThatEmailIsInvalid() 0 8 1
A iShouldBeNotifiedThatAProblemOccuredWhileSendingTheContactRequest() 0 7 1
A assertFieldValidationMessage() 0 10 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\Ui\Shop;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\NotificationType;
16
use Sylius\Behat\Page\PageInterface;
17
use Sylius\Behat\Page\Shop\Contact\ContactPageInterface;
18
use Sylius\Behat\Service\NotificationCheckerInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Grzegorz Sadowski <[email protected]>
23
 */
24
final class ContactContext implements Context
25
{
26
    /**
27
     * @var ContactPageInterface
28
     */
29
    private $contactPage;
30
31
    /**
32
     * @var NotificationCheckerInterface
33
     */
34
    private $notificationChecker;
35
36
    /**
37
     * @param ContactPageInterface $contactPage
38
     * @param NotificationCheckerInterface $notificationChecker
39
     */
40
    public function __construct(
41
        ContactPageInterface $contactPage,
42
        NotificationCheckerInterface $notificationChecker
43
    ) {
44
        $this->contactPage = $contactPage;
45
        $this->notificationChecker = $notificationChecker;
46
    }
47
48
    /**
49
     * @When I want to request contact
50
     */
51
    public function iWantToRequestContact()
52
    {
53
        $this->contactPage->open();
54
    }
55
56
    /**
57
     * @When I specify the email as :email
58
     * @When I do not specify the email
59
     */
60
    public function iSpecifyTheEmail($email = null)
61
    {
62
        $this->contactPage->specifyEmail($email);
63
    }
64
65
    /**
66
     * @When I specify the message as :message
67
     * @When I do not specify the message
68
     */
69
    public function iSpecifyTheMessage($message = null)
70
    {
71
        $this->contactPage->specifyMessage($message);
72
    }
73
74
    /**
75
     * @When I send it
76
     * @When I try to send it
77
     */
78
    public function iSendIt()
79
    {
80
        $this->contactPage->send();
81
    }
82
83
    /**
84
     * @Then I should be notified that the contact request has been submitted successfully
85
     */
86
    public function iShouldBeNotifiedThatTheContactRequestHasBeenSubmittedSuccessfully()
87
    {
88
        $this->notificationChecker->checkNotification(
89
            'Your contact request has been submitted successfully.',
90
            NotificationType::success()
91
        );
92
    }
93
94
    /**
95
     * @Then /^I should be notified that the (email|message) is required$/
96
     */
97
    public function iShouldBeNotifiedThatElementIsRequired($element)
98
    {
99
        $this->assertFieldValidationMessage(
100
            $this->contactPage,
101
            $element,
102
            sprintf('Please enter your %s.', $element)
103
        );
104
    }
105
106
    /**
107
     * @Then I should be notified that the email is invalid
108
     */
109
    public function iShouldBeNotifiedThatEmailIsInvalid()
110
    {
111
        $this->assertFieldValidationMessage(
112
            $this->contactPage,
113
            'email',
114
            'This email is invalid.'
115
        );
116
    }
117
118
    /**
119
     * @Then I should be notified that a problem occured while sending the contact request
120
     */
121
    public function iShouldBeNotifiedThatAProblemOccuredWhileSendingTheContactRequest()
122
    {
123
        $this->notificationChecker->checkNotification(
124
            'A problem occurred while sending the contact request. Please try again later.',
125
            NotificationType::failure()
126
        );
127
    }
128
129
    /**
130
     * @param PageInterface $page
131
     * @param string $element
132
     * @param string $expectedMessage
133
     */
134
    private function assertFieldValidationMessage(PageInterface $page, $element, $expectedMessage)
135
    {
136
        $currentMessage = $page->getValidationMessageFor($element);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Behat\Page\PageInterface as the method getValidationMessageFor() does only exist in the following implementations of said interface: Sylius\Behat\Page\Shop\Contact\ContactPage.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
137
138
        Assert::same(
139
            $currentMessage,
140
            $expectedMessage,
141
            'There is a message: "%s", but should be: "%s".'
142
        );
143
    }
144
}
145