Completed
Push — master ( bba9f0...4317fb )
by Matthew
08:14
created

QueueControllerTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
use Symfony\Component\Templating\TemplateNameParser;
27
use Twig\Environment;
28
29
class QueueControllerTest extends TestCase
30
{
31
    public static function setUpBeforeClass()
32
    {
33
        JobManagerTest::setUpBeforeClass();
34
    }
35
36 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...
37
    {
38
        $container = $this->getContainer();
39
        $queueController = new QueueController();
40
        $queueController->setContainer($container);
41
        $response = $queueController->statusAction();
42
        self::assertArrayHasKey('status', $response);
43
        self::assertArrayHasKey('css', $response);
44
        self::assertArrayHasKey('js', $response);
45
    }
46
47
    protected function getContainer()
48
    {
49
        $container = new Container();
50
        $container->setParameter('dtc_grid.theme.css', []);
51
        $container->setParameter('dtc_grid.theme.js', []);
52
        $container->setParameter('dtc_grid.jquery', []);
53
        $container->setParameter('dtc_queue.class_job', Job::class);
54
        $container->setParameter('dtc_queue.class_job_archive', JobArchive::class);
55
        $container->setParameter('dtc_queue.class_run', Run::class);
56
        $container->setParameter('dtc_queue.class_run_archive', RunArchive::class);
57
        $container->setParameter('dtc_queue.admin.chartjs', '');
58
        $container->setParameter('dtc_queue.default_manager', 'orm');
59
        $container->set('dtc_queue.job_manager', JobManagerTest::$jobManager);
60
        $container->set('dtc_queue.worker_manager', new WorkerManager(JobManagerTest::$jobManager, new EventDispatcher()));
61
        $rendererFactory = new RendererFactory(
62
            new Router(new YamlFileLoader(new FileLocator(__DIR__)), 'test.yml'),
63
            [
64
                'theme.css' => [],
65
                'theme.js' => [],
66
                'page_div_style' => 'somestyle',
67
                'jquery' => [],
68
                'purl' => [],
69
                'datatables.css' => [],
70
                'datatables.js' => [],
71
                'jq_grid.css' => [],
72
                'jq_grid.js' => [], ]
73
        );
74
        $twigEngine = new TwigEngine(new Environment(new \Twig_Loader_Array()), new TemplateNameParser(), new FileLocator(__DIR__));
75
        $rendererFactory->setTwigEngine($twigEngine);
76
        $container->set('dtc_grid.renderer.factory', $rendererFactory);
77
        $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...
78
        $container->set('dtc_queue.grid_source.jobs_waiting.orm', $liveJobsGridSource);
79
        $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...
80
        $liveJobsGridSource->setRunning(true);
81
        $container->set('dtc_queue.grid_source.jobs_running.orm', $liveJobsGridSource);
82
        $container->set('dtc_queue.job_manager', JobManagerTest::$jobManager);
83
        $gridSourceManager = new GridSourceManager(new AnnotationReader(), __DIR__);
84
        $container->set('dtc_grid.manager.source', $gridSourceManager);
85
        $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...
86
87
        return $container;
88
    }
89
90 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...
91
    {
92
        $container = $this->getContainer();
93
        $queueController = new QueueController();
94
        $queueController->setContainer($container);
95
        $response = $queueController->jobsAction();
96
        self::assertArrayHasKey('css', $response);
97
        self::assertArrayHasKey('js', $response);
98
    }
99
100 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...
101
    {
102
        $container = $this->getContainer();
103
        $queueController = new QueueController();
104
        $queueController->setContainer($container);
105
        $response = $queueController->runningJobsAction();
106
        self::assertArrayHasKey('css', $response);
107
        self::assertArrayHasKey('js', $response);
108
    }
109
110 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...
111
    {
112
        $container = $this->getContainer();
113
        $queueController = new QueueController();
114
        $queueController->setContainer($container);
115
        $response = $queueController->workersAction();
116
        self::assertArrayHasKey('workers', $response);
117
        self::assertArrayHasKey('css', $response);
118
        self::assertArrayHasKey('js', $response);
119
    }
120
121
    public function testArchiveJobsAction()
122
    {
123
        $container = $this->getContainer();
124
        $queueController = new QueueController();
125
        $queueController->setContainer($container);
126
        $response = $queueController->archiveAction(new Request());
127
        self::assertNotNull($response);
128
        self::assertTrue($response instanceof StreamedResponse);
129
    }
130
}
131