Completed
Push — master ( 2d2a4f...6d125c )
by Greg
01:53
created

ForEachTest::testUninitializedIterable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Collection\CollectionBuilder;
6
use Robo\State\Data;
7
use Robo\Traits\TestTasksTrait;
8
9
class ForEachTest extends TestCase
10
{
11
    use TestTasksTrait;
12
    use Collection\loadTasks;
13
14
    protected $fixtures;
15
16
    public function setUp()
17
    {
18
        $this->fixtures = new Fixtures();
19
        $this->initTestTasksTrait();
20
    }
21
22
    public function tearDown()
23
    {
24
        $this->fixtures->cleanup();
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function examples()
31
    {
32
        return [
33
            'without items' => [
34
                'expected' => [],
35
                'items' => [],
36
            ],
37
            'with items' => [
38
                'expected' => [
39
                    'a = b',
40
                    'c = d',
41
                    'e = f',
42
                ],
43
                'items' => [
44
                    'a' => 'b',
45
                    'c' => 'd',
46
                    'e' => 'f',
47
                ],
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @dataProvider examples
54
     */
55
    public function testSetIterableInConstructor($expected, $items)
56
    {
57
        $actual = [];
58
59
        // set iterable in the __constructor
60
        $result = $this
0 ignored issues
show
Bug introduced by
The method withBuilder does only exist in Robo\Collection\TaskForEach, but not in Robo\Collection\CollectionBuilder.

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...
61
            ->taskForEach($items)
62 View Code Duplication
            ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
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...
63
                $builder->addCode(function () use ($key, $value, &$actual) {
64
                    $actual[] = "$key = $value";
65
66
                    return 0;
67
                });
68
            })
69
            ->run();
70
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
71
72
        $this->assertEquals($expected, $actual);
73
    }
74
75
    /**
76
     * @dataProvider examples
77
     */
78
    public function testSetIterableWithDeferTaskConfiguration($expected, $items)
79
    {
80
        $actual = [];
81
82
        // set iterable with deferTaskConfiguration()
83
        $result = $this
84
            ->collectionBuilder()
85
            ->addCode(function (Data $data) use ($items) {
86
                $data['items'] = $items;
87
88
                return 0;
89
            })
90
            ->addTask(
91
                $this
0 ignored issues
show
Bug introduced by
The method deferTaskConfiguration does only exist in Robo\Collection\CollectionBuilder, but not in Robo\Collection\TaskForEach.

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...
92
                    ->taskForEach()
93
                    ->deferTaskConfiguration('setIterable', 'items')
94 View Code Duplication
                    ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
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...
95
                        $builder->addCode(function () use ($key, $value, &$actual) {
96
                            $actual[] = "$key = $value";
97
98
                            return 0;
99
                        });
100
                    })
101
            )
102
            ->run();
103
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
104
105
        $this->assertEquals($expected, $actual);
106
    }
107
108
    public function testUninitializedIterable()
109
    {
110
        $actual = 0;
111
        // call the __constructor() without argument
112
        $result = $this
0 ignored issues
show
Bug introduced by
The method withBuilder does only exist in Robo\Collection\TaskForEach, but not in Robo\Collection\CollectionBuilder.

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...
113
            ->taskForEach()
114
            ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
115
                $builder->addCode(function () use ($key, $value, &$actual) {
116
                    $actual++;
117
118
                    return 0;
119
                });
120
            })
121
            ->run();
122
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
123
124
        $this->assertEquals(0, $actual);
125
    }
126
}
127