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 ( ccb5b0...806400 )
by Alex
02:59
created

TransitionSchedulerTest::setupSchedulerContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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
15
use Carbon\Carbon;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\Repository\ScheduledJobRepository;
18
use Gtt\Bundle\WorkflowExtensionsBundle\Entity\ScheduledJob;
19
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\ScheduledTransition;
20
use Gtt\Bundle\WorkflowExtensionsBundle\Schedule\TransitionScheduler;
21
use Gtt\Bundle\WorkflowExtensionsBundle\SubjectManipulator;
22
use JMS\JobQueueBundle\Entity\Job;
23
use Psr\Log\LoggerInterface;
24
25
class TransitionSchedulerTest extends \PHPUnit_Framework_TestCase
26
{
27
    public function tearDown()
28
    {
29
        parent::tearDown();
30
        Carbon::setTestNow();
31
    }
32
33
    public function testInternalExceptionsDoNotBlockExecution()
34
    {
35
        list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext();
0 ignored issues
show
Unused Code introduced by
The assignment to $subjectClass is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $subjectId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
36
37
        $transition1 = new ScheduledTransition('t1', 'PT1S');
38
        $transition2 = new ScheduledTransition('t2', 'PT2S');
39
40
        $logger = $this->getMock(LoggerInterface::class);
41
        $logger->expects(self::exactly(2))->method('error');
42
43
        /** @var $repository \PHPUnit_Framework_MockObject_MockObject */
44
        $repository
45
            ->expects(self::at(0))
46
            ->method('findScheduledJobToReschedule')
47
            ->willThrowException(new \Exception())
48
        ;
49
50
        $repository
51
            ->expects(self::at(1))
52
            ->method('findScheduledJobToReschedule')
53
            ->willReturn(null)
54
        ;
55
        /** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
56
        $em->expects(self::once())->method('persist')->willThrowException(new \Exception());
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...
57
58
        $scheduler = new TransitionScheduler($em, $subjectManipulator, $logger);
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->setupSchedulerContext() on line 35 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Gtt\Bundle\WorkflowExten...cheduler::__construct() does only seem to accept object<Doctrine\Common\Persistence\ObjectManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
        $scheduler->scheduleTransitions($subject, $workflowName, [$transition1, $transition2]);
60
    }
61
62
    public function testSchedulerSchedulesCorrectJob()
63
    {
64
        list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext();
0 ignored issues
show
Unused Code introduced by
The assignment to $repository is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
65
66
        $transition = new ScheduledTransition('t1', 'PT1S');
67
68
        $transitionTriggerJobArgs = [
69
            '--transition='   . $transition->getTransitionName(),
70
            '--workflow='     . $workflowName,
71
            '--subjectClass=' . $subjectClass,
72
            '--subjectId='    . $subjectId,
73
        ];
74
75
        Carbon::setTestNow(Carbon::now());
76
        $now = Carbon::now();
77
        $executeJobAfter = $now->add($transition->getOffset());
78
79
        /** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
80
        $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...
81
            [self::callback(
82
                function (Job $job) use ($transitionTriggerJobArgs, $executeJobAfter) {
83
                    return
84
                        $job->getArgs() == $transitionTriggerJobArgs &&
85
                        $job->getExecuteAfter() == $executeJobAfter;
86
                }
87
            )],
88
            [self::callback(
89
                function (ScheduledJob $job) {
90
                    return
91
                        $job->getReschedulable() == true && $job->getJob() instanceof Job;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
                }
93
            )]
94
        );
95
96
        $scheduler = new TransitionScheduler($em, $subjectManipulator, $this->getMock(LoggerInterface::class));
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->setupSchedulerContext() on line 64 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Gtt\Bundle\WorkflowExten...cheduler::__construct() does only seem to accept object<Doctrine\Common\Persistence\ObjectManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
        $scheduler->scheduleTransitions($subject, $workflowName, [$transition]);
98
    }
99
100
    public function testSchedulerReschedulesCorrectJob()
101
    {
102
        list($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em) = $this->setupSchedulerContext();
0 ignored issues
show
Unused Code introduced by
The assignment to $subjectClass is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $subjectId is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
103
104
        $transition = new ScheduledTransition('t1', 'PT1S');
105
106
        Carbon::setTestNow(Carbon::now());
107
        $now = Carbon::now();
108
109
        $transitionTriggerJob = new Job('command');
110
        $newExecuteJobAfter   = $now->add($transition->getOffset());
111
112
        $scheduledJob = new ScheduledJob($transitionTriggerJob);
113
114
        $repository
115
            ->expects(self::once())
116
            ->method('findScheduledJobToReschedule')
117
            ->willReturn($scheduledJob)
118
        ;
119
120
        /** @var $em \PHPUnit_Framework_MockObject_MockObject|ObjectManager */
121
        $em->expects(self::once())->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...
122
            [self::callback(
123
                function (Job $job) use ($newExecuteJobAfter) {
124
                    return $job->getExecuteAfter() == $newExecuteJobAfter;
125
                }
126
            )]
127
        );
128
129
        $scheduler = new TransitionScheduler($em, $subjectManipulator, $this->getMock(LoggerInterface::class));
0 ignored issues
show
Bug introduced by
It seems like $em defined by $this->setupSchedulerContext() on line 102 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Gtt\Bundle\WorkflowExten...cheduler::__construct() does only seem to accept object<Doctrine\Common\Persistence\ObjectManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
        $scheduler->scheduleTransitions($subject, $workflowName, [$transition]);
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    private function setupSchedulerContext()
137
    {
138
        $subject = new \StdClass();
139
        $subjectClass = get_class($subject);
140
        $subjectId = '1';
141
142
        $workflowName = 'w1';
143
144
        $subjectManipulator = $this->getMockBuilder(SubjectManipulator::class)->disableOriginalConstructor()->getMock();
145
        $subjectManipulator->expects(self::once())->method('getSubjectId')->with($subject)->willReturn($subjectId);
146
147
        $repository = $this->getMockBuilder(ScheduledJobRepository::class)->disableOriginalConstructor()->getMock();
148
149
        $em = $this->getMockBuilder(ObjectManager::class)->disableOriginalConstructor()->getMock();
150
        $em->expects(self::once())->method('getRepository')->with(ScheduledJob::class)->willReturn($repository);
151
152
        return array($subject, $subjectClass, $subjectId, $workflowName, $subjectManipulator, $repository, $em);
153
    }
154
}
155