Completed
Push — master ( fff6ad...32c587 )
by Christian
03:34
created

AbstractProviderTest::setUp()   C

Complexity

Conditions 9
Paths 1

Size

Total Lines 62
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 62
rs 6.6867

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 Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Tests\Provider;
13
14
use Sonata\AdminBundle\Form\FormMapper;
15
use Sonata\MediaBundle\Provider\MediaProviderInterface;
16
use Symfony\Component\Form\FormBuilder;
17
use Symfony\Component\Form\FormTypeInterface;
18
19
/**
20
 * @author Virgile Vivier <[email protected]>
21
 */
22
abstract class AbstractProviderTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var FormBuilder
26
     */
27
    protected $formBuilder;
28
29
    /**
30
     * @var FormMapper
31
     */
32
    protected $formMapper;
33
34
    /**
35
     * @var FormTypeInterface
36
     */
37
    protected $formType;
38
39
    /**
40
     * @var MediaProviderInterface
41
     */
42
    protected $provider;
43
44
    public function setUp()
45
    {
46
        // NEXT_MAJOR: Hack for php 5.3 only, remove it when requirement of PHP is >= 5.4
47
        $that = $this;
48
49
        $this->formMapper = $this->getMockBuilder('Sonata\AdminBundle\Form\FormMapper')->disableOriginalConstructor()->getMock();
50
        $this->formMapper
51
            ->expects($this->any())
52
            ->method('add')
53
            ->will($this->returnCallback(function ($name, $type = null) use ($that) {
54
                // NEXT_MAJOR: Remove this "if" (when requirement of Symfony is >= 2.8)
55
                if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
56
                    if (null !== $type) {
57
                        $isFQCN = class_exists($type);
58
                        if (!$isFQCN && method_exists('Symfony\Component\Form\AbstractType', 'getName')) {
59
                            // 2.8
60
                            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
                                sprintf(
62
                                    'Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0.'
63
                                    .' Use the fully-qualified type class name instead.',
64
                                    $type
65
                                ),
66
                                E_USER_DEPRECATED)
67
                            ;
68
                        }
69
70
                        $that->assertTrue($isFQCN, sprintf('Unable to ensure %s is a FQCN', $type));
71
                    }
72
                }
73
            }));
74
75
        $this->formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')->disableOriginalConstructor()->getMock();
76
        $this->formBuilder
77
            ->expects($this->any())
78
            ->method('add')
79
            ->will($this->returnCallback(function ($name, $type = null) use ($that) {
80
                // NEXT_MAJOR: Remove this "if" (when requirement of Symfony is >= 2.8)
81
                if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
82
                    if (null !== $type) {
83
                        $isFQCN = class_exists($type);
84
                        if (!$isFQCN && method_exists('Symfony\Component\Form\AbstractType', 'getName')) {
85
                            // 2.8
86
                            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
                                sprintf(
88
                                    'Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0.'
89
                                    .' Use the fully-qualified type class name instead.',
90
                                    $type
91
                                ),
92
                                E_USER_DEPRECATED)
93
                            ;
94
                        }
95
96
                        $that->assertTrue($isFQCN, sprintf('Unable to ensure %s is a FQCN', $type));
97
                    }
98
                }
99
            }));
100
101
102
        $this->formBuilder->expects($this->any())->method('getOption')->willReturn('api');
103
104
        $this->provider = $this->getProvider();
105
    }
106
107
    /**
108
     * Get the provider which have to be tested.
109
     */
110
    abstract public function getProvider();
111
112
    public function testBuildEditForm()
113
    {
114
        $this->provider->buildEditForm($this->formMapper);
115
    }
116
117
    public function testBuildCreateForm()
118
    {
119
        $this->provider->buildCreateForm($this->formMapper);
120
    }
121
122
    public function testBuildMediaType()
123
    {
124
        $this->provider->buildMediaType($this->formBuilder);
125
    }
126
}
127