GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( aa4ff3...5266c0 )
by Alex
22:35
created

ActionSchedulerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 9.9 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 10
loc 101
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 5 1
B testSchedulerSchedulesCorrectJob() 10 36 3
A scheduledActionProvider() 0 9 1
A configureEmToExpectNewJobCreation() 0 21 1
A setupSchedulerContext() 0 17 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
 * This file is part of the Global Trading Technologies Ltd workflow-extension-bundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * (c) fduch <[email protected]>
9
 * @date   09.08.16
10
 */
11
12
namespace Gtt\Bundle\WorkflowExtensionsBundle\Tests\Schedule;
13
14
use Carbon\Carbon;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Gtt\Bundle\WorkflowExtensionsBundle\Command\ExecuteActionCommand;
17
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository;
18
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob;
19
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ActionScheduler;
20
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ValueObject\ScheduledAction;
21
use Gtt\Bundle\WorkflowExtensionsBundle\WorkflowContext;
22
use JMS\JobQueueBundle\Entity\Job;
23
use Psr\Log\LoggerInterface;
24
use Symfony\Component\Workflow\Workflow;
25
26
class ActionSchedulerTest extends \PHPUnit_Framework_TestCase
27
{
28
    public function tearDown()
29
    {
30
        parent::tearDown();
31
        Carbon::setTestNow();
32
    }
33
34
    /**
35
     * @dataProvider scheduledActionProvider
36
     */
37
    public function testSchedulerSchedulesCorrectJob(ScheduledAction $action, $alreadyScheduled)
38
    {
39
        /** @var WorkflowContext $workflowContext */
40
        list($workflowContext, $repository, $em) = $this->setupSchedulerContext();
41
42
        Carbon::setTestNow(Carbon::now());
43
        $now             = Carbon::now();
44
45
        $executeJobAfter = clone $now;
46
        $executeJobAfter->add($action->getOffset());
47
48
        if ($action->isReschedulable()) {
49
            if ($alreadyScheduled) {
50
                // expecting rescheduling
51
                $actionJob    = new Job('command', ['--some' => 'args']);
52
                $scheduledJob = new ScheduledJob($actionJob);
53
54
                $expectedActionJob = clone $actionJob;
55
                $expectedActionJob->setExecuteAfter($executeJobAfter);
56
57
                $repository->expects(self::once())->method('findScheduledJobToReschedule')->willReturn($scheduledJob);
58
                $em->expects(self::once())->method('persist')->with(self::equalTo($expectedActionJob));
59 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
                // expecting the new one
61
                $repository->expects(self::once())->method('findScheduledJobToReschedule')->willReturn(null);
62
                $this->configureEmToExpectNewJobCreation($em, $action, $workflowContext, $executeJobAfter);
63
            }
64 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            // non-reschedulable action - expecting creation of the new job
66
            $repository->expects(self::never())->method('findScheduledJobToReschedule')->willReturn(null);
67
            $this->configureEmToExpectNewJobCreation($em, $action, $workflowContext, $executeJobAfter);
68
        }
69
70
        $scheduler = new ActionScheduler($em, $this->getMock(LoggerInterface::class));
71
        $scheduler->scheduleAction($workflowContext, $action);
72
    }
73
74
    public function scheduledActionProvider()
75
    {
76
        return [
77
            [new ScheduledAction('a1', [], 'PT1S'), true],
78
            [new ScheduledAction('a1', [], 'PT1S'), false],
79
            [new ScheduledAction('a1', [], 'PT1S', true), true],
80
            [new ScheduledAction('a1', [], 'PT1S', true), false]
81
        ];
82
    }
83
84
    private function configureEmToExpectNewJobCreation(ObjectManager $em, ScheduledAction $action, WorkflowContext $workflowContext, \DateTime $executeJobAfter)
85
    {
86
        $expectedActionJob = new Job(
87
            ExecuteActionCommand::COMMAND_NAME,
88
            [
89
                '--action='       . $action->getName(),
90
                '--arguments='    . json_encode($action->getArguments()),
91
                '--workflow='     . $workflowContext->getWorkflow()->getName(),
92
                '--subjectClass=' . get_class($workflowContext->getSubject()),
93
                '--subjectId='    . $workflowContext->getSubjectId()
94
            ]
95
        );
96
97
        $expectedActionJob->setExecuteAfter($executeJobAfter);
98
99
        /** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
100
        $em->expects(self::exactly(2))->method('persist')->withConsecutive(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Persistence\ObjectManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
101
            self::equalTo($expectedActionJob),
102
            self::equalTo(new ScheduledJob($expectedActionJob, $action->isReschedulable()))
103
        );
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    private function setupSchedulerContext()
110
    {
111
        $subject = new \StdClass();
112
        $subjectId = '1';
113
114
        $workflow = $this->getMockBuilder(Workflow::class)->disableOriginalConstructor()->getMock();
115
        $workflow->expects(self::any())->method('getName')->willReturn('w1');
116
117
        $workflowContext = new WorkflowContext($workflow, $subject, $subjectId);
118
119
        $repository = $this->getMockBuilder(ScheduledJobRepository::class)->disableOriginalConstructor()->getMock();
120
121
        $em = $this->getMockBuilder(ObjectManager::class)->disableOriginalConstructor()->getMock();
122
        $em->expects(self::once())->method('getRepository')->with(ScheduledJob::class)->willReturn($repository);
123
124
        return [$workflowContext, $repository, $em];
125
    }
126
}
127