Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Pieces class. #19

Merged
merged 1 commit into from
Nov 11, 2019
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
28 changes: 9 additions & 19 deletions src/Resources/Pieces.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,11 @@
class Pieces extends BaseResource
{
/**
* @var \Mvdnbrk\DhlParcel\Resources\Piece[]
*/
protected $pieces;

/**
* Create a new Pieces resource.
* The piece items in this collection.
*
* @param array $attributes
* @var \Mvdnbrk\DhlParcel\Resources\Piece[]
*/
public function __construct($attributes = [])
{
$this->pieces = [];

parent::__construct($attributes);
}
protected $items = [];

/**
* Set the pieces.
Expand All @@ -29,24 +19,24 @@ public function __construct($attributes = [])
public function setPiecesAttribute($pieces)
{
foreach ($pieces as $piece) {
$this->addPiece($piece);
$this->add($piece);
}
}

/**
* Add a piece.
* Add a piece item to this collection.
*
* @param \Mvdnbrk\DhlParcel\Resources\Piece|array $value
*/
public function addPiece($value)
public function add($value)
{
if ($value instanceof Piece) {
$this->pieces[] = $value;
$this->items[] = $value;

return;
}

$this->pieces[] = new Piece($value);
$this->items[] = new Piece($value);
}

/**
Expand All @@ -56,7 +46,7 @@ public function addPiece($value)
*/
public function toArray()
{
return collect($this->pieces)
return collect($this->items)
->whenEmpty(function ($collection) {
return $collection->push(new Piece);
})
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Resources/ParcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function create_a_new_parcel()
$this->assertEquals('Doe', $parcel->recipient->last_name);
$this->assertEquals('Test Company B.V.', $parcel->sender->company_name);
$this->assertEquals('Test 123', $parcel->options->label_description);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $parcel->pieces->pieces[0]->quantity);
$this->assertEquals(1, $parcel->pieces->pieces[0]->weight);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->items[0]->parcel_type);
$this->assertEquals(1, $parcel->pieces->items[0]->quantity);
$this->assertEquals(1, $parcel->pieces->items[0]->weight);
$this->assertSame(true, $parcel->options->only_recipient);
$this->assertSame(true, $parcel->options->signature);
}
Expand Down Expand Up @@ -215,10 +215,10 @@ public function it_can_set_the_pieces_by_passing_a_pieces_object()
'pieces' => $pieces,
]);

$this->assertInstanceOf(Piece::class, $parcel->pieces->pieces[0]);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $parcel->pieces->pieces[0]->quantity);
$this->assertEquals(1, $parcel->pieces->pieces[0]->weight);
$this->assertInstanceOf(Piece::class, $parcel->pieces->items[0]);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $parcel->pieces->items[0]->parcel_type);
$this->assertEquals(1, $parcel->pieces->items[0]->quantity);
$this->assertEquals(1, $parcel->pieces->items[0]->weight);
}

/** @test */
Expand Down
24 changes: 12 additions & 12 deletions tests/Unit/Resources/PiecesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function creating_a_new_pieces_resource_with_array()
],
]);

$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $pieces->pieces[0]->quantity);
$this->assertEquals(1, $pieces->pieces[0]->weight);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type);
$this->assertEquals(1, $pieces->items[0]->quantity);
$this->assertEquals(1, $pieces->items[0]->weight);
}

/** @test */
Expand All @@ -35,35 +35,35 @@ public function creating_a_new_pieces_resource_with_array_of_piece_objects()
],
]);

$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $pieces->pieces[0]->quantity);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type);
$this->assertEquals(1, $pieces->items[0]->quantity);
}

/** @test */
public function it_can_add_a_new_piece_object()
{
$pieces = new Pieces;

$pieces->addPiece(new Piece);
$pieces->add(new Piece);

$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $pieces->pieces[0]->quantity);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type);
$this->assertEquals(1, $pieces->items[0]->quantity);
}

/** @test */
public function it_can_add_a_new_piece_with_an_array()
{
$pieces = new Pieces;

$pieces->addPiece([
$pieces->add([
'parcel_type' => Piece::PARCEL_TYPE_SMALL,
'quantity' => 1,
'weight' => 1,
]);

$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->pieces[0]->parcel_type);
$this->assertEquals(1, $pieces->pieces[0]->quantity);
$this->assertEquals(1, $pieces->pieces[0]->weight);
$this->assertEquals(Piece::PARCEL_TYPE_SMALL, $pieces->items[0]->parcel_type);
$this->assertEquals(1, $pieces->items[0]->quantity);
$this->assertEquals(1, $pieces->items[0]->weight);
}

/** @test */
Expand Down