Completed
Push — master ( d3a073...5737c8 )
by Greg
02:21
created

tests/cli/ForEachCest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Codeception\Example;
4
use Robo\Collection\CollectionBuilder;
5
use Robo\State\Data;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Data.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
class ForEachCest
8
{
9
10
    /**
11
     * @return array
12
     */
13
    protected function examples()
14
    {
15
        return [
16
            'without items' => [
17
                'expected' => [],
18
                'items' => [],
19
            ],
20
            'with items' => [
21
                'expected' => [
22
                    'a = b',
23
                    'c = d',
24
                    'e = f',
25
                ],
26
                'items' => [
27
                    'a' => 'b',
28
                    'c' => 'd',
29
                    'e' => 'f',
30
                ],
31
            ],
32
        ];
33
    }
34
35
    /**
36
     * @dataProvider examples
37
     */
38
    public function setIterableInConstructor(CliGuy $I, Example $example)
39
    {
40
        $actual = [];
41
42
        $I->wantTo('set iterable in the __constructor');
43
        $I
44
            ->taskForEach($example['items'])
45 View Code Duplication
            ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
46
                $builder->addCode(function () use ($key, $value, &$actual) {
47
                    $actual[] = "$key = $value";
48
49
                    return 0;
50
                });
51
            })
52
            ->run();
53
54
        $I->assertEquals($example['expected'], $actual);
55
    }
56
57
    /**
58
     * @dataProvider examples
59
     */
60
    public function setIterableWithDeferTaskConfiguration(CliGuy $I, Example $example)
61
    {
62
        $actual = [];
63
64
        $I->wantTo('set iterable with deferTaskConfiguration()');
65
        $I
66
            ->collectionBuilder()
67
            ->addCode(function (Data $data) use ($example) {
68
                $data['items'] = $example['items'];
69
70
                return 0;
71
            })
72
            ->addTask(
73
                $I
74
                    ->taskForEach()
75
                    ->deferTaskConfiguration('setIterable', 'items')
76 View Code Duplication
                    ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
77
                        $builder->addCode(function () use ($key, $value, &$actual) {
78
                            $actual[] = "$key = $value";
79
80
                            return 0;
81
                        });
82
                    })
83
            )
84
            ->run();
85
86
        $I->assertEquals($example['expected'], $actual);
87
    }
88
89
    public function uninitializedIterable(CliGuy $I)
90
    {
91
        $actual = 0;
92
        $I->wantTo('call the __constructor() without argument');
93
        $I
94
            ->taskForEach()
95
            ->withBuilder(function (CollectionBuilder $builder, $key, $value) use (&$actual) {
96
                $builder->addCode(function () use ($key, $value, &$actual) {
97
                    $actual++;
98
99
                    return 0;
100
                });
101
            })
102
            ->run();
103
104
        $I->assertEquals(0, $actual);
105
    }
106
}
107