Completed
Push — master ( 5fe2ea...bba9f0 )
by Matthew
13:23
created

QueueControllerTest   B

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 36.89 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 16
dl 38
loc 103
rs 8.4614
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 4 1
A testStatusAction() 10 10 1
B getContainer() 0 43 1
A testJobsAction() 9 9 1
A testJobsRunningAction() 9 9 1
A testWorkersAction() 10 10 1
A testArchiveJobsAction() 0 9 1

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 Dtc\QueueBundle\Tests\Controller;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Dtc\GridBundle\Grid\Renderer\RendererFactory;
7
use Dtc\GridBundle\Grid\Source\EntityGridSource;
8
use Dtc\GridBundle\Manager\GridSourceManager;
9
use Dtc\QueueBundle\Controller\QueueController;
10
use Dtc\QueueBundle\Entity\Job;
11
use Dtc\QueueBundle\Entity\JobArchive;
12
use Dtc\QueueBundle\Entity\Run;
13
use Dtc\QueueBundle\Entity\RunArchive;
14
use Dtc\QueueBundle\EventDispatcher\EventDispatcher;
15
use Dtc\QueueBundle\Model\WorkerManager;
16
use Dtc\QueueBundle\ORM\LiveJobsGridSource;
17
use Dtc\QueueBundle\Tests\ORM\JobManagerTest;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Bundle\TwigBundle\TwigEngine;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\Container;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\StreamedResponse;
24
use Symfony\Component\Routing\Loader\YamlFileLoader;
25
use Symfony\Component\Routing\Router;
26
27
class QueueControllerTest extends TestCase
28
{
29
    public static function setUpBeforeClass()
30
    {
31
        JobManagerTest::setUpBeforeClass();
32
    }
33
34 View Code Duplication
    public function testStatusAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $container = $this->getContainer();
37
        $queueController = new QueueController();
38
        $queueController->setContainer($container);
39
        $response = $queueController->statusAction();
40
        self::assertArrayHasKey('status', $response);
41
        self::assertArrayHasKey('css', $response);
42
        self::assertArrayHasKey('js', $response);
43
    }
44
45
    protected function getContainer()
46
    {
47
        $container = new Container();
48
        $container->setParameter('dtc_grid.theme.css', []);
49
        $container->setParameter('dtc_grid.theme.js', []);
50
        $container->setParameter('dtc_grid.jquery', []);
51
        $container->setParameter('dtc_queue.class_job', Job::class);
52
        $container->setParameter('dtc_queue.class_job_archive', JobArchive::class);
53
        $container->setParameter('dtc_queue.class_run', Run::class);
54
        $container->setParameter('dtc_queue.class_run_archive', RunArchive::class);
55
        $container->setParameter('dtc_queue.admin.chartjs', '');
56
        $container->setParameter('dtc_queue.default_manager', 'orm');
57
        $container->set('dtc_queue.job_manager', JobManagerTest::$jobManager);
58
        $container->set('dtc_queue.worker_manager', new WorkerManager(JobManagerTest::$jobManager, new EventDispatcher()));
59
        $rendererFactory = new RendererFactory(
60
            new Router(new YamlFileLoader(new FileLocator(__DIR__)), 'test.yml'),
61
            [
62
                'theme.css' => [],
63
                'theme.js' => [],
64
                'page_div_style' => 'somestyle',
65
                'jquery' => [],
66
                'purl' => [],
67
                'datatables.css' => [],
68
                'datatables.js' => [],
69
                'jq_grid.css' => [],
70
                'jq_grid.js' => [], ]
71
        );
72
        $mockBuilder = self::getMockBuilder(TwigEngine::class);
73
        $twigEngineMock = $mockBuilder->getMock();
74
        $rendererFactory->setTwigEngine($twigEngineMock);
75
        $container->set('dtc_grid.renderer.factory', $rendererFactory);
76
        $liveJobsGridSource = new LiveJobsGridSource(JobManagerTest::$jobManager);
0 ignored issues
show
Compatibility introduced by
\Dtc\QueueBundle\Tests\O...anagerTest::$jobManager of type object<Dtc\QueueBundle\Model\JobManagerInterface> is not a sub-type of object<Dtc\QueueBundle\ORM\JobManager>. It seems like you assume a concrete implementation of the interface Dtc\QueueBundle\Model\JobManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
        $container->set('dtc_queue.grid_source.jobs_waiting.orm', $liveJobsGridSource);
78
        $liveJobsGridSource = new LiveJobsGridSource(JobManagerTest::$jobManager);
0 ignored issues
show
Compatibility introduced by
\Dtc\QueueBundle\Tests\O...anagerTest::$jobManager of type object<Dtc\QueueBundle\Model\JobManagerInterface> is not a sub-type of object<Dtc\QueueBundle\ORM\JobManager>. It seems like you assume a concrete implementation of the interface Dtc\QueueBundle\Model\JobManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
79
        $liveJobsGridSource->setRunning(true);
80
        $container->set('dtc_queue.grid_source.jobs_running.orm', $liveJobsGridSource);
81
        $container->set('dtc_queue.job_manager', JobManagerTest::$jobManager);
82
        $gridSourceManager = new GridSourceManager(new AnnotationReader(), __DIR__);
83
        $container->set('dtc_grid.manager.source', $gridSourceManager);
84
        $gridSourceManager->add(Job::class, new EntityGridSource(JobManagerTest::$jobManager->getObjectManager(), Job::class));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
85
86
        return $container;
87
    }
88
89 View Code Duplication
    public function testJobsAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $container = $this->getContainer();
92
        $queueController = new QueueController();
93
        $queueController->setContainer($container);
94
        $response = $queueController->jobsAction();
95
        self::assertArrayHasKey('css', $response);
96
        self::assertArrayHasKey('js', $response);
97
    }
98
99 View Code Duplication
    public function testJobsRunningAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $container = $this->getContainer();
102
        $queueController = new QueueController();
103
        $queueController->setContainer($container);
104
        $response = $queueController->runningJobsAction();
105
        self::assertArrayHasKey('css', $response);
106
        self::assertArrayHasKey('js', $response);
107
    }
108
109 View Code Duplication
    public function testWorkersAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $container = $this->getContainer();
112
        $queueController = new QueueController();
113
        $queueController->setContainer($container);
114
        $response = $queueController->workersAction();
115
        self::assertArrayHasKey('workers', $response);
116
        self::assertArrayHasKey('css', $response);
117
        self::assertArrayHasKey('js', $response);
118
    }
119
120
    public function testArchiveJobsAction()
121
    {
122
        $container = $this->getContainer();
123
        $queueController = new QueueController();
124
        $queueController->setContainer($container);
125
        $response = $queueController->archiveAction(new Request());
126
        self::assertNotNull($response);
127
        self::assertTrue($response instanceof StreamedResponse);
128
    }
129
}
130