LocaleTypeTest::testSubmitDefaultLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 9.7692
c 0
b 0
f 0
cc 1
eloc 41
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Locale\Tests\Form\Type;
13
14
use Lug\Component\Locale\Form\Type\LocaleCodeType;
15
use Lug\Component\Locale\Form\Type\LocaleType;
16
use Lug\Component\Locale\Model\LocaleInterface;
17
use Lug\Component\Locale\Provider\LocaleProviderInterface;
18
use Lug\Component\Resource\Factory\FactoryInterface;
19
use Lug\Component\Resource\Form\Type\AbstractResourceType;
20
use Lug\Component\Resource\Form\Type\ResourceType;
21
use Lug\Component\Resource\Model\ResourceInterface;
22
use Lug\Component\Resource\Repository\RepositoryInterface;
23
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
24
use Symfony\Component\Form\FormFactoryInterface;
25
use Symfony\Component\Form\Forms;
26
use Symfony\Component\Validator\ValidatorBuilder;
27
28
/**
29
 * @author GeLo <[email protected]>
30
 */
31
class LocaleTypeTest extends \PHPUnit_Framework_TestCase
32
{
33
    /**
34
     * @var FormFactoryInterface
35
     */
36
    private $formFactory;
37
38
    /**
39
     * @var LocaleType
40
     */
41
    private $localeType;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
45
     */
46
    private $resource;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
50
     */
51
    private $resourceFactory;
52
53
    /**
54
     * @var \PHPUnit_Framework_MockObject_MockObject|RepositoryInterface
55
     */
56
    private $repository;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject|LocaleProviderInterface
60
     */
61
    private $localeProvider;
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function setUp()
67
    {
68
        $this->resource = $this->createResourceMock();
69
        $this->resourceFactory = $this->createFactoryMock();
70
        $this->repository = $this->createRepositoryMock();
71
        $this->localeProvider = $this->createLocaleProviderMock();
72
73
        $this->resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
            ->expects($this->any())
75
            ->method('getName')
76
            ->will($this->returnValue('locale'));
77
78
        $this->localeType = new LocaleType($this->resource, $this->resourceFactory, $this->localeProvider);
79
80
        $this->formFactory = Forms::createFormFactoryBuilder()
81
            ->addType(new ResourceType())
82
            ->addType($this->localeType)
83
            ->addType(new LocaleCodeType($this->repository))
84
            ->addExtension(new ValidatorExtension((new ValidatorBuilder())->getValidator()))
85
            ->getFormFactory();
86
    }
87
88
    public function testInheritance()
89
    {
90
        $this->assertInstanceOf(AbstractResourceType::class, $this->localeType);
91
    }
92
93
    public function testSubmit()
94
    {
95
        $this->localeProvider
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Locale\Pro...LocaleProviderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
96
            ->expects($this->once())
97
            ->method('getDefaultLocale')
98
            ->will($this->returnValue($this->createLocaleMock()));
99
100
        $this->resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
101
            ->expects($this->once())
102
            ->method('getModel')
103
            ->will($this->returnValue(get_class($locale = $this->createLocaleMock())));
104
105
        $this->repository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\R...ory\RepositoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
            ->expects($this->once())
107
            ->method('findAll')
108
            ->will($this->returnValue([]));
109
110
        $this->resourceFactory
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Factory\FactoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
            ->expects($this->once())
112
            ->method('create')
113
            ->will($this->returnValue($locale));
114
115
        $locale
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Locale\Model\LocaleInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
            ->expects($this->once())
117
            ->method('setCode')
118
            ->with($this->identicalTo($code = 'fr'));
119
120
        $locale
121
            ->expects($this->once())
122
            ->method('setEnabled')
123
            ->with($this->identicalTo(true));
124
125
        $locale
126
            ->expects($this->once())
127
            ->method('setRequired')
128
            ->with($this->identicalTo(true));
129
130
        $form = $this->formFactory
131
            ->create(LocaleType::class)
132
            ->submit(['code' => $code, 'enabled' => true, 'required' => true]);
133
134
        $view = $form->createView();
135
136
        $this->assertCount(4, $view->children);
137
        $this->assertArrayHasKey('code', $view->children);
138
        $this->assertArrayHasKey('enabled', $view->children);
139
        $this->assertArrayHasKey('required', $view->children);
140
        $this->assertArrayHasKey('submit', $view->children);
141
142
        $this->assertArrayHasKey('disabled', $view->children['code']->vars);
143
        $this->assertFalse($view->children['code']->vars['disabled']);
144
145
        $this->assertArrayHasKey('disabled', $view->children['enabled']->vars);
146
        $this->assertFalse($view->children['enabled']->vars['disabled']);
147
148
        $this->assertArrayHasKey('disabled', $view->children['required']->vars);
149
        $this->assertFalse($view->children['required']->vars['disabled']);
150
    }
151
152
    public function testSubmitDefaultLocale()
153
    {
154
        $this->localeProvider
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Locale\Pro...LocaleProviderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
            ->expects($this->once())
156
            ->method('getDefaultLocale')
157
            ->will($this->returnValue($defaultLocale = $this->createLocaleMock()));
158
159
        $defaultLocale
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Locale\Model\LocaleInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
160
            ->expects($this->exactly(2))
161
            ->method('getCode')
162
            ->will($this->returnValue($defaultLocaleCode = 'fr'));
163
164
        $defaultLocale
165
            ->expects($this->never())
166
            ->method('setCode');
167
168
        $defaultLocale
169
            ->expects($this->never())
170
            ->method('setEnabled');
171
172
        $defaultLocale
173
            ->expects($this->never())
174
            ->method('setRequired');
175
176
        $this->resource
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\Model\ResourceInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
            ->expects($this->once())
178
            ->method('getModel')
179
            ->will($this->returnValue(get_class($defaultLocale)));
180
181
        $this->repository
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Lug\Component\Resource\R...ory\RepositoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
182
            ->expects($this->once())
183
            ->method('findAll')
184
            ->will($this->returnValue([$defaultLocale]));
185
186
        $form = $this->formFactory
187
            ->create(LocaleType::class, $defaultLocale)
188
            ->submit(['code' => 'USD', 'enabled' => false, 'required' => false]);
189
190
        $view = $form->createView();
191
192
        $this->assertCount(4, $view->children);
193
        $this->assertArrayHasKey('code', $view->children);
194
        $this->assertArrayHasKey('enabled', $view->children);
195
        $this->assertArrayHasKey('required', $view->children);
196
        $this->assertArrayHasKey('submit', $view->children);
197
198
        $this->assertArrayHasKey('disabled', $view->children['code']->vars);
199
        $this->assertTrue($view->children['code']->vars['disabled']);
200
201
        $this->assertArrayHasKey('disabled', $view->children['enabled']->vars);
202
        $this->assertTrue($view->children['enabled']->vars['disabled']);
203
204
        $this->assertArrayHasKey('disabled', $view->children['required']->vars);
205
        $this->assertTrue($view->children['required']->vars['disabled']);
206
    }
207
208
    /**
209
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
210
     */
211
    private function createResourceMock()
212
    {
213
        return $this->createMock(ResourceInterface::class);
214
    }
215
216
    /**
217
     * @return \PHPUnit_Framework_MockObject_MockObject|FactoryInterface
218
     */
219
    private function createFactoryMock()
220
    {
221
        return $this->createMock(FactoryInterface::class);
222
    }
223
224
    /**
225
     * @return \PHPUnit_Framework_MockObject_MockObject|RepositoryInterface
226
     */
227
    private function createRepositoryMock()
228
    {
229
        return $this->createMock(RepositoryInterface::class);
230
    }
231
232
    /**
233
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleProviderInterface
234
     */
235
    private function createLocaleProviderMock()
236
    {
237
        return $this->createMock(LocaleProviderInterface::class);
238
    }
239
240
    /**
241
     * @return \PHPUnit_Framework_MockObject_MockObject|LocaleInterface
242
     */
243
    private function createLocaleMock()
244
    {
245
        return $this->createMock(LocaleInterface::class);
246
    }
247
}
248