Skip to content

Commit ff08271

Browse files
committed
test: base
1 parent 00e38d3 commit ff08271

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

tests/UnderscoreBaseTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Ahc\Underscore\Tests;
4+
5+
use Ahc\Underscore\UnderscoreBase as _;
6+
7+
class Stub
8+
{
9+
public function toArray()
10+
{
11+
return ['a', 'b', 'c'];
12+
}
13+
}
14+
15+
class Json implements \JsonSerializable
16+
{
17+
public function jsonSerialize()
18+
{
19+
return ['a' => 1, 'b' => 2, 'c' => 3];
20+
}
21+
}
22+
23+
class UnderscoreBaseTest extends \PHPUnit_Framework_TestCase
24+
{
25+
public function test_asArray()
26+
{
27+
$this->assertSame(['one'], (new _)->asArray('one'));
28+
$this->assertSame([1, 2], (new _)->asArray([1, 2]));
29+
$this->assertSame(['a', 'b', 'c'], (new _)->asArray(new Stub));
30+
$this->assertSame(['a', 1, 'c', 3], (new _)->asArray(new _(['a', 1, 'c', 3])));
31+
$this->assertSame(['a' => 1, 'b' => 2, 'c' => 3], (new _)->asArray(new Json));
32+
}
33+
34+
public function test_alias()
35+
{
36+
$this->assertTrue(class_exists('Ahc\Underscore'));
37+
38+
$this->assertEquals(new \Ahc\Underscore\Underscore, new \Ahc\Underscore);
39+
}
40+
41+
public function test_underscore()
42+
{
43+
$this->assertTrue(function_exists('underscore'));
44+
45+
$this->assertInstanceOf(_::class, underscore());
46+
}
47+
48+
public function test_now()
49+
{
50+
$this->assertTrue(is_float(_::_()->now()));
51+
}
52+
53+
public function test_keys_values()
54+
{
55+
$array = [[1, 2], 'a' => 3, 7, 'b' => 'B'];
56+
57+
$this->assertSame(array_keys($array), _::_($array)->keys()->get());
58+
$this->assertSame(array_values($array), _::_($array)->values()->get());
59+
}
60+
61+
public function test_pairs()
62+
{
63+
$array = ['a' => 3, 7, 'b' => 'B'];
64+
65+
$this->assertSame(['a' => ['a', 3], 0 => [0, 7], 'b' => ['b', 'B']], _::_($array)->pairs()->get());
66+
}
67+
68+
public function test_invert()
69+
{
70+
$array = ['a' => 3, 7, 'b' => 'B'];
71+
72+
$this->assertSame(array_flip($array), _::_($array)->invert()->get());
73+
}
74+
75+
public function test_pick_omit()
76+
{
77+
$array = _::_(['a' => 3, 7, 'b' => 'B', 1 => ['c', 5]]);
78+
79+
$this->assertSame([7, 'b' => 'B'], $array->pick([0, 'b'])->get());
80+
$this->assertSame(['b' => 'B', 1 => ['c', 5]], $array->pick(1, 'b')->get());
81+
$this->assertSame(['a' => 3, 7], $array->omit([1, 'b'])->get());
82+
$this->assertSame(['b' => 'B', 1 => ['c', 5]], $array->omit('a', 0)->get());
83+
}
84+
}

0 commit comments

Comments
 (0)