|
| 1 | +<?php |
| 2 | + |
| 3 | +use anlutro\LaravelSettings\ArrayUtil; |
| 4 | + |
| 5 | +class ArrayUtilTest extends PHPUnit_Framework_TestCase |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @test |
| 9 | + * @dataProvider getGetData |
| 10 | + */ |
| 11 | + public function getReturnsCorrectValue(array $data, $key, $expected) |
| 12 | + { |
| 13 | + $this->assertEquals($expected, ArrayUtil::get($data, $key)); |
| 14 | + } |
| 15 | + |
| 16 | + public function getGetData() |
| 17 | + { |
| 18 | + return [ |
| 19 | + [[], 'foo', null], |
| 20 | + [['foo' => 'bar'], 'foo', 'bar'], |
| 21 | + [['foo' => 'bar'], 'bar', null], |
| 22 | + [['foo' => 'bar'], 'foo.bar', null], |
| 23 | + [['foo' => ['bar' => 'baz']], 'foo.bar', 'baz'], |
| 24 | + [['foo' => ['bar' => 'baz']], 'foo.baz', null], |
| 25 | + [['foo' => ['bar' => 'baz']], 'foo', ['bar' => 'baz']], |
| 26 | + [ |
| 27 | + ['foo' => 'bar', 'bar' => 'baz'], |
| 28 | + ['foo', 'bar'], |
| 29 | + ['foo' => 'bar', 'bar' => 'baz'] |
| 30 | + ], |
| 31 | + [ |
| 32 | + ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], |
| 33 | + ['foo.bar', 'bar'], |
| 34 | + ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], |
| 35 | + ], |
| 36 | + [ |
| 37 | + ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], |
| 38 | + ['foo.bar'], |
| 39 | + ['foo' => ['bar' => 'baz']], |
| 40 | + ], |
| 41 | + [ |
| 42 | + ['foo' => ['bar' => 'baz'], 'bar' => 'baz'], |
| 43 | + ['foo.bar', 'baz'], |
| 44 | + ['foo' => ['bar' => 'baz'], 'baz' => null], |
| 45 | + ], |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @test |
| 51 | + * @dataProvider getSetData |
| 52 | + */ |
| 53 | + public function setSetsCorrectKeyToValue(array $input, $key, $value, array $expected) |
| 54 | + { |
| 55 | + ArrayUtil::set($input, $key, $value); |
| 56 | + $this->assertEquals($expected, $input); |
| 57 | + } |
| 58 | + |
| 59 | + public function getSetData() |
| 60 | + { |
| 61 | + return [ |
| 62 | + [ |
| 63 | + ['foo' => 'bar'], |
| 64 | + 'foo', |
| 65 | + 'baz', |
| 66 | + ['foo' => 'baz'], |
| 67 | + ], |
| 68 | + [ |
| 69 | + [], |
| 70 | + 'foo', |
| 71 | + 'bar', |
| 72 | + ['foo' => 'bar'], |
| 73 | + ], |
| 74 | + [ |
| 75 | + [], |
| 76 | + 'foo.bar', |
| 77 | + 'baz', |
| 78 | + ['foo' => ['bar' => 'baz']], |
| 79 | + ], |
| 80 | + [ |
| 81 | + ['foo' => ['bar' => 'baz']], |
| 82 | + 'foo.baz', |
| 83 | + 'foo', |
| 84 | + ['foo' => ['bar' => 'baz', 'baz' => 'foo']], |
| 85 | + ], |
| 86 | + [ |
| 87 | + ['foo' => ['bar' => 'baz']], |
| 88 | + 'foo.baz.bar', |
| 89 | + 'baz', |
| 90 | + ['foo' => ['bar' => 'baz', 'baz' => ['bar' => 'baz']]], |
| 91 | + ], |
| 92 | + [ |
| 93 | + [], |
| 94 | + 'foo.bar.baz', |
| 95 | + 'foo', |
| 96 | + ['foo' => ['bar' => ['baz' => 'foo']]], |
| 97 | + ], |
| 98 | + ]; |
| 99 | + } |
| 100 | + |
| 101 | + /** @test */ |
| 102 | + public function setThrowsExceptionOnNonArraySegment() |
| 103 | + { |
| 104 | + $data = ['foo' => 'bar']; |
| 105 | + $this->setExpectedException('UnexpectedValueException', 'Non-array segment encountered'); |
| 106 | + ArrayUtil::set($data, 'foo.bar', 'baz'); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @test |
| 111 | + * @dataProvider getHasData |
| 112 | + */ |
| 113 | + public function hasReturnsCorrectly(array $input, $key, $expected) |
| 114 | + { |
| 115 | + $this->assertEquals($expected, ArrayUtil::has($input, $key)); |
| 116 | + } |
| 117 | + |
| 118 | + public function getHasData() |
| 119 | + { |
| 120 | + return [ |
| 121 | + [[], 'foo', false], |
| 122 | + [['foo' => 'bar'], 'foo', true], |
| 123 | + [['foo' => 'bar'], 'bar', false], |
| 124 | + [['foo' => 'bar'], 'foo.bar', false], |
| 125 | + [['foo' => ['bar' => 'baz']], 'foo.bar', true], |
| 126 | + [['foo' => ['bar' => 'baz']], 'foo.baz', false], |
| 127 | + [['foo' => ['bar' => 'baz']], 'foo', true], |
| 128 | + [['foo' => null], 'foo', true], |
| 129 | + [['foo' => ['bar' => null]], 'foo.bar', true], |
| 130 | + ]; |
| 131 | + } |
| 132 | +} |
0 commit comments