Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

Sylius/Behat/Context/Ui/Shop/ContactContext.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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