Completed
Push — master ( 17f750...96b143 )
by Chad
17s
created

LinqCollectionTest::orderByOrdersSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SubjectivePHPTest\Linq;
4
5
use ArrayIterator;
6
use PHPUnit\Framework\TestCase;
7
use StdClass;
8
use SubjectivePHP\Linq\LinqCollection;
9
10
/**
11
 * @coversDefaultClass \SubjectivePHP\Linq\LinqCollection
12
 * @covers ::__construct
13
 * @covers ::getIterator
14
 * @covers ::<private>
15
 */
16
final class LinqCollectionTest extends TestCase
17
{
18
    /**
19
     * @var LinqCollection
20
     */
21
    private $collection;
22
23
    /**
24
     * Prepare each test.
25
     */
26
    public function setUp()
27
    {
28
        $data = json_decode(file_get_contents(__DIR__ . '/books.json'));
29
        $this->collection = LinqCollection::from(new ArrayIterator($data));
30
    }
31
32
    /**
33
     * @test
34
     * @covers ::from
35
     */
36
    public function collectionCanBeCreatedFromIterator()
37
    {
38
        $array = json_decode(file_get_contents(__DIR__ . '/books.json'), true);
39
        $collection = LinqCollection::from(new ArrayIterator($array));
40
        $this->assertSame($array, iterator_to_array($collection));
41
    }
42
43
    /**
44
     * @test
45
     * @covers ::from
46
     */
47
    public function collectionCanBeCreatedFromArray()
48
    {
49
        $array = json_decode(file_get_contents(__DIR__ . '/books.json'), true);
50
        $collection = LinqCollection::from($array);
51
        $this->assertSame($array, iterator_to_array($collection));
52
    }
53
54
    /**
55
     * @test
56
     * @covers ::from
57
     * @expectedException \InvalidArgumentException
58
     */
59
    public function collectionCannotBeConstructedWithString()
60
    {
61
        LinqCollection::from('abcd');
0 ignored issues
show
Bug introduced by
'abcd' of type string is incompatible with the type Traversable|array expected by parameter $collection of SubjectivePHP\Linq\LinqCollection::from(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        LinqCollection::from(/** @scrutinizer ignore-type */ 'abcd');
Loading history...
62
    }
63
64
    /**
65
     * @test
66
     * @covers ::first
67
     * @expectedException \SubjectivePHP\Linq\InvalidOperationException
68
     */
69
    public function cannotCallFirstOnEmptyCollection()
70
    {
71
        $collection = LinqCollection::from([]);
72
        $collection->first();
73
    }
74
75
    /**
76
     * @test
77
     * @covers ::first
78
     */
79
    public function firstReturnsFirstElement()
80
    {
81
        $first = $this->collection->first();
82
        $this->assertSame('58339e95d5200', $first->id);
83
    }
84
85
    /**
86
     * @test
87
     * @covers ::firstOrDefault
88
     */
89
    public function firstReturnsDefaultIfEmpty()
90
    {
91
        $default = new \StdClass();
92
        $callable = function ($element) {
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
        $callable = function (/** @scrutinizer ignore-unused */ $element) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
            return false;
94
        };
95
        $this->assertSame($default, $this->collection->firstOrDefault($callable, $default));
96
    }
97
98
    /**
99
     * @test
100
     * @covers ::count
101
     */
102
    public function countReturnsCountOfElementsInSequence()
103
    {
104
        $this->assertSame(11, $this->collection->count());
105
    }
106
107
    /**
108
     * @test
109
     * @covers ::orderBy
110
     */
111
    public function orderByOrdersSequence()
112
    {
113
        $collection = LinqCollection::from(['z', 'g', 'a', 'n']);
114
        $callable = function ($a, $b) {
115
            return strcmp($a, $b);
116
        };
117
118
        $this->assertSame(
119
            [2 => 'a', 1 => 'g', 3 => 'n', 0 => 'z'],
120
            iterator_to_array($collection->orderBy($callable))
121
        );
122
    }
123
124
    /**
125
     * @test
126
     * @covers ::skip
127
     * @covers ::take
128
     */
129
    public function skipAndTake()
130
    {
131
        $result = $this->collection->skip(2)->take(1);
132
133
        $this->assertEquals(
134
            [
135
                2 => (object)[
136
                    "author" => "Corets, Eva",
137
                    "title" => "Maeve Ascendant",
138
                    "genre" => "Fantasy",
139
                    "price" => 5.95,
140
                    "published" => 974437200,
141
                    "description" => 'After the collapse of a nanotechnology society in England, the young survivors '
142
                    . 'lay the foundation for a new society.',
143
                    "id" => "58339e95d526f"
144
                ],
145
            ],
146
            iterator_to_array($result)
147
        );
148
    }
149
150
    /**
151
     * @test
152
     * @covers ::where
153
     */
154
    public function whereFilters()
155
    {
156
        $callable = function (StdClass $book) : bool {
157
            return $book->genre === 'Romance';
158
        };
159
160
        $result = $this->collection->where($callable);
161
162
        $this->assertEquals(
163
            [
164
                5 => (object)[
165
                    "author" => "Randall, Cynthia",
166
                    "title" => "Lover Birds",
167
                    "genre" => "Romance",
168
                    "price" => 4.95,
169
                    "published" => 967867200,
170
                    "description" => 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get'
171
                    . ' ruffled.',
172
                    "id" => "58339e95d530e"
173
                ],
174
                6 => (object)[
175
                    "author" => "Thurman, Paula",
176
                    "title" => "Splish Splash",
177
                    "genre" => "Romance",
178
                    "price" => 4.95,
179
                    "published" => 973141200,
180
                    "description" => "A deep sea diver finds true love twenty thousand leagues beneath the sea.",
181
                    "id" => "58339e95d5343"
182
                ]
183
            ],
184
            iterator_to_array($result)
185
        );
186
    }
187
188
    /**
189
     * @test
190
     * @covers ::select
191
     */
192
    public function select()
193
    {
194
        $callable = function (StdClass $book) : array {
195
            return [
196
                'id' => $book->id,
197
                'genre' => $book->genre,
198
            ];
199
        };
200
201
        $result = $this->collection->select($callable);
202
        $this->assertSame(
203
            [
204
                ['id' => '58339e95d5200', 'genre' => 'Computer'],
205
                ['id' => '58339e95d5239', 'genre' => 'Fantasy'],
206
                ['id' => '58339e95d526f', 'genre' => 'Fantasy'],
207
                ['id' => '58339e95d52a4', 'genre' => 'Fantasy'],
208
                ['id' => '58339e95d52d9', 'genre' => 'Fantasy'],
209
                ['id' => '58339e95d530e', 'genre' => 'Romance'],
210
                ['id' => '58339e95d5343', 'genre' => 'Romance'],
211
                ['id' => '58339e95d5378', 'genre' => 'Horror'],
212
                ['id' => '58339e95d53ae', 'genre' => 'Science Fiction'],
213
                ['id' => '58339e95d53e4', 'genre' => 'Computer'],
214
                ['id' => '58339e95d5419', 'genre' => 'Computer'],
215
            ],
216
            iterator_to_array($result)
217
        );
218
    }
219
}
220