Completed
Pull Request — 3.1 (#348)
by Piotr
29:38 queued 15:17
created

AdminContext::iShouldSeeTitleAtTopBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Behat\Context;
13
14
use Behat\Gherkin\Node\TableNode;
15
use FSi\Bundle\AdminBundle\Admin\AbstractElement;
16
use FSi\Bundle\AdminBundle\Admin\Element;
17
use FSi\Bundle\AdminBundle\Admin\ManagerInterface;
18
use FSi\Bundle\AdminBundle\Behat\Page\AdminPanel;
19
use FSi\Bundle\AdminBundle\Behat\Page\Page;
20
21
class AdminContext extends AbstractContext
22
{
23
    /**
24
     * @var AdminPanel
25
     */
26
    private $adminPanelPage;
27
28
    public function __construct(AdminPanel $adminPanelPage)
29
    {
30
        $this->adminPanelPage = $adminPanelPage;
31
    }
32
33
    /**
34
     * @BeforeScenario
35
     */
36
    public function resizeWindow()
37
    {
38
        if ($this->isSeleniumDriverUsed()) {
39
            $this->getSession()->resizeWindow(1280, 1024);
40
        }
41
    }
42
43
    /**
44
     * @Given /^the following admin elements were registered$/
45
     */
46
    public function theFollowingAdminElementsWereRegistered(TableNode $table)
47
    {
48
        foreach ($table->getHash() as $serviceRow) {
49
            $id = $serviceRow['Id'];
50
            $class = $serviceRow['Class'];
51
            expect($this->getAdminManager()->hasElement($id))->toBe(true);
52
            expect($this->getAdminManager()->getElement($id))->toBeAnInstanceOf($class);
0 ignored issues
show
Bug introduced by
The method toBeAnInstanceOf() does not exist on Bossa\PhpSpec\Expect\Subject. Did you maybe mean beAnInstanceOf()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
            if (array_key_exists('Parent', $serviceRow) && !empty($serviceRow['Parent'])) {
54
                expect($this->getAdminManager()->getElement($id)->getParentId())->toBe($serviceRow['Parent']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FSi\Bundle\AdminBundle\Admin\Element as the method getParentId() does only exist in the following implementations of said interface: FSi\Bundle\AdminBundle\A...D\DependentBatchElement, FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement, FSi\Bundle\AdminBundle\A...\DependentDeleteElement, FSi\Bundle\AdminBundle\A...UD\DependentFormElement, FSi\Bundle\AdminBundle\A...UD\DependentListElement, FSi\Bundle\AdminBundle\A...DependentDisplayElement, FSi\Bundle\AdminBundle\D...n\DependentBatchElement, FSi\Bundle\AdminBundle\D...in\DependentCRUDElement, FSi\Bundle\AdminBundle\D...\DependentDeleteElement, FSi\Bundle\AdminBundle\D...DependentDisplayElement, FSi\Bundle\AdminBundle\D...in\DependentFormElement, FSi\Bundle\AdminBundle\D...in\DependentListElement, FSi\Bundle\AdminBundle\s...MyDependentBatchElement, FSi\Bundle\AdminBundle\s...\MyDependentCrudElement, FSi\Bundle\AdminBundle\s...yDependentDeleteElement, FSi\Bundle\AdminBundle\s...\MyDependentFormElement, FSi\Bundle\AdminBundle\s...\MyDependentListElement, FSi\Bundle\AdminBundle\s...xtures\MyDependentBatch, FSi\Bundle\AdminBundle\s...ixtures\MyDependentCRUD, FSi\Bundle\AdminBundle\s...ixtures\MyDependentForm, FSi\Bundle\AdminBundle\s...ixtures\MyDependentList, FSi\FixturesBundle\Admin\CategoryNews, FSi\FixturesBundle\Admin\CategoryNewsDisplay.

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...
55
            }
56
        }
57
    }
58
59
    /**
60
     * @Given /^("[^"]*" element) have following options defined$/
61
     */
62
    public function elementHaveFollowingOptionsDefined(
63
        AbstractElement $adminElement,
64
        TableNode $options
65
    ) {
66
        foreach ($options->getHash() as $optionRow) {
67
            $option = $optionRow['Option'];
68
            $value = $optionRow['Value'];
69
            expect($adminElement->hasOption($option))->toBe(true);
70
            expect($adminElement->getOption($option))->toBe($value);
71
        }
72
    }
73
74
    /**
75
     * @Given /^there are following admin elements available$/
76
     */
77
    public function thereAreFollowingAdminElementsAvailable(TableNode $table)
78
    {
79
        foreach ($table->getHash() as $elementRow) {
80
            $id = $elementRow['Id'];
81
            $name = $elementRow['Name'];
82
            expect($this->getAdminManager()->hasElement($id))->toBe(true);
83
            expect($this->getAdminManager()->getElement($id)->getName())->toBe($name);
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<FSi\Bundle\AdminBundle\Admin\Element>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        }
85
    }
86
87
    /**
88
     * @Then /^I should see "([^"]*)" title at top bar$/
89
     */
90
    public function iShouldSeeTitleAtTopBar($navbarBrandText)
91
    {
92
        expect($this->adminPanelPage->getNavbarBrandText())->toBe($navbarBrandText);
93
    }
94
95
    /**
96
     * @Given I should see :page page header :headerContent
97
     */
98
    public function iShouldSeePageHeader(Page $page, $headerContent)
99
    {
100
        expect($page->getHeader())->toBe($headerContent);
101
    }
102
103
    /**
104
     * @Given /^translations are enabled in application$/
105
     */
106
    public function translationsAreEnabledInApplication()
107
    {
108
        $translator = $this->getContainer()->get('translator');
109
        expect($translator)->toBeAnInstanceOf('Symfony\Component\Translation\TranslatorInterface');
0 ignored issues
show
Bug introduced by
The method toBeAnInstanceOf() does not exist on Bossa\PhpSpec\Expect\Subject. Did you maybe mean beAnInstanceOf()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
110
    }
111
112
    /**
113
     * @Then /^I should see language dropdown button in navigation bar with text "([^"]*)"$/
114
     * @Then /^I should see language dropdown button with text "([^"]*)"$/
115
     */
116
    public function iShouldSeeLanguageDropdownButtonInNavigationBarWithText($button)
117
    {
118
        expect($this->adminPanelPage->getLanguageDropdown()->hasLink($button))->toBe(true);
119
    }
120
121
    /**
122
     * @Given /^language dropdown button should have following links$/
123
     */
124
    public function languageDropdownButtonShouldHaveFollowingLinks(TableNode $dropdownLinks)
125
    {
126
        $links = $this->adminPanelPage->getLanguageDropdownOptions();
127
128
        foreach ($dropdownLinks->getHash() as $link) {
129
            expect($links)->toContain($link['Link']);
130
        }
131
    }
132
133
    /**
134
     * @When /^I click "([^"]*)" link from language dropdown button$/
135
     */
136
    public function iClickLinkFromLanguageDropdownButton($link)
137
    {
138
        $this->adminPanelPage->getLanguageDropdown()->clickLink($link);
139
    }
140
141
    /**
142
     * @Transform /^"([^"]*)" element/
143
     */
144
    public function transformListNameToAdminElement(string $id): Element
145
    {
146
        return $this->getAdminManager()->getElement($id);
147
    }
148
149
    /**
150
     * @Transform /^(\d+)/
151
     */
152
    public function castStringToNumber(string $number): int
153
    {
154
        return (int) $number;
155
    }
156
157
    /**
158
     * @Transform /^first|second|third$/
159
     */
160
    public function castWordToNumber(string $word): int
161
    {
162
        switch ($word) {
163
            case 'first':
164
                return 1;
165
            case 'second':
166
                return 2;
167
            case 'third':
168
                return 3;
169
        }
170
    }
171
172
    private function getAdminManager(): ManagerInterface
173
    {
174
        return $this->getContainer()->get('test.admin.manager');
175
    }
176
}
177