public function testCreate() { // Abort if we use a read only token if (!ACCESS_TOKEN && ACCESS_TOKEN_PERMISSION == 'r') { $this->markTestSkipped('This test need an ACCESS_TOKEN_PERMISSION with read and write permission.'); } $tile = $this->stack->instance('tile'); $tile->name = 'Test tile\'s name'; $tile->message = 'Test tile\'s message #testtile ' . date('Y-m-d h:i:s'); $tile->term_id = STACKLA_POST_TERM_ID; $tile->image_url = 'https://scontent-syd2-1.cdninstagram.com/t51.2885-15/e35/14374061_652837251542826_5176014892573917184_n.jpg'; $validations = $tile->validate(); if (count($validations)) { foreach ($validations as $validation) { echo $validation['property'] . " -- " . $validation['message'] . ";\n"; } throw new \Exception("Invalid data for Tile"); } $res = $tile->create(); if ($res) { $this->assertEquals(200, $tile->getResponseCode(), "Need to be 200"); $this->assertNotEmpty($tile->sta_feed_id, "Tile created without any ID"); $this->assertNotEmpty($tile->created_at, "Tile created without any create time"); $this->assertEquals(0, count($tile->errors), "Error: " . json_encode($tile->errors)); } // wait 15 seconds for content to be ingested sleep(15); if ($tile->sta_feed_id) { $request = new Request($this->credentials, API_HOST, API_STACK); $jsonContent = $request->sendGet('tiles/guid:' . $tile->sta_feed_id); if ($jsonContent === false) { $response = $request->getResponse(); $jsonContent = $response->getBody(true); } $resTile = json_decode($jsonContent, true); if (!count($resTile['errors'])) { // $this->assertEmpty($resTile['errors'], sprintf("Tile with ID '%s' is not exist", $tile->sta_feed_id)); } else { // validating all fields $this->assertEquals($tile->name, $resTile['data']['name'], 'Tile\'s name is different from the post request value'); $this->assertEquals($tile->message, $resTile['data']['message'], 'Tile\'s message is different from the post request value'); $this->assertEquals($tile->image_url, $resTile['data']['image_url'], 'Tile\'s image_url is different from the post request value'); } } return $tile; }
/** * Fills object value from Array list * * @param array $arr Array of object data * * @return $this * @throws \Exception */ public function fromArray($arr) { // don't track the changes for fields $this->resetTracking(); $this->no_tracking = true; if (!empty($arr)) { if (isset($arr['data']) && isset($arr['errors'])) { $this->setErrors($arr['errors']); $this->setData($arr['data']); } else { // Iterate over each element in array foreach ($arr as $k => $v) { // If the value is an array, it means, it is an object after conversion if (is_array($v) && !empty($v)) { // Determine the class of the object if (($clazz = ReflectionUtil::getPropertyClass(get_class($this), $k)) != null && !in_array($clazz, $this->type_keywords)) { // If the value is an associative array, it means, its an object. Just make recursive call to it. if (ArrayUtil::isAssocArray($v)) { /** @var self $o */ $o = new $clazz(); $o->fromArray($v); $this->assignValue($k, $o); } else { // Else, value is an array of object/data $arr = array(); // Iterate through each element in that array. foreach ($v as $nk => $nv) { if (is_array($nv)) { $o = new $clazz(); $o->fromArray($nv); $arr[$nk] = $o; } else { $arr[$nk] = $nv; } } $this->assignValue($k, $arr); } } else { $this->assignValue($k, $v); } } else { $this->assignValue($k, $v); } } } } elseif ($arr === false) { $reqRes = $this->request ? $this->request->getResponse() : null; if ($reqRes) { $responseBody = $reqRes->getBody(true); $msg = array(); if ($responseBody != '') { $response = json_decode($responseBody, true); if (count($response['errors'])) { foreach ($response['errors'] as $error) { $msg[] = isset($error['message']) ? "[" . $error['code'] . "] " . $error['message'] : json_encode($error); } } } $this->setErrors($msg); throw new \Exception(implode("\r\n", $msg), $reqRes->getStatusCode()); } else { throw new \Exception('Error while adding data to ' . get_class($this) . ' object'); } } $this->no_tracking = false; $this->isPlaceholder = false; return $this; }
public function testCreateFacebookTerm() { // Abort if we use a read only token if (!ACCESS_TOKEN && ACCESS_TOKEN_PERMISSION == 'r') { $this->markTestSkipped('This test need an ACCESS_TOKEN_PERMISSION with read and write permission.'); } $term = $this->stack->instance('term'); $term->name = 'Test user type term'; $term->display_name = 'Test user term'; $term->active = 1; $term->num_of_backfill = 0; $term->term = 'CNCASYum'; $term->type = Term::TYPE_USER; $term->filter = ''; $term->network = Stackla::NETWORK_FACEBOOK; $res = $term->create(); if ($res) { $this->assertGreaterThan(0, $term->id, "Term created without any ID"); // $this->assertNotEmpty($term->created, "Term created without any create time"); $this->assertEquals(0, count($term->errors), "Error: " . json_encode($term->errors)); } if ($term->id) { $request = new Request($this->credentials, API_HOST, API_STACK); $jsonContent = $request->sendGet('terms/' . $term->id); if ($jsonContent === false) { $response = $request->getResponse(); $jsonContent = $response->getBody(true); } $resTerm = json_decode($jsonContent, true); if (!count($resTerm['errors'])) { // $this->assertEmpty($resTerm['errors'], sprintf("Term with ID '%s' is not exist", $term->id)); } else { // validating all fields $this->assertEquals($term->source_user_id, $resTerm['data']['source_user_id'], 'Term\'s source_user_id is different from the post request value'); $this->assertEquals($term->term, $resTerm['data']['term'], 'Term\'s term is different from the post request value'); $this->assertEquals($term->network, $resTerm['data']['network'], 'Term\'s network is different from the post request value'); $this->assertEquals($term->filter, $resTerm['data']['filter'], 'Term\'s filter is different from the post request value'); $this->assertEquals($term->display_name, $resTerm['data']['displat_name'], 'Term\'s display name is different from the post request value'); $this->assertEquals($term->type, $resTerm['data']['type'], 'Term\'s type is different from the post request value'); $this->assertEquals($term->name, $resTerm['data']['name'], 'Term\'s name is different from the post request value'); } $term->delete(); } }