Exemple #1
0
 public function testAttachDetachMethods()
 {
     $product = new UsingSPL\Product();
     $productPriceObserver = new UsingSPL\ProductPriceObserver();
     $observers = $product->getObservers();
     $this->assertInstanceOf('SplObjectStorage', $observers);
     $this->assertFalse($observers->contains($productPriceObserver));
     $product->attach($productPriceObserver);
     $this->assertTrue($observers->contains($productPriceObserver));
     $product->detach($productPriceObserver);
     $this->assertFalse($observers->contains($productPriceObserver));
 }
 public function testObserverIsUpdated()
 {
     // Create a mock for the ProductPriceObserver class and only mock the update() method.
     $productPriceObserver = $this->getMockBuilder(UsingSPL\ProductPriceObserver::class)->setMethods(array('update'))->getMock();
     // Set up the expectation for the update() method (called only once
     // and with an instance of SplSubject as its parameter).
     $productPriceObserver->expects($this->once())->method('update')->with($this->isInstanceOf('SplSubject'));
     // Create the Product, attach the ProductPriceObserver object and notify it.
     $product = new UsingSPL\Product();
     $product->attach($productPriceObserver);
     $product->notify();
 }