|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Support; |
| 4 | + |
| 5 | +use Illuminate\Support\Collection; |
| 6 | +use Illuminate\Support\HigherOrderCollectionProxy; |
| 7 | +use Illuminate\Support\HigherOrderTapProxy; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class HigherOrderProxyTest extends TestCase |
| 11 | +{ |
| 12 | + public function test_get_proxies_property_access_to_items() |
| 13 | + { |
| 14 | + $items = new Collection([ |
| 15 | + (object) ['name' => 'Alice'], |
| 16 | + (object) ['name' => 'Bob'], |
| 17 | + ]); |
| 18 | + |
| 19 | + $proxy = new HigherOrderCollectionProxy($items, 'pluck'); |
| 20 | + |
| 21 | + // The proxied method returns a Collection instance; assert type and values |
| 22 | + $this->assertInstanceOf(Collection::class, $proxy->name); |
| 23 | + $this->assertEquals(['Alice', 'Bob'], $proxy->name->all()); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_call_proxies_method_call_to_items() |
| 27 | + { |
| 28 | + $items = new Collection([ |
| 29 | + new class |
| 30 | + { |
| 31 | + public function shout($s) |
| 32 | + { |
| 33 | + return strtoupper($s); |
| 34 | + } |
| 35 | + }, |
| 36 | + new class |
| 37 | + { |
| 38 | + public function shout($s) |
| 39 | + { |
| 40 | + return strtoupper($s).'!'; |
| 41 | + } |
| 42 | + }, |
| 43 | + ]); |
| 44 | + |
| 45 | + $proxy = new HigherOrderCollectionProxy($items, 'map'); |
| 46 | + |
| 47 | + $result = $proxy->shout('hey'); |
| 48 | + |
| 49 | + $this->assertEquals(['HEY', 'HEY!'], $result->all()); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_call_forwards_and_returns_target() |
| 53 | + { |
| 54 | + $target = new class |
| 55 | + { |
| 56 | + public $count = 0; |
| 57 | + |
| 58 | + public function increment($by = 1) |
| 59 | + { |
| 60 | + $this->count += $by; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + $proxy = new HigherOrderTapProxy($target); |
| 65 | + |
| 66 | + $result = $proxy->increment(3); |
| 67 | + |
| 68 | + $this->assertSame(3, $target->count); |
| 69 | + $this->assertSame($target, $result); |
| 70 | + } |
| 71 | +} |
0 commit comments