SupportTappableTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Support\Traits\Tappable;
  4. use PHPUnit\Framework\TestCase;
  5. class SupportTappableTest extends TestCase
  6. {
  7. public function testTappableClassWithCallback()
  8. {
  9. $name = TappableClass::make()->tap(function ($tappable) {
  10. $tappable->setName('MyName');
  11. })->getName();
  12. $this->assertSame('MyName', $name);
  13. }
  14. public function testTappableClassWithInvokableClass()
  15. {
  16. $name = TappableClass::make()->tap(new class
  17. {
  18. public function __invoke($tappable)
  19. {
  20. $tappable->setName('MyName');
  21. }
  22. })->getName();
  23. $this->assertSame('MyName', $name);
  24. }
  25. public function testTappableClassWithNoneInvokableClass()
  26. {
  27. $this->expectException('Error');
  28. $name = TappableClass::make()->tap(new class
  29. {
  30. public function setName($tappable)
  31. {
  32. $tappable->setName('MyName');
  33. }
  34. })->getName();
  35. $this->assertSame('MyName', $name);
  36. }
  37. public function testTappableClassWithoutCallback()
  38. {
  39. $name = TappableClass::make()->tap()->setName('MyName')->getName();
  40. $this->assertSame('MyName', $name);
  41. }
  42. }
  43. class TappableClass
  44. {
  45. use Tappable;
  46. private $name;
  47. public static function make()
  48. {
  49. return new static;
  50. }
  51. public function setName($name)
  52. {
  53. $this->name = $name;
  54. }
  55. public function getName()
  56. {
  57. return $this->name;
  58. }
  59. }