Issues (3627)

Unit/Controller/AbstractFormControllerTest.php (4 issues)

1
<?php
2
3
namespace Mautic\CoreBundle\Tests\Unit\Controller;
4
5
use Mautic\CoreBundle\Controller\AbstractFormController;
6
use Symfony\Component\Form\Form;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class AbstractFormControllerTest extends \PHPUnit\Framework\TestCase
11
{
12
    /**
13
     * @var \PHPUnit\Framework\MockObject\MockObject|AbstractFormController
14
     */
15
    private $classFromAbstractFormController;
16
17
    /**
18
     * @var \PHPUnit\Framework\MockObject\MockObject|ParameterBag
19
     */
20
    private $parameterBagMock;
21
22
    /**
23
     * @var \PHPUnit\Framework\MockObject\MockObject|Request
24
     */
25
    private $requestMock;
26
27
    /**
28
     * @var \PHPUnit\Framework\MockObject\MockObject|Form
29
     */
30
    private $formMock;
31
32
    /**
33
     * Create a new instance from the AbstractFormController Class and creates mocks.
34
     */
35
    protected function setUp(): void
36
    {
37
        $this->classFromAbstractFormController = new class() extends AbstractFormController {
38
            public function returnIsFormCancelled(Form $form): bool
39
            {
40
                return $this->isFormCancelled($form);
41
            }
42
        };
43
        $this->parameterBagMock     = $this->createMock(ParameterBag::class);
44
        $this->requestMock          = $this->createMock(Request::class);
45
        $this->requestMock->request = $this->parameterBagMock;
0 ignored issues
show
Accessing request on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
        $this->formMock             = $this->createMock(Form::class);
47
    }
48
49
    /**
50
     * Test to send a Form that does not have an array representation in request.
51
     */
52
    public function testIsFormCancelledWhenFormArrayNull(): void
53
    {
54
        $this->parameterBagMock->method('get')
0 ignored issues
show
The method method() does not exist on Symfony\Component\HttpFoundation\ParameterBag. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $this->parameterBagMock->/** @scrutinizer ignore-call */ 
55
                                 method('get')

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...
55
            ->with('company')
56
            ->willReturn(null);
57
        $this->classFromAbstractFormController->setRequest($this->requestMock);
58
        $this->formMock->method('getName')
0 ignored issues
show
The method method() does not exist on Symfony\Component\Form\Form. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $this->formMock->/** @scrutinizer ignore-call */ 
59
                         method('getName')

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...
59
            ->willReturn('company');
60
        $isFormCancelled = $this->classFromAbstractFormController->returnIsFormCancelled($this->formMock);
0 ignored issues
show
The method returnIsFormCancelled() does not exist on Mautic\CoreBundle\Contro...\AbstractFormController. It seems like you code against a sub-type of Mautic\CoreBundle\Contro...\AbstractFormController such as anonymous//app/bundles/C...ormControllerTest.php$0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        /** @scrutinizer ignore-call */ 
61
        $isFormCancelled = $this->classFromAbstractFormController->returnIsFormCancelled($this->formMock);
Loading history...
61
        $this->assertFalse($isFormCancelled);
62
    }
63
64
    /**
65
     * Test to send a Form that has an array representation in request. And the cancel button was clicked.
66
     */
67
    public function testIsFormCancelledWhenCancelled(): void
68
    {
69
        $this->parameterBagMock->method('get')
70
            ->with('company_merge')
71
            ->willReturn(['buttons' => ['cancel' => null]]);
72
        $this->classFromAbstractFormController->setRequest($this->requestMock);
73
        $this->formMock->method('getName')
74
            ->willReturn('company_merge');
75
        $isFormCancelled = $this->classFromAbstractFormController->returnIsFormCancelled($this->formMock);
76
        $this->assertTrue($isFormCancelled);
77
    }
78
79
    /**
80
     * Test to send a Form that has an array representation in request. And the submit button was clicked.
81
     */
82
    public function testIsFormCancelledWhenNotCancelled(): void
83
    {
84
        $this->parameterBagMock->method('get')
85
            ->with('company_merge')
86
            ->willReturn(['buttons' => ['submit' => null]]);
87
        $this->classFromAbstractFormController->setRequest($this->requestMock);
88
        $this->formMock->method('getName')
89
            ->willReturn('company_merge');
90
        $isFormCancelled = $this->classFromAbstractFormController->returnIsFormCancelled($this->formMock);
91
        $this->assertFalse($isFormCancelled);
92
    }
93
}
94