shouldThrowAnExceptionIfJsonIsMalformed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Phake_IMock>.

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...
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');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$commands was never initialized. Although not strictly required by PHP, it is generally a good practice to add $commands = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
85
        $return     = $this->formHandler->process($this->request, (new ConsoleApplication())->setCommands($commands));
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Phake_IMock>.

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...
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')]));
0 ignored issues
show
Documentation introduced by
$this->form is of type object<Phake_IMock>, but the function expects a object<Symfony\Component\Form\FormInterface>.

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...
98
        \Phake::when($this->form)->isValid()->thenReturn(false);
99
        \Phake::when($this->form)->isSubmitted()->thenReturn(true);
100
101
        $commands[] = (new Command())->setDefinition('amf:test');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$commands was never initialized. Although not strictly required by PHP, it is generally a good practice to add $commands = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
102
        $return     = $this->formHandler->process($this->request, (new ConsoleApplication())->setCommands($commands));
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Phake_IMock>.

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...
103
104
        $this->assertFalse($return['success']);
105
        $this->assertNotEmpty($return['errors']);
106
    }
107
}
108