public function testToArray() { $data = ['id' => 'item123', 'title' => 'The Item', 'description' => 'The Item\'s description', 'author' => 'john.doe', 'dateCreated' => '2010-01-01 00:00:00']; $this->item->fromArray($data); $itemArray = $this->item->toArray(); $this->assertCount(4, $itemArray); unset($data['dateCreated']); $this->assertEquals($data, $itemArray); }
/** * Rates an item increasing its score and setting the rate from certain user. * * @param User|string $user a User model or userId * @param Item|string $item an Item model or itemId * @param int $score * @return Item */ public function rateItem($user, $item, $score = 0) { $data = ['userId' => $user instanceof User ? $user->getUserId() : $user, 'points' => $score, 'item' => $item instanceof Item ? $item->toArray() : ['id' => $item]]; $response = $this->connect('POST', self::ITEM_RATE_ROUTE, ['json' => $data]); $contents = $response->getBody()->getContents(); $itemData = $this->serializer->deserialize($contents, 'array', 'json'); // If an Item wasn't provided, create a new Item instance if (!$item instanceof Item) { $item = new Item(); } // Refresh the Item's data and return it $item->fromArray($itemData['item']); return $item; }