Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cria classe responsavel pelo carrinho
- Loading branch information
1 parent
c451450
commit eaf91a4
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Domains; | ||
|
||
class Cart | ||
{ | ||
/** @var CartItem[] */ | ||
private $items = []; | ||
|
||
/** | ||
* @param CartItem[] $values | ||
*/ | ||
public function __construct(array $values = []) | ||
{ | ||
$this->setItems($values); | ||
} | ||
|
||
/** | ||
* Define os items do carrinho | ||
* | ||
* @param CartItem[] $values | ||
* | ||
* @return self | ||
*/ | ||
public function setItems(array $values = []): self | ||
{ | ||
$this->values = []; | ||
|
||
foreach($values as $value) { | ||
$this->addItem($value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adiciona um item ao carrinho | ||
* | ||
* @param CartItem $value | ||
* | ||
* @return self | ||
*/ | ||
public function addItem(CartItem $value): self | ||
{ | ||
$this->items[] = $value; | ||
return $this; | ||
} | ||
|
||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
upload/system/library/PagSeguro/tests/unit/Domains/CartTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Domains\Cart; | ||
use ValdeirPsr\PagSeguro\Domains\CartItem; | ||
|
||
class CartTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function newInstance() | ||
{ | ||
$instance = new Cart; | ||
$this->assertInstanceOf(Cart::class, $instance); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function defineItemsWithValidArgumentsWithoutErrors() | ||
{ | ||
$instance = new Cart([ | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem() | ||
]); | ||
|
||
$this->assertContainsOnlyInstancesOf(CartItem::class, $instance->getItems()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function defineItemsWithInvalidArgumentsShouldGiveError() | ||
{ | ||
$this->expectException(TypeError::class); | ||
$instance = new Cart([ | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new CartItem(), | ||
new Cart(), | ||
new CartItem() | ||
]); | ||
} | ||
} |