Completed
Push — master ( 3a8bbf...f6d028 )
by Chad
17s
created

LinqCollectionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 65
dl 0
loc 152
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A collectionCannotBeConstructedWithString() 0 3 1
A whereFilters() 0 31 1
A collectionCanBeCreatedFromIterator() 0 5 1
A collectionCanBeCreatedFromArray() 0 5 1
A cannotCallFirstOnEmptyCollection() 0 4 1
A select() 0 25 1
A skipAndTake() 0 18 1
A setUp() 0 4 1
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 ::<private>
14
 */
15
final class LinqCollectionTest extends TestCase
16
{
17
    /**
18
     * @var LinqCollection
19
     */
20
    private $collection;
21
22
    /**
23
     * Prepare each test.
24
     */
25
    public function setUp()
26
    {
27
        $data = json_decode(file_get_contents(__DIR__ . '/books.json'));
28
        $this->collection = LinqCollection::from(new ArrayIterator($data));
29
    }
30
31
    /**
32
     * @test
33
     * @covers ::from
34
     */
35
    public function collectionCanBeCreatedFromIterator()
36
    {
37
        $array = json_decode(file_get_contents(__DIR__ . '/books.json'), true);
38
        $collection = LinqCollection::from(new ArrayIterator($array));
39
        $this->assertSame($array, iterator_to_array($collection));
40
    }
41
42
    /**
43
     * @test
44
     * @covers ::from
45
     */
46
    public function collectionCanBeCreatedFromArray()
47
    {
48
        $array = json_decode(file_get_contents(__DIR__ . '/books.json'), true);
49
        $collection = LinqCollection::from($array);
50
        $this->assertSame($array, iterator_to_array($collection));
51
    }
52
53
    /**
54
     * @test
55
     * @covers ::from
56
     * @expectedException \InvalidArgumentException
57
     */
58
    public function collectionCannotBeConstructedWithString()
59
    {
60
        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

60
        LinqCollection::from(/** @scrutinizer ignore-type */ 'abcd');
Loading history...
61
    }
62
63
    /**
64
     * @test
65
     * @covers ::first
66
     * @expectedException \SubjectivePHP\Linq\InvalidOperationException
67
     */
68
    public function cannotCallFirstOnEmptyCollection()
69
    {
70
        $collection = LinqCollection::from([]);
71
        $collection->first();
72
    }
73
74
    /**
75
     * @test
76
     * @covers ::skip
77
     * @covers ::take
78
     */
79
    public function skipAndTake()
80
    {
81
        $result = $this->collection->skip(2)->take(1);
82
83
        $this->assertEquals(
84
            [
85
                2 => (object)[
86
                    "author" => "Corets, Eva",
87
                    "title" => "Maeve Ascendant",
88
                    "genre" => "Fantasy",
89
                    "price" => 5.95,
90
                    "published" => 974437200,
91
                    "description" => 'After the collapse of a nanotechnology society in England, the young survivors '
92
                    . 'lay the foundation for a new society.',
93
                    "id" => "58339e95d526f"
94
                ],
95
            ],
96
            iterator_to_array($result)
97
        );
98
    }
99
100
    /**
101
     * @test
102
     * @covers ::where
103
     */
104
    public function whereFilters()
105
    {
106
        $callable = function (StdClass $book) : bool {
107
            return $book->genre === 'Romance';
108
        };
109
110
        $result = $this->collection->where($callable);
111
112
        $this->assertEquals(
113
            [
114
                5 => (object)[
115
                    "author" => "Randall, Cynthia",
116
                    "title" => "Lover Birds",
117
                    "genre" => "Romance",
118
                    "price" => 4.95,
119
                    "published" => 967867200,
120
                    "description" => 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get'
121
                    . ' ruffled.',
122
                    "id" => "58339e95d530e"
123
                ],
124
                6 => (object)[
125
                    "author" => "Thurman, Paula",
126
                    "title" => "Splish Splash",
127
                    "genre" => "Romance",
128
                    "price" => 4.95,
129
                    "published" => 973141200,
130
                    "description" => "A deep sea diver finds true love twenty thousand leagues beneath the sea.",
131
                    "id" => "58339e95d5343"
132
                ]
133
            ],
134
            iterator_to_array($result)
135
        );
136
    }
137
138
    /**
139
     * @test
140
     * @covers ::select
141
     */
142
    public function select()
143
    {
144
        $callable = function (StdClass $book) : array {
145
            return [
146
                'id' => $book->id,
147
                'genre' => $book->genre,
148
            ];
149
        };
150
151
        $result = $this->collection->select($callable);
152
        $this->assertSame(
153
            [
154
                ['id' => '58339e95d5200', 'genre' => 'Computer'],
155
                ['id' => '58339e95d5239', 'genre' => 'Fantasy'],
156
                ['id' => '58339e95d526f', 'genre' => 'Fantasy'],
157
                ['id' => '58339e95d52a4', 'genre' => 'Fantasy'],
158
                ['id' => '58339e95d52d9', 'genre' => 'Fantasy'],
159
                ['id' => '58339e95d530e', 'genre' => 'Romance'],
160
                ['id' => '58339e95d5343', 'genre' => 'Romance'],
161
                ['id' => '58339e95d5378', 'genre' => 'Horror'],
162
                ['id' => '58339e95d53ae', 'genre' => 'Science Fiction'],
163
                ['id' => '58339e95d53e4', 'genre' => 'Computer'],
164
                ['id' => '58339e95d5419', 'genre' => 'Computer'],
165
            ],
166
            iterator_to_array($result)
167
        );
168
    }
169
}
170