Skip to content

Commit

Permalink
Refactor Pieces class. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdnbrk authored Nov 11, 2019
1 parent 8af6d71 commit 63bd914
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
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

0 comments on commit 63bd914

Please sign in to comment.