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); }
public function testToArray() { $data = ['actionCode' => 'LOGIN', 'item' => ['id' => 'The Item', 'title' => 'The Item', 'description' => 'The Item\'s description', 'author' => 'john.doe'], 'categories' => ['foo', 'bar']]; $item = new Item(); $item->setItemId('The Item')->setTitle('The Item')->setDescription('The Item\'s description')->setAuthor('john.doe'); $this->action->fromArray($data); $this->action->setItem($item); $actionArray = $this->action->toArray(); $this->assertCount(3, $actionArray); $this->assertEquals($data, $actionArray); }
/** * @return array */ public function toArray() { $actionArray = []; $actionArray['actionCode'] = $this->actionCode; if (isset($this->item)) { $actionArray['item'] = $this->item->toArray(); } if (!empty($this->categories)) { $actionArray['categories'] = $this->categories; } return $actionArray; }
public function testNotifyInteractionWIthItem() { $history = new History(); $this->client->getEmitter()->attach($history); $this->subscriber->addResponse(new Response(200, [], new Stream(fopen('data://text/plain,[]', 'r')))); $this->client->notifyInteraction('user', 'target-user', 'RATE', 'the-item'); $contents = $history->getLastRequest()->getBody()->__toString(); $request = json_decode($contents, true); $this->assertEquals(['userId' => 'user', 'interactionCode' => 'RATE', 'targetUserId' => 'target-user', 'item' => ['id' => 'the-item']], $request); $this->subscriber->addResponse(new Response(200, [], new Stream(fopen('data://text/plain,[]', 'r')))); $item = new Item(); $item->setItemId('the-item')->setAuthor('the-author'); $this->client->notifyInteraction('user', 'target-user', 'RATE', $item); $contents = $history->getLastRequest()->getBody()->__toString(); $request = json_decode($contents, true); $this->assertEquals(['userId' => 'user', 'interactionCode' => 'RATE', 'targetUserId' => 'target-user', 'item' => $item->toArray()], $request); }
/** * Returns information about the status of an interactionaccording to its limits * * @param User|string $user A User model or userId * @param User|string $targetUser A User model or userId * @param string $interactionCode * @param Item|string $item An Item model or itemId * @return array */ public function getInteractionStatus($user, $targetUser, $interactionCode, $item) { // Prepare request body $requestData = ['user' => $user instanceof User ? $user->getUserId() : $user, 'targetUser' => ['id' => $targetUser instanceof User ? $targetUser->getUserId() : $targetUser], 'interactionCode' => $interactionCode, 'item' => $item instanceof Item ? $item->toArray() : ['id' => $item]]; $response = $this->connect('POST', self::INTERACTION_STATUS_ROUTE, ['json' => $requestData]); $contents = $response->getBody()->getContents(); $responseData = $this->serializer->deserialize($contents, 'array', 'json'); // Process response properties unset($responseData['status']); if (!isset($responseData['score'])) { $responseData['score'] = 0; } return $responseData; }