Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php declare(strict_types=1);
  2.  
  3. namespace tests\unit;
  4.  
  5. use BadMethodCallException;
  6.  
  7. class Immutable
  8. {
  9.     private $a;
  10.     private $b;
  11.     private $c;
  12.  
  13.     protected $set = false;
  14.  
  15.     /**
  16.      * Returns an instance clone that is now immutable, further calls to with<Name> wiill return clones
  17.      * @throws BadMethodCallException if already set
  18.      */
  19.     public function set(): Immutable
  20.     {
  21.         if ($this->set) {
  22.             throw new BadMethodCallException("Instance is already set!");
  23.         }
  24.         $new = clone $this;
  25.         $new->set = true;
  26.         return $new;
  27.     }
  28.  
  29.     public function isHard(): bool { return $this->set; }
  30.  
  31.     private function with(string $name, $value): Immutable
  32.     {
  33.         $new = $this->set ? clone $this : $this;
  34.         $new->{$name} = $value;
  35.         return $new;
  36.     }
  37.  
  38.     public function withA(string $value): Immutable { return $this->with('a', $value); }
  39.     public function withB(string $value): Immutable { return $this->with('b', $value); }
  40.     public function withC(string $value): Immutable { return $this->with('c', $value); }
  41.     public function __toString() { return "{$this->a} : {$this->b} : {$this->c}"; }
  42. }
  43.  
  44. class ImmutableTest extends \PHPUnit\Framework\TestCase
  45. {
  46.     public function testThatDemonstrateUseCase()
  47.     {
  48.         // Semi builder pattern, but without needless instance generation
  49.         $blank = (new Immutable)
  50.             ->withA('Alpacca') // update $blank
  51.             ->withC('Whool') // update $blank
  52.             ->set() // $blank is from now on immutable
  53.         ;
  54.  
  55.         $one = $blank->withB('Cool');
  56.         $two = $blank->withB('Warm');
  57.  
  58.         $this->assertTrue($blank->isHard());
  59.         $this->assertTrue($one->isHard());
  60.         $this->assertTrue($two->isHard());
  61.  
  62.         $this->assertNotSame($blank, $one);
  63.         $this->assertNotSame($blank, $two);
  64.         $this->assertNotSame($one, $two);
  65.  
  66.         $this->assertEquals("Alpacca :  : Whool", $blank);
  67.         $this->assertEquals("Alpacca : Cool : Whool", $one);
  68.         $this->assertEquals("Alpacca : Warm : Whool", $two);
  69.     }
  70.  
  71.     public function testUndefined()
  72.     {
  73.         $this->expectException(BadMethodCallException::class);
  74.         $this->expectExceptionMessage("Instance is already set");
  75.         $c = (new Immutable)->set()->withA('a')->set();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement