1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AMFConsoleBundle. |
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 Tests\Form\Handler; |
11
|
|
|
|
12
|
|
|
use AMF\ConsoleBundle\Form\Model\ConsoleApplication; |
13
|
|
|
use AMF\ConsoleBundle\Form\Handler\ConsoleApplicationFormHandler; |
14
|
|
|
use AMF\ConsoleBundle\Form\Model\Command; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
19
|
|
|
use Symfony\Component\Form\FormErrorIterator; |
20
|
|
|
use Symfony\Component\Form\FormError; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test case for ConsoleApplicationFormHandler |
24
|
|
|
* |
25
|
|
|
* @author Amine Fattouch <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ConsoleApplicationFormHandlerTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var \Phake_IMock |
31
|
|
|
*/ |
32
|
|
|
private $form; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Phake_IMock |
36
|
|
|
*/ |
37
|
|
|
private $kernel; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Phake_IMock |
41
|
|
|
*/ |
42
|
|
|
private $formHandler; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $applicationJson; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Configures current tests. |
51
|
|
|
*/ |
52
|
|
|
public function setUp() |
53
|
|
|
{ |
54
|
|
|
$this->request = \Phake::mock(Request::class); |
|
|
|
|
55
|
|
|
$this->form = \Phake::mock(FormInterface::class); |
56
|
|
|
$this->kernel = \Phake::mock(KernelInterface::class); |
57
|
|
|
$this->formHandler = \Phake::partialMock(ConsoleApplicationFormHandler::class, $this->form, $this->kernel); |
58
|
|
|
|
59
|
|
|
$this->applicationJson = '{"commands": [{"definition": "amf:test"}]}'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function shouldThrowAnExceptionIfJsonIsMalformed() |
66
|
|
|
{ |
67
|
|
|
$this->expectException(BadRequestHttpException::class); |
68
|
|
|
|
69
|
|
|
\Phake::when($this->request)->getContent()->thenReturn(null); |
70
|
|
|
|
71
|
|
|
$this->formHandler->process($this->request, new ConsoleApplication()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function shouldReturnAnArrayWithSuccessIfFormIsValid() |
78
|
|
|
{ |
79
|
|
|
\Phake::when($this->request)->getContent()->thenReturn($this->applicationJson); |
80
|
|
|
\Phake::when($this->form)->getErrors($this->anything())->thenReturn([]); |
81
|
|
|
\Phake::when($this->form)->isValid()->thenReturn(true); |
82
|
|
|
\Phake::when($this->form)->isSubmitted()->thenReturn(true); |
83
|
|
|
|
84
|
|
|
$commands[] = (new Command())->setDefinition('amf:test'); |
|
|
|
|
85
|
|
|
$return = $this->formHandler->process($this->request, (new ConsoleApplication())->setCommands($commands)); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->assertTrue($return['success']); |
88
|
|
|
$this->assertNotNull($return['output']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function shouldReturnErrorsIfFormIsInvalid() |
95
|
|
|
{ |
96
|
|
|
\Phake::when($this->request)->getContent()->thenReturn($this->applicationJson); |
97
|
|
|
\Phake::when($this->form)->getErrors($this->anything())->thenReturn(new FormErrorIterator($this->form, [new FormError('invalid')])); |
|
|
|
|
98
|
|
|
\Phake::when($this->form)->isValid()->thenReturn(false); |
99
|
|
|
\Phake::when($this->form)->isSubmitted()->thenReturn(true); |
100
|
|
|
|
101
|
|
|
$commands[] = (new Command())->setDefinition('amf:test'); |
|
|
|
|
102
|
|
|
$return = $this->formHandler->process($this->request, (new ConsoleApplication())->setCommands($commands)); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$this->assertFalse($return['success']); |
105
|
|
|
$this->assertNotEmpty($return['errors']); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: