|
| 1 | +<?php namespace HybridauthTest\Hybridauth\Data; |
| 2 | + |
| 3 | +use Hybridauth\Data\Collection; |
| 4 | + |
| 5 | +class CollectionTest extends \PHPUnit_Framework_TestCase |
| 6 | +{ |
| 7 | + public function some_random_id() |
| 8 | + { |
| 9 | + return 69; |
| 10 | + } |
| 11 | + |
| 12 | + public function some_random_year() |
| 13 | + { |
| 14 | + return 2020; |
| 15 | + } |
| 16 | + |
| 17 | + public function some_random_array() |
| 18 | + { |
| 19 | + return ['id' => 69, 'slugs' => ['Γεια σας', 'Bonjour', '안녕하세요', 'year' => 2020]]; |
| 20 | + } |
| 21 | + |
| 22 | + public function some_random_object() |
| 23 | + { |
| 24 | + $object = new \StdClass(); |
| 25 | + $object->id = 69; |
| 26 | + $object->slugs = ['Γεια σας', 'Bonjour', '안녕하세요', 'year' => 2020]; |
| 27 | + |
| 28 | + return $object; |
| 29 | + } |
| 30 | + |
| 31 | + public function test_instance_of() |
| 32 | + { |
| 33 | + $collection = new Collection; |
| 34 | + |
| 35 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_identity() |
| 39 | + { |
| 40 | + $array = $this->some_random_array(); |
| 41 | + |
| 42 | + $collection = new Collection($array); |
| 43 | + |
| 44 | + $result = $collection->toArray(); |
| 45 | + |
| 46 | + $this->assertEquals($result, $array); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @covers Collection::exists |
| 51 | + */ |
| 52 | + public function test_exists() |
| 53 | + { |
| 54 | + $array = $this->some_random_array(); |
| 55 | + |
| 56 | + $collection = new Collection($array); |
| 57 | + |
| 58 | + $this->assertTrue($collection->exists('id')); |
| 59 | + |
| 60 | + $this->assertFalse($collection->exists('_non_existant_')); |
| 61 | + |
| 62 | + // |
| 63 | + |
| 64 | + $object = $this->some_random_object(); |
| 65 | + |
| 66 | + $collection = new Collection($object); |
| 67 | + |
| 68 | + $this->assertTrue($collection->exists('id')); |
| 69 | + |
| 70 | + $this->assertFalse($collection->exists('_non_existant_')); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @covers Collection::get |
| 75 | + */ |
| 76 | + public function test_get() |
| 77 | + { |
| 78 | + $array = $this->some_random_array(); |
| 79 | + |
| 80 | + $collection = new Collection($array); |
| 81 | + |
| 82 | + $this->assertEquals($collection->get('id'), $this->some_random_id()); |
| 83 | + |
| 84 | + $this->assertNull($collection->get('_non_existant_')); |
| 85 | + |
| 86 | + // |
| 87 | + |
| 88 | + $object = $this->some_random_object(); |
| 89 | + |
| 90 | + $collection = new Collection($object); |
| 91 | + |
| 92 | + $this->assertEquals($collection->get('id'), $this->some_random_id()); |
| 93 | + |
| 94 | + $this->assertNull($collection->get('_non_existant_')); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @covers Collection::filter |
| 99 | + */ |
| 100 | + public function test_filter() |
| 101 | + { |
| 102 | + $array = $this->some_random_array(); |
| 103 | + |
| 104 | + $collection = new Collection($array); |
| 105 | + |
| 106 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('id')); |
| 107 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('slugs')); |
| 108 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('_non_existant_')); |
| 109 | + |
| 110 | + $this->assertNull($collection->filter('slugs')->get('_non_existant_')); |
| 111 | + |
| 112 | + $this->assertEquals($collection->filter('slugs')->get('year'), $this->some_random_year()); |
| 113 | + |
| 114 | + // |
| 115 | + |
| 116 | + $object = $this->some_random_object(); |
| 117 | + |
| 118 | + $collection = new Collection($object); |
| 119 | + |
| 120 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('id')); |
| 121 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('slugs')); |
| 122 | + $this->assertInstanceOf('\\Hybridauth\\Data\\Collection', $collection->filter('_non_existant_')); |
| 123 | + |
| 124 | + $this->assertNull($collection->filter('slugs')->get('_non_existant_')); |
| 125 | + |
| 126 | + $this->assertEquals($collection->filter('slugs')->get('year'), $this->some_random_year()); |
| 127 | + } |
| 128 | +} |
0 commit comments