Test Failed
Pull Request — master (#17)
by Denis
10:14
created

ConfigConverterTest::testApply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 47
nc 1
nop 0
dl 0
loc 64
rs 9.1563
c 0
b 0
f 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 declare(strict_types=1);
2
3
namespace Tests\Unit\Artprima\QueryFilterBundle\ParamConverter;
4
5
use Artprima\QueryFilterBundle\ParamConverter\ConfigConverter;
6
use Artprima\QueryFilterBundle\QueryFilter\Config\BaseConfig;
7
use Doctrine\Persistence\ManagerRegistry;
8
use Doctrine\Persistence\ObjectRepository;
9
use Doctrine\ORM\EntityManager;
10
use PHPUnit\Framework\TestCase;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
12
use Symfony\Component\HttpFoundation\Request as HttpRequest;
13
14
/**
15
 * Class ConfigConverterTest
16
 *
17
 * @author Denis Voytyuk <[email protected]>
18
 */
19
class ConfigConverterTest extends TestCase
20
{
21
    public function testSupports()
22
    {
23
        $manager = $this->getMockBuilder(EntityManager::class)
24
            ->disableOriginalConstructor()
25
            ->getMock();
26
27
        $registry = $this->getMockBuilder(ManagerRegistry::class)
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        $registry
32
            ->expects($this->once())
33
            ->method('getManagers')
34
            ->willReturn([$manager]);
35
36
        $configuration = new ParamConverter([]);
37
        $configuration->setClass(BaseConfig::class);
38
39
        $converter = new ConfigConverter($registry);
40
        self::assertTrue($converter->supports($configuration));
41
    }
42
43
    public function testApply()
44
    {
45
        $repo = $this->getMockBuilder(ObjectRepository::class)
46
            ->disableOriginalConstructor()
47
            ->setMethods(['getData', 'findAll', 'findBy', 'findOneBy', 'getClassName', 'find'])
48
            ->getMock();
49
50
        $manager = $this->getMockBuilder(EntityManager::class)
51
            ->disableOriginalConstructor()
52
            ->getMock();
53
54
        $manager
55
            ->expects($this->once())
56
            ->method('getRepository')
57
            ->with('DummyClass')
58
            ->willReturn($repo);
59
60
        $registry = $this->getMockBuilder(ManagerRegistry::class)
61
            ->disableOriginalConstructor()
62
            ->getMock();
63
64
        $registry
65
            ->expects($this->once())
66
            ->method('getManagerForClass')
67
            ->with('DummyClass')
68
            ->willReturn($manager);
69
70
        $configuration = new ParamConverter([]);
71
        $configuration->setName('dummy');
72
        $configuration->setClass(BaseConfig::class);
73
        $configuration->setOptions([
74
            'entity_manager' => null,
75
            'entity_class' => 'DummyClass',
76
            'repository_method' => 'getData',
77
        ]);
78
79
        $converter = new ConfigConverter($registry);
80
81
82
        $request = new HttpRequest([
83
            'limit' => 100,
84
            'page'=> 3,
85
            'filter' => [
86
                'c.dummy' => 'the road to hell',
87
            ],
88
            'sortby' => 'c.id',
89
            'sortdir' => 'asc',
90
        ]);
91
92
        $result = $converter->apply($request, $configuration);
93
94
        $this->assertIsObject($request->attributes->get('dummy'));
95
        /** @var BaseConfig $v */
96
        $v = $request->attributes->get('dummy');
97
        $this->assertInstanceOf(BaseConfig::class, $v);
98
        $this->assertEquals(100, $v->getRequest()->getLimit());
99
        $this->assertEquals(3, $v->getRequest()->getPageNum());
100
        $this->assertEquals(['c.dummy' => 'the road to hell'], $v->getRequest()->getQuery());
101
        $this->assertEquals('c.id', $v->getRequest()->getSortBy());
102
        $this->assertEquals('asc', $v->getRequest()->getSortDir());
103
        $this->assertEquals('asc', $v->getRequest()->getSortDir());
104
        $this->assertIsCallable($v->getRepositoryCallback());
105
106
        $this->assertTrue($result);
107
    }
108
}
109