Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
fix(Collection): fix add function for collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Schulze committed Feb 18, 2016
1 parent 5468676 commit 993cddd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Model/Category/CategoryDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
* @method CategoryDraft setExternalId(string $externalId = null)
* @method CustomFieldObjectDraft getCustom()
* @method CategoryDraft setCustom(CustomFieldObjectDraft $custom = null)
* @method LocalizedString getMetaDescription()
* @method CategoryDraft setMetaDescription(LocalizedString $metaDescription = null)
* @method LocalizedString getMetaTitle()
* @method CategoryDraft setMetaTitle(LocalizedString $metaTitle = null)
* @method LocalizedString getMetaKeywords()
* @method CategoryDraft setMetaKeywords(LocalizedString $metaKeywords = null)
*/
class CategoryDraft extends JsonObject
{
Expand Down
7 changes: 3 additions & 4 deletions src/Model/Common/AbstractJsonDeserializeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $data = [], $context = null)
{
$this->setContext($context);
if (!is_null($data)) {
$this->rawData = $data;
$this->rawData = $this->typeData = $data;
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ protected function getTyped($offset)
*/
public function setRawData(array $rawData)
{
$this->rawData = $rawData;
$this->rawData = $this->typeData = $rawData;

return $this;
}
Expand Down Expand Up @@ -190,8 +190,7 @@ public function jsonSerialize()
*/
public function toArray()
{
$data = array_merge($this->rawData, $this->typeData);
$data = array_filter($data, function ($value) {
$data = array_filter($this->typeData, function ($value) {
return !is_null($value);
});
$data = array_map(
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Common/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function initialize($offset)
*/
public function toArray()
{
$values = $this->rawData;
$values = [];
foreach ($this->typeData as $key => $value) {
if ($value instanceof JsonDeserializeInterface) {
$values[$key] = $value->toArray();
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/Model/Common/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function testGetReturnRaw()
[[new \DateTime('2015-01-01')]]
);
$collection->setType('\DateTime');
$this->assertSame('2015-01-01', $collection->getAt(0)->format('Y-m-d'));
$this->assertSame('2015-01-01', $collection->current()->format('Y-m-d'));
}

public function testSetAt()
Expand All @@ -224,4 +224,15 @@ public function testSetAt()

$this->assertInstanceOf('\DateTime', $collection[1]);
}

public function testAdd()
{
$collection = Collection::fromArray([
['currencyCode' => 'EUR', 'centAmount' => 100],
]);
$collection->setType('\Commercetools\Core\Model\Common\Money');
$collection->add(Money::ofCurrencyAndAmount('USD', 110));

$this->assertCount(2, $collection);
}
}

0 comments on commit 993cddd

Please # to comment.