1 | <?php |
||
18 | class CollectionTest extends \Codeception\TestCase\Test |
||
19 | { |
||
20 | /** |
||
21 | * @var \CodeGuy |
||
22 | */ |
||
23 | protected $guy; |
||
24 | |||
25 | public function testAfterFilters() |
||
26 | { |
||
27 | $collection = new Collection(); |
||
28 | $collection->setLogger(Robo::logger()); |
||
29 | |||
30 | $taskA = new CollectionTestTask('a', 'value-a'); |
||
31 | $taskB = new CollectionTestTask('b', 'value-b'); |
||
32 | |||
33 | $collection |
||
34 | ->add($taskA, 'a-name') |
||
35 | ->add($taskB, 'b-name'); |
||
36 | |||
37 | // We add methods of our task instances as before and |
||
38 | // after tasks. These methods have access to the task |
||
39 | // class' fields, and may modify them as needed. |
||
40 | $collection |
||
41 | ->after('a-name', [$taskA, 'parenthesizer']) |
||
42 | ->after('a-name', [$taskA, 'emphasizer']) |
||
43 | ->after('b-name', [$taskB, 'emphasizer']) |
||
44 | ->after('b-name', [$taskB, 'parenthesizer']) |
||
45 | ->after('b-name', [$taskB, 'parenthesizer'], 'special-name'); |
||
46 | |||
47 | $result = $collection->run(); |
||
48 | |||
49 | // Ensure that the results have the correct key values |
||
50 | $this->assertEquals( |
||
51 | 'a-name,b-name,special-name,time', |
||
52 | implode(',', array_keys($result->getData()))); |
||
53 | |||
54 | // Verify that all of the after tasks ran in |
||
55 | // the correct order. |
||
56 | $this->assertEquals( |
||
57 | '*(value-a)*', |
||
58 | $result['a-name']['a']); |
||
59 | $this->assertEquals( |
||
60 | '(*value-b*)', |
||
61 | $result['b-name']['b']); |
||
62 | |||
63 | // Note that the last after task is added with a special name; |
||
64 | // its results therefore show up under the name given, rather |
||
65 | // than being stored under the name of the task it was added after. |
||
66 | $this->assertEquals( |
||
67 | '((*value-b*))', |
||
68 | $result['special-name']['b']); |
||
69 | } |
||
70 | |||
71 | public function testBeforeFilters() |
||
72 | { |
||
73 | $collection = new Collection(); |
||
74 | $collection->setLogger(Robo::logger()); |
||
75 | |||
76 | $taskA = new CollectionTestTask('a', 'value-a'); |
||
77 | $taskB = new CollectionTestTask('b', 'value-b'); |
||
78 | |||
79 | $collection |
||
80 | ->add($taskA, 'a-name') |
||
81 | ->add($taskB, 'b-name'); |
||
82 | |||
83 | // We add methods of our task instances as before and |
||
84 | // after tasks. These methods have access to the task |
||
85 | // class' fields, and may modify them as needed. |
||
86 | $collection |
||
87 | ->before('b-name', [$taskA, 'parenthesizer']) |
||
88 | ->before('b-name', [$taskA, 'emphasizer'], 'special-before-name'); |
||
89 | |||
90 | $result = $collection->run(); |
||
91 | |||
92 | // Ensure that the results have the correct key values |
||
93 | $this->assertEquals( |
||
94 | 'a-name,b-name,special-before-name,time', |
||
95 | implode(',', array_keys($result->getData()))); |
||
96 | |||
97 | // The result from the 'before' task is attached |
||
98 | // to 'b-name', since it was called as before('b-name', ...) |
||
99 | $this->assertEquals( |
||
100 | '(value-a)', |
||
101 | $result['b-name']['a']); |
||
102 | // When a 'before' task is given its own name, then |
||
103 | // its results are attached under that name. |
||
104 | $this->assertEquals( |
||
105 | '*(value-a)*', |
||
106 | $result['special-before-name']['a']); |
||
107 | } |
||
108 | |||
109 | public function testAddCodeRollbackAndCompletion() |
||
110 | { |
||
111 | $collection = new Collection(); |
||
112 | $collection->setLogger(Robo::logger()); |
||
113 | |||
114 | $rollback1 = new CountingTask(); |
||
115 | $rollback2 = new CountingTask(); |
||
116 | $completion1 = new CountingTask(); |
||
117 | $completion2 = new CountingTask(); |
||
118 | |||
119 | $collection |
||
120 | ->progressMessage("start collection tasks") |
||
121 | ->rollback($rollback1) |
||
122 | ->completion($completion1) |
||
123 | ->rollbackCode(function() use($rollback1) { $rollback1->run(); } ) |
||
124 | ->completionCode(function() use($completion1) { $completion1->run(); } ) |
||
125 | ->addCode(function () { return 42; }) |
||
126 | ->progressMessage("not reached") |
||
127 | ->rollback($rollback2) |
||
128 | ->completion($completion2) |
||
129 | ->addCode(function () { return 13; }); |
||
130 | |||
131 | $collection->setLogger($this->guy->logger()); |
||
132 | |||
133 | $result = $collection->run(); |
||
134 | // Execution stops on the first error. |
||
135 | // Confirm that status code is converted to a Result object. |
||
136 | $this->assertEquals(42, $result->getExitCode()); |
||
137 | $this->assertEquals(2, $rollback1->getCount()); |
||
138 | $this->assertEquals(0, $rollback2->getCount()); |
||
139 | $this->assertEquals(2, $completion1->getCount()); |
||
140 | $this->assertEquals(0, $completion2->getCount()); |
||
141 | $this->guy->seeInOutput('start collection tasks'); |
||
142 | $this->guy->doNotSeeInOutput('not reached'); |
||
143 | } |
||
144 | |||
145 | public function testStateWithAddCode() |
||
170 | |||
171 | public function testStateWithTaskResult() |
||
199 | |||
200 | public function testDeferredInitialization() |
||
233 | |||
234 | public function testDeferredInitializationWithMessageStorage() |
||
314 | } |
||
315 | |||
316 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.