GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 20f11e...3e0ce4 )
by
unknown
13s queued 10s
created

Form/EventListener/AntiSpamTimeListenerTest.php (2 issues)

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
 * (c) Christian Gripp <[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
namespace Core23\AntiSpamBundle\Tests\Form\EventListener;
11
12
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamTimeListener;
13
use Core23\AntiSpamBundle\Provider\TimeProviderInterface;
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Prophecy\Prophecy\ObjectProphecy;
17
use Symfony\Component\Form\FormConfigInterface;
18
use Symfony\Component\Form\FormError;
19
use Symfony\Component\Form\FormEvent;
20
use Symfony\Component\Form\FormEvents;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\Translation\TranslatorInterface;
23
24
final class AntiSpamTimeListenerTest extends TestCase
25
{
26
    private $timeProvider;
27
28
    private $translator;
29
30
    protected function setUp(): void
31
    {
32
        $this->timeProvider = $this->prophesize(TimeProviderInterface::class);
33
        $this->translator   =  $this->prophesize(TranslatorInterface::class);
34
    }
35
36
    public function testGetSubscribedEvents(): void
37
    {
38
        static::assertSame([
39
            FormEvents::PRE_SUBMIT => 'preSubmit',
40
        ], AntiSpamTimeListener::getSubscribedEvents());
41
    }
42
43
    public function testPreSubmit(): void
44
    {
45
        $this->timeProvider->isValid('my-form', ['foo' => 'bar'])
46
            ->willReturn(true)
47
        ;
48
        $this->timeProvider->removeFormProtection('my-form')
49
            ->shouldBeCalled()
50
        ;
51
52
        $config = $this->prophesize(FormConfigInterface::class);
53
        $config->getOption('compound')
54
            ->willReturn(true)
55
        ;
56
57
        $form = $this->prepareForm($config, true);
58
59
        $event = $this->prophesize(FormEvent::class);
60
        $event->getForm()
61
            ->willReturn($form)
62
        ;
63
64
        $listener = new AntiSpamTimeListener(
65
            $this->timeProvider->reveal(),
66
            $this->translator->reveal(),
67
            ['foo' => 'bar']
68
        );
69
        $listener->preSubmit($event->reveal());
70
    }
71
72
    public function testPreSubmitInvalidForm(): void
73
    {
74
        $this->translator->trans('time_error', [], 'Core23AntiSpamBundle')
75
            ->willReturn('There is an error')
76
        ;
77
78
        $this->timeProvider->isValid('my-form', ['foo' => 'bar'])
79
            ->willReturn(false)
80
        ;
81
        $this->timeProvider->removeFormProtection('my-form')
82
            ->shouldBeCalled()
83
        ;
84
85
        $config = $this->prophesize(FormConfigInterface::class);
86
        $config->getOption('compound')
87
            ->willReturn(true)
88
        ;
89
90
        $form = $this->prepareForm($config, true);
91
        $form->addError(Argument::type(FormError::class))
0 ignored issues
show
\Prophecy\Argument::type...\Form\FormError::class) is of type object<Prophecy\Argument\Token\TypeToken>, but the function expects a object<Symfony\Component\Form\FormError>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
The method addError does only exist in Symfony\Component\Form\FormInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
92
            ->shouldBeCalled()
93
        ;
94
95
        $event = $this->prophesize(FormEvent::class);
96
        $event->getForm()
97
            ->willReturn($form)
98
        ;
99
100
        $listener = new AntiSpamTimeListener(
101
            $this->timeProvider->reveal(),
102
            $this->translator->reveal(),
103
            ['foo' => 'bar']
104
        );
105
        $listener->preSubmit($event->reveal());
106
    }
107
108
    public function testPreSubmitChildForm(): void
109
    {
110
        $config = $this->prophesize(FormConfigInterface::class);
111
        $config->getOption('compound')
112
            ->willReturn(false)
113
        ;
114
115
        $form = $this->prepareForm($config);
116
117
        $event = $this->prophesize(FormEvent::class);
118
        $event->getForm()
119
            ->willReturn($form)
120
        ;
121
122
        $listener = new AntiSpamTimeListener(
123
            $this->timeProvider->reveal(),
124
            $this->translator->reveal(),
125
            []
126
        );
127
        $listener->preSubmit($event->reveal());
128
129
        $this->timeProvider->removeFormProtection('my-form')
130
            ->shouldNotHaveBeenCalled()
131
        ;
132
    }
133
134
    public function testPreSubmitCompoundForm(): void
135
    {
136
        $config = $this->prophesize(FormConfigInterface::class);
137
        $config->getOption('compound')
138
            ->willReturn(true)
139
        ;
140
141
        $form = $this->prepareForm($config);
142
143
        $event = $this->prophesize(FormEvent::class);
144
        $event->getForm()
145
            ->willReturn($form)
146
        ;
147
148
        $listener = new AntiSpamTimeListener(
149
            $this->timeProvider->reveal(),
150
            $this->translator->reveal(),
151
            []
152
        );
153
        $listener->preSubmit($event->reveal());
154
155
        $this->timeProvider->removeFormProtection('my-form')
156
            ->shouldNotHaveBeenCalled()
157
        ;
158
    }
159
160
    /**
161
     * @param FormConfigInterface|ObjectProphecy $config
162
     *
163
     * @return FormInterface|ObjectProphecy
164
     */
165
    private function prepareForm($config, bool $root = false)
166
    {
167
        $form = $this->prophesize(FormInterface::class);
168
        $form->isRoot()
169
            ->willReturn($root)
170
        ;
171
        $form->getConfig()
172
            ->willReturn($config)
173
        ;
174
        $form->getName()
175
            ->willReturn('my-form')
176
        ;
177
178
        return $form;
179
    }
180
}
181