Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/creational/prototype/Laptop.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public function __construct()
{
$this->id = 2;
$this->name = 'Smartphone';
$this->color = 'Default color';
}
}
11 changes: 11 additions & 0 deletions app/creational/prototype/ProductPrototype.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ abstract class ProductPrototype
{
protected int $id;
protected string $name;
protected string $color;

public function getId(): int
{
Expand All @@ -18,4 +19,14 @@ public function getName(): string
{
return $this->name;
}

public function getColor(): string
{
return $this->color;
}

public function setColor(string $color): void
{
$this->color = $color;
}
}
1 change: 1 addition & 0 deletions app/creational/prototype/Smartphone.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public function __construct()
{
$this->id = 1;
$this->name = 'Smartphone';
$this->color = 'Default color';
}
}
10 changes: 7 additions & 3 deletions tests/creational/prototype/PrototypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function testSmartphoneClone(): void

self::assertEquals($clonedSmartphone->getId(), $smartphone->getId());
self::assertEquals($clonedSmartphone->getName(), $smartphone->getName());
self::assertEquals($clonedSmartphone->getColor(), $smartphone->getColor());
self::assertEquals($clonedSmartphone, $smartphone);
self::assertNotSame($clonedSmartphone, $smartphone);
}

public function testCartCanAddClonedProducts(): void
Expand All @@ -30,12 +32,14 @@ public function testCartCanAddClonedProducts(): void
$cart->addProduct($laptop);

$clonedLaptop = clone $laptop;

$clonedLaptop->setColor('White');
$cart->addProduct($clonedLaptop);

self::assertCount(2, $cart->getProducts());
self::assertEquals($cart->getProducts()[1], $cart->getProducts()[0]);
self::assertNotSame($cart->getProducts()[1], $cart->getProducts()[0]);
self::assertEquals($cart->getProducts()[1]->getId(), $cart->getProducts()[0]->getId());
self::assertEquals($cart->getProducts()[1]->getName(), $cart->getProducts()[0]->getName());
self::assertEquals('White', $cart->getProducts()[1]->getColor());
self::assertEquals('Default color', $cart->getProducts()[0]->getColor());
}
}