Completed
Push — master ( 38310f...b83e6e )
by Matthew
08:14
created

RunManagerTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\ORM;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Doctrine\ORM\EntityManager;
7
use Dtc\QueueBundle\Entity\RunArchive;
8
use Dtc\QueueBundle\ORM\RunManager;
9
use PHPUnit\Framework\TestCase;
10
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
11
12
class RunManagerTest extends TestCase
13
{
14
    protected static $runManager;
15
16
    public static function setUpBeforeClass()
17
    {
18
        JobManagerTest::setUpBeforeClass();
19
        $jobManager = JobManagerTest::$jobManager;
20
        $runClass = \Dtc\QueueBundle\Entity\Run::class;
21
        $runArchiveClass = \Dtc\QueueBundle\Entity\RunArchive::class;
0 ignored issues
show
Unused Code introduced by
$runArchiveClass is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        $runManager = new \Dtc\QueueBundle\ORM\RunManager($jobManager->getObjectManager(), $runClass, RunArchive::class);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseDoctrineJobManager, Dtc\QueueBundle\Doctrine\DoctrineJobManager, 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...
23
        self::$runManager = $runManager;
24
    }
25
26
    public function testPruneStaleRuns()
27
    {
28
        /** @var RunManager $runManager */
29
        $runManager = self::$runManager;
30
        $runClass = $runManager->getRunClass();
31
        $objectManager = $runManager->getObjectManager();
32
        $runRepository = $objectManager->getRepository($runClass);
33
        self::assertEmpty($runRepository->findAll());
34
        $runArchiveRepository = $objectManager->getRepository($runManager->getRunArchiveClass());
35
        self::assertEmpty($runArchiveRepository->findAll());
36
37
        $run = new $runClass();
38
        $time = time() - 96400;
39
        $date = new \DateTime("@$time");
40
41
        $run->setStartedAt($date);
42
        $run->setLastHeartbeatAt($date);
43
        $objectManager->persist($run);
44
        $objectManager->flush($run);
45
        self::assertCount(1, $runRepository->findAll());
46
47
        $count = $runManager->pruneStalledRuns();
48
        self::assertEquals(1, $count);
49
        self::assertEmpty($runRepository->findAll());
50
        $count = $runManager->pruneStalledRuns();
51
        self::assertEquals(0, $count);
52
    }
53
54 View Code Duplication
    public function testCloseEm()
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
        /** @var RunManager $runManager */
57
        $runManager = self::$runManager;
58
59
        $factory = new LazyLoadingValueHolderFactory();
60
        /** @var ContainerInterface $container */
61
        $container = new ContainerExtended();
62
        $objectManager = $runManager->getObjectManager();
63
        $container->set(
64
            'doctrine.orm.default_entity_manager',
65
            $factory->createProxy(
66
                EntityManager::class,
67
                function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($objectManager) {
68
                    $wrappedObject = $objectManager;
69
                    $initializer = null;
70
                }
71
            )
72
        );
73
        $registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], null, 'default');
74
        $runManager->setRegistry($registry);
75
        $runManager->setEntityManagerName('default');
76
77
        $run = $runManager->runStart($start = microtime(true));
78
        $runManager->getObjectManager()->close();
79
        $runManager->runStop($run, $start);
80
    }
81
82 View Code Duplication
    public function testCloseEm2()
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...
83
    {
84
        /** @var RunManager $runManager */
85
        $runManager = self::$runManager;
86
87
        $factory = new LazyLoadingValueHolderFactory();
88
        /** @var ContainerInterface $container */
89
        $container = new ContainerExtended();
90
        $objectManager = $runManager->getObjectManager();
91
        $container->set(
92
            'doctrine.orm.default_entity_manager',
93
            $factory->createProxy(
94
                EntityManager::class,
95
                function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($objectManager) {
96
                    $wrappedObject = $objectManager;
97
                    $initializer = null;
98
                }
99
            )
100
        );
101
        $registry = new Registry($container, [], ['default' => 'doctrine.orm.default_entity_manager'], null, 'default');
102
        $runManager->setRegistry($registry);
103
        $runManager->setEntityManagerName('default');
104
105
        $run = $runManager->runStart($start = microtime(true));
106
        $runManager->getObjectManager()->close();
107
        $registry->resetManager('default');
108
        $runManager->runStop($run, $start);
109
    }
110
}
111