|
| 1 | +<?php |
| 2 | + |
| 3 | +class CollectionTest extends \PHPUnit\Framework\TestCase |
| 4 | +{ |
| 5 | + /** @test */ |
| 6 | + public function empty_instantiated_collection_returns_no_items() |
| 7 | + { |
| 8 | + $collection = new \App\Support\Collection; |
| 9 | + |
| 10 | + $this->assertEmpty($collection->get()); |
| 11 | + } |
| 12 | + |
| 13 | + /** @test */ |
| 14 | + public function count_is_correct_for_items_passed_in() { |
| 15 | + $collection = new \App\Support\Collection([ |
| 16 | + 'one', 'two', 'three' |
| 17 | + ]); |
| 18 | + |
| 19 | + $this->assertEquals( |
| 20 | + $collection->count(), |
| 21 | + 3 |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + /** @test */ |
| 26 | + public function items_returned_match_items_passed_in() { |
| 27 | + $collection = new \App\Support\Collection([ |
| 28 | + 'one', 'two', 'three' |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->assertCount(3, $collection->get()); |
| 32 | + $this->assertEquals($collection->get()[0], 'one'); |
| 33 | + $this->assertEquals($collection->get()[1], 'two'); |
| 34 | + $this->assertEquals($collection->get()[2], 'three'); |
| 35 | + } |
| 36 | + |
| 37 | + /** @test */ |
| 38 | + public function collection_is_instance_of_iterator_aggregate() { |
| 39 | + $collection = new \App\Support\Collection; |
| 40 | + |
| 41 | + $this->assertInstanceOf(iteratorAggregate::class, $collection); |
| 42 | + } |
| 43 | + |
| 44 | + /** @test */ |
| 45 | + public function collection_can_be_iterated() { |
| 46 | + $collection = new \App\Support\Collection([ |
| 47 | + 'one', 'two', 'three' |
| 48 | + ]); |
| 49 | + |
| 50 | + $items = []; |
| 51 | + |
| 52 | + foreach($collection as $item) { |
| 53 | + $items[] = $item; |
| 54 | + } |
| 55 | + |
| 56 | + $this->assertCount(3, $items); |
| 57 | + $this->assertInstanceOf( |
| 58 | + ArrayIterator::class, |
| 59 | + $collection->getIterator() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** @test */ |
| 64 | + public function can_add_to_existing_collection() { |
| 65 | + $collection = new \App\Support\Collection(['one', 'two']); |
| 66 | + $collection->add(['three']); |
| 67 | + |
| 68 | + $this->assertCount(3, $collection->get()); |
| 69 | + $this->assertEquals(3, $collection->count()); |
| 70 | + } |
| 71 | + |
| 72 | + /** @test */ |
| 73 | + public function collection_can_be_merged_with_another_collection() { |
| 74 | + |
| 75 | + $collection1 = new \App\Support\Collection(['one', 'two']); |
| 76 | + $collection2 = new \App\Support\Collection(['three', 'four', 'five']); |
| 77 | + |
| 78 | + $collection1->merge($collection2); |
| 79 | + |
| 80 | + $this->assertCount(5, $collection1->get()); |
| 81 | + $this->assertEquals(5, $collection1->count()); |
| 82 | + } |
| 83 | + |
| 84 | + /** @test */ |
| 85 | + public function returns_json_encoded_items() { |
| 86 | + $collection = new \App\Support\Collection([ |
| 87 | + ['username' => 'dino'], |
| 88 | + ['username' => 'frank'] |
| 89 | + ]); |
| 90 | + |
| 91 | + $this->assertIsString($collection->toJson()); |
| 92 | + |
| 93 | + $this->assertEquals( |
| 94 | + '[{"username":"dino"},{"username":"frank"}]', |
| 95 | + $collection->toJson() |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** @test */ |
| 100 | + public function json_encoding_a_collection_object_returns_json() { |
| 101 | + $collection = new \App\Support\Collection([ |
| 102 | + ['username' => 'dino'], |
| 103 | + ['username' => 'jeff'] |
| 104 | + ]); |
| 105 | + |
| 106 | + $encoded = json_encode($collection); |
| 107 | + |
| 108 | + $this->assertIsString($collection->toJson()); |
| 109 | + |
| 110 | + $this->assertEquals( |
| 111 | + '[{"username":"dino"},{"username":"jeff"}]', |
| 112 | + $encoded |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments