CronScanCommandTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 7
dl 17
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B testExecute() 0 31 1
A createCommandTester() 17 17 2

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 Alpixel\Bundle\CronBundle\Tests\Command;
4
5
use Alpixel\Bundle\CronBundle\Command\CronScanCommand;
6
use Alpixel\Bundle\CronBundle\Tests\Fixtures\CronTestCommand;
7
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Tester\CommandTester;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
class CronScanCommandTest extends KernelTestCase
13
{
14
    private $container;
15
16
    public function setUp()
17
    {
18
        self::bootKernel();
19
        $this->container = static::$kernel->getContainer();
20
    }
21
22
    public function testExecute()
23
    {
24
        $commandTester = $this->createCommandTester($this->container);
25
26
        $exitCode = $commandTester->execute([
27
            'command' => 'cron:scan',
28
        ], [
29
            'decorated'   => false,
30
            'interactive' => false,
31
        ]);
32
33
        $this->assertEquals(0, $exitCode, 'Returns 0 in case of success');
34
        $this->assertRegExp('/Finished scanning for cron jobs/', $commandTester->getDisplay());
35
36
        $commandTester = $this->createCommandTester($this->container, [new CronTestCommand()]);
37
38
        $exitCode = $commandTester->execute([
39
            'command' => 'cron:scan',
40
        ], [
41
            'decorated'   => false,
42
            'interactive' => false,
43
        ]);
44
45
        $this->assertEquals(0, $exitCode, 'Returns 0 in case of success');
46
        $this->assertRegExp('/Finished scanning for cron jobs/', $commandTester->getDisplay());
47
48
        $entityManager = $this->container->get('doctrine.orm.entity_manager');
49
        $command = $entityManager->getRepository('CronBundle:CronJob')->findOneByCommand('my_cron_test');
50
51
        $this->assertNotNull($command, 'Can\'t find newly created command in database');
52
    }
53
54 View Code Duplication
    private function createCommandTester(ContainerInterface $container, $extraCommands = [])
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...
55
    {
56
        $application = new Application();
57
        $application->setAutoExit(false);
58
59
        $command = new CronScanCommand();
60
        $command->setContainer($container);
61
62
        foreach ($extraCommands as $extraCommand) {
63
            $extraCommand->setContainer($container);
64
            $application->add($extraCommand);
65
        }
66
67
        $application->add($command);
68
69
        return new CommandTester($application->find('cron:scan'));
70
    }
71
}
72