44
55test ('we can box, transform and unbox a value ' , function () {
66 $ result = Box::of (5 )
7- ->pipe (fn ($ x ) => $ x * 2 )
8- ->pipe (fn ($ x ) => $ x + 1 )
7+ ->map (fn ($ x ) => $ x * 2 )
8+ ->map (fn ($ x ) => $ x + 1 )
99 ->unbox ();
1010
1111 expect ($ result )->toBe (11 );
1212});
1313
1414test ('we can combine a pipe and unbox into one statement by using pull ' , function () {
1515 $ result = Box::of (5 )
16- ->pipe (fn ($ x ) => $ x * 2 )
17- ->pull (fn ($ x ) => $ x + 1 );
16+ ->map (fn ($ x ) => $ x * 2 )
17+ ->get (fn ($ x ) => $ x + 1 );
1818
1919 expect ($ result )->toBe (11 );
2020});
2626 ->toThrow (LogicException::class, $ expectedMessage );
2727});
2828
29- test ('assertion by value passes when the values are the same ' , function () {
30- $ result = Box::of ('Hello ' )->pipe (strtoupper (...))->assert ('HELLO ' )->unbox ();
29+ // @phpstan-ignore-next-line method.notFound
30+ test ('assertion by value passes when the values are the same ' , function (mixed $ input , mixed $ output ) {
31+ $ result = Box::of ($ input )->assert ($ output )->unbox ();
3132
32- expect ($ result )->toBe ('HELLO ' );
33+ expect ($ result )->toBe ($ input );
34+ })->with ([
35+ ['HELLO ' , 'HELLO ' ],
36+ [5 , 5 ],
37+ [5.5 , 5.5 ],
38+ [true , true ],
39+ [false , false ],
40+ [null , null ],
41+ [[], []],
42+ [[1 , 2 , 3 ], [1 , 2 , 3 ]],
43+ [['a ' => 1 , 'b ' => 2 ], ['a ' => 1 , 'b ' => 2 ]],
44+ ['str_split ' , 'str_split ' ], // making sure functions as strings are treated as strings
45+ // [(object)[], (object)[]], // this one will fail because two objects have different references
46+ ]);
47+
48+ test ('assertion by value fails for two equal objects ' , function () {
49+ $ object1 = (object )['number ' => 5 ];
50+ $ object2 = (object )['number ' => 5 ];
51+
52+ expect (fn () => Box::of ($ object1 )->assert ($ object2 ))->toThrow (LogicException::class);
3353});
3454
35- test ('assertion by callback fails when check is not passed ' , function () {
36- $ expectedMessage = ' Value did not pass the callback check ' ;
55+ test ('assertion by value passes for two equal objects with the same reference ' , function () {
56+ $ object = ( object )[ ' number ' => 5 ] ;
3757
38- expect (fn () => Box::of (5 )->assert (fn ($ x ) => $ x < 5 ))
39- ->toThrow (LogicException::class, $ expectedMessage );
58+ $ result = Box::of ($ object )->assert ($ object )->unbox ();
59+
60+ expect ($ result ->number )->toBe (5 );
61+ });
62+
63+ // This test is not here to "lock in" desired behavior. It is here to document a pitfall.
64+ test ('performing a mutation on an object using map() will produce side effects ' , function () {
65+ $ object = (object )['number ' => 5 ];
66+
67+ // Avoid code like this at all costs!
68+ // Problem 1: The original object is mutated
69+ // Problem 2: The object is thrown away and instead $value will simply be 10
70+ $ newObject = Box::of ($ object )->map (fn ($ x ) => $ x ->number = 10 )->unbox ();
71+
72+ expect ($ object ->number )->toBe (10 , 'Bad: Original object is mutated ' );
73+ expect ($ newObject )->toBeInt ('Bad: $newObject is not an object, instead it is an int. ' );
4074});
4175
4276test ('assertion by callback passes when check is passed ' , function () {
5084
5185test ('we can chain multiple transformations and assertions ' , function () {
5286 $ result = Box::of ('Hello ' )
53- ->pipe (strtoupper (...))->assert ('HELLO ' )
54- ->pipe (strrev (...))->assert ('OLLEH ' )
55- ->pipe (str_split (...))->assert (['O ' , 'L ' , 'L ' , 'E ' , 'H ' ])
56- ->pipe (fn ($ arr ) => array_map (ord (...), $ arr ))->assert ([79 , 76 , 76 , 69 , 72 ])
57- ->pipe (array_sum (...))->assert (372 )->assert (fn ($ x ) => $ x > 0 )
58- ->pull (fn ($ x ) => $ x + 100 );
87+ ->map (strtoupper (...))->assert ('HELLO ' )
88+ ->map (strrev (...))->assert ('OLLEH ' )
89+ ->map (str_split (...))->assert (['O ' , 'L ' , 'L ' , 'E ' , 'H ' ])
90+ ->map (fn ($ arr ) => array_map (ord (...), $ arr ))->assert ([79 , 76 , 76 , 69 , 72 ])
91+ ->map (array_sum (...))->assert (372 )->assert (fn ($ x ) => $ x > 0 )
92+ ->get (fn ($ x ) => $ x + 100 );
5993
6094 expect ($ result )->toBe (472 );
6195});
0 commit comments