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; |
|
|
|
|
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') |
|
|
|
|
55
|
|
|
->with('company') |
56
|
|
|
->willReturn(null); |
57
|
|
|
$this->classFromAbstractFormController->setRequest($this->requestMock); |
58
|
|
|
$this->formMock->method('getName') |
|
|
|
|
59
|
|
|
->willReturn('company'); |
60
|
|
|
$isFormCancelled = $this->classFromAbstractFormController->returnIsFormCancelled($this->formMock); |
|
|
|
|
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
|
|
|
|