Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

ModelManagerTest::testSortParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 57
rs 9.6818
cc 1
eloc 36
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 Sonata 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\DoctrineORMAdminBundle\Tests\Model;
13
14
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
15
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
16
17
class ModelManagerTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testSortParameters()
20
    {
21
        $registry = $this->getMock('Symfony\Bridge\Doctrine\RegistryInterface');
22
23
        $manager  = new ModelManager($registry);
24
25
        $datagrid1 = $this->getMockBuilder('\Sonata\AdminBundle\Datagrid\Datagrid')->disableOriginalConstructor()->getMock();
26
        $datagrid2 = $this->getMockBuilder('\Sonata\AdminBundle\Datagrid\Datagrid')->disableOriginalConstructor()->getMock();
27
28
        $field1 = new FieldDescription();
29
        $field1->setName('field1');
30
31
        $field2 = new FieldDescription();
32
        $field2->setName('field2');
33
34
        $field3 = new FieldDescription();
35
        $field3->setName('field3');
36
        $field3->setOption('sortable', 'field3sortBy');
37
38
        $datagrid1
39
            ->expects($this->any())
40
            ->method('getValues')
41
            ->will($this->returnValue(array(
42
                '_sort_by'    => $field1,
43
                '_sort_order' => 'ASC',
44
            )));
45
46
        $datagrid2
47
            ->expects($this->any())
48
            ->method('getValues')
49
            ->will($this->returnValue(array(
50
                '_sort_by'    => $field3,
51
                '_sort_order' => 'ASC',
52
            )));
53
54
        $parameters = $manager->getSortParameters($field1, $datagrid1);
55
56
        $this->assertEquals('DESC', $parameters['filter']['_sort_order']);
57
        $this->assertEquals('field1', $parameters['filter']['_sort_by']);
58
59
        $parameters = $manager->getSortParameters($field2, $datagrid1);
60
61
        $this->assertEquals('ASC', $parameters['filter']['_sort_order']);
62
        $this->assertEquals('field2', $parameters['filter']['_sort_by']);
63
64
        $parameters = $manager->getSortParameters($field3, $datagrid1);
65
66
        $this->assertEquals('ASC', $parameters['filter']['_sort_order']);
67
        $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']);
68
69
70
71
        $parameters = $manager->getSortParameters($field3, $datagrid2);
72
73
        $this->assertEquals('DESC', $parameters['filter']['_sort_order']);
74
        $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']);
75
    }
76
}
77