Completed
Pull Request — master (#24)
by Quentin
03:30
created

JsonExtensionTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 20.18 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 1
cbo 6
dl 22
loc 109
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Majora\Framework\Form\Extension\Json\Tests\Functional;
4
5
use Symfony\Component\Form\Forms;
6
use Symfony\Component\Form\Form;
7
use Symfony\Component\Form\Extension\Core\Type\TextType;
8
use Symfony\Component\Form\Extension\Core\Type\FormType;
9
use Symfony\Component\HttpFoundation\Request;
10
use Majora\Framework\Form\Extension\Json\JsonExtension;
11
12
class JsonExtensionTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var Form
16
     */
17
    private $form;
18
19
    /**
20
     * @var Form
21
     */
22
    private $formWithoutJson;
23
24
    public function setUp()
25
    {
26
        $formFactory = Forms::createFormFactoryBuilder()
27
            ->addExtension(new JsonExtension())
28
            ->getFormFactory();
29
        $this->form = $formFactory
30
            ->createBuilder(
31
                FormType::class,
32
                null,
33
                ['json_format' => true]
34
                )
35
            ->add('name', TextType::class)
36
            ->add('lastname', TextType::class)
37
            ->getForm();
38
        $this->formWithoutJson = $formFactory
39
            ->createBuilder(FormType::class)
40
            ->add('name', TextType::class)
41
            ->add('lastname', TextType::class)
42
            ->getForm();
43
    }
44
45
    public function testSubmitValidJsonShouldPopulateForm()
46
    {
47
        $this->form->submit('{ "name": "test1" }');
48
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getData());
49
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getNormData());
50
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getViewData());
51
    }
52
53
    public function testSubmitInvalidJsonShouldThrowException()
54
    {
55
        $this->setExpectedExceptionRegExp(
56
          'InvalidArgumentException',
57
          '/^Invalid submitted json data, error (.*) : (.*), json : invalid json$/'
58
        );
59
        $this->form->submit('invalid json');
60
    }
61
62
    public function testFormWithoutJsonShouldWorkNormally()
63
    {
64
        $this->formWithoutJson->submit(['name' => 'test1']);
65
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getData());
66
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getNormData());
67
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getViewData());
68
    }
69
70
    public function testSubmitWithoutStringShouldThrowException()
71
    {
72
        $this->setExpectedExceptionRegExp(
73
          'InvalidArgumentException',
74
          '/^Invalid argument: the submitted variable must be a string when you enable the json_format option.$/'
75
        );
76
        $this->form->submit(['invalid json']);
77
    }
78
79
    public function testRequestWithValidJsonShouldPopulateForm()
80
    {
81
        $request = $this->getRequest('{ "name": "test1" }');
82
        $this->form->handleRequest($request);
83
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getData());
84
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getNormData());
85
        $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getViewData());
86
    }
87
88
    public function testRequestWithInvalidJsonShouldTHrowException()
89
    {
90
        $this->setExpectedExceptionRegExp(
91
          'InvalidArgumentException',
92
          '/^Invalid submitted json data, error (.*) : (.*), json : invalid json$/'
93
        );
94
        $request = $this->getRequest('invalid json');
95
        $this->form->handleRequest($request);
96
    }
97
98
    public function testRequestWithoutStringShouldThrowException()
99
    {
100
        $this->setExpectedExceptionRegExp(
101
          'InvalidArgumentException',
102
          '/^Invalid argument: the submitted variable must be a string when you enable the json_format option.$/'
103
        );
104
        $request = $this->getRequest(['test']);
105
        $this->form->handleRequest($request);
106
    }
107
108
    protected function getRequest($content)
109
    {
110
        return new Request(
111
            [],
112
            [],
113
            [],
114
            [],
115
            [],
116
            ['CONTENT_TYPE' => 'application/json'],
117
            $content
118
        );
119
    }
120
}
121