/** * @param \Nomadez\SDK\Request $request * * @return \Nomadez\SDK\Response * @author Andreas Glaser */ public function sendRequest(Request $request) { $headers = $this->config['headers']; if ($this->apiKey) { $headers[$this->config['api.key.header']] = $this->apiKey; } $headers = array_replace_recursive($headers, $request->getHeaders()); try { $data = ['method' => $request->getMethod(), 'url' => $this->config['api.url'] . $request->getUri() . '.json', 'options' => ['headers' => $headers, 'timeout' => $this->config['client.timeout'], 'connect_timeout' => $this->config['client.connect_timeout'], 'allow_redirects' => true, 'body' => null]]; if (!ValueHelper::isEmpty($request->getPayload())) { $data['options']['body'] = json_encode($request->getPayload()); } $this->log(LogLevel::DEBUG, $data); $response = $this->guzzleHttpClient->request($data['method'], $data['url'], $data['options']); } catch (GuzzleException\ServerException $e) { $response = $e->getResponse(); } catch (GuzzleException\ClientException $e) { $response = $e->getResponse(); } $bodyRaw = $response->getBody()->getContents(); $bodyDecoded = json_decode($bodyRaw); $log = ['statusCode' => $response->getStatusCode(), 'body' => $bodyDecoded, 'bodyRaw' => $bodyRaw]; $this->log(LogLevel::DEBUG, $log); return new Response($response->getStatusCode(), $bodyRaw, $response->getHeaders()); }
/** * @author Andreas Glaser * * @todo Complete this method! */ public function testAnonymousLeadSubmissionToCityLevel() { $courseClasses = [null, 1, 2]; $response = $this->leadPubRes->create(['lead' => ['userCreatedBy' => ['email' => $this->faker->safeEmail, 'profile' => ['firstName' => $this->faker->firstName, 'lastName' => $this->faker->lastName, 'phone' => $this->faker->phoneNumber, 'country' => ['id' => 1], 'city' => ['id' => 1]]], 'durationWeeks' => rand(4, 56), 'studentNote' => $this->faker->sentence(), 'countryDest' => ['id' => 1], 'cityDest' => ['id' => 1], 'courseType' => ['id' => 1], 'courseClass' => ['id' => $courseClasses[array_rand($courseClasses)]], 'leadDateStart' => ['id' => rand(1, 4)]]]); $payload = $response->getBodyDecoded(); $this->assertEquals(201, $response->getStatusCode()); $this->assertTrue(ValueHelper::isInteger($payload['id'])); $this->assertTrue(ValueHelper::isDateTime($payload['created_at'])); }
/** * Tries to format given input. * * @param string|\DateTime $dateTime * @param string $format * @param null $null * * @return null|string * @author Andreas Glaser */ public static function formatOrNull($dateTime, $format = 'Y-m-d H:i:s', $null = null) { if ($dateTime instanceof \DateTime) { return $dateTime->format($format); } elseif (ValueHelper::isDateTime($dateTime)) { return static::stringToDateTime($dateTime)->format($format); } else { return $null; } }
/** * @param $currencyArray * * @author Andreas Glaser */ private function assertCurrencyArray($currencyArray) { $this->assertArrayKeyExistsNotEmpty('id', $currencyArray); $this->assertArrayKeyExistsNotEmpty('slug', $currencyArray); $this->assertArrayKeyExistsNotEmpty('name', $currencyArray); $this->assertArrayKeyExistsNotEmpty('iso3', $currencyArray); $this->assertArrayKeyExistsNotEmpty('symbol', $currencyArray); $this->assertTrue(ValueHelper::isInteger($currencyArray['id'])); $this->assertRegExp('/^[a-z0-9-]+$/', $currencyArray['slug']); $this->assertRegExp('/^[A-Z0-9-]+$/', $currencyArray['iso3']); }
/** * @param $cityArray * * @author Andreas Glaser */ private function assertCityArray($cityArray) { $this->assertArrayKeyExistsNotEmpty('id', $cityArray); $this->assertArrayKeyExistsNotEmpty('slug', $cityArray); $this->assertArrayKeyExistsNotEmpty('name', $cityArray); $this->assertArrayKeyExistsNotEmpty('coordinate', $cityArray); $this->assertTrue(ValueHelper::isInteger($cityArray['id'])); $this->assertRegExp('/^[a-z0-9-]+$/', $cityArray['slug']); $this->assertNotEmpty($cityArray['name']); $this->assertNotEmpty($cityArray['coordinate']); }
/** * Response constructor. * * @param $statusCode * @param $body * @param $headers * * @author Andreas Glaser */ public function __construct($statusCode, $body, $headers) { $this->statusCode = $statusCode; // guess if request succeeded if (ValueHelper::isInteger($this->statusCode) && $this->statusCode >= 200 && $this->statusCode < 300) { $this->setIsSuccess(true); } $this->bodyRaw = $body; $this->bodyDecoded = json_decode($body, true); $this->headers = $headers; }
/** * @param $courseTypeArray * * @author Andreas Glaser */ private function assertCourseTypeArray($courseTypeArray) { $this->assertArrayHasKey('id', $courseTypeArray); $this->assertArrayHasKey('slug', $courseTypeArray); $this->assertArrayHasKey('name', $courseTypeArray); $this->assertArrayHasKey('description_short', $courseTypeArray); $this->assertArrayHasKey('description_long', $courseTypeArray); $this->assertArrayHasKey('image', $courseTypeArray); $this->assertArrayHasKey('parent', $courseTypeArray); $this->assertTrue(ValueHelper::isInteger($courseTypeArray['id'])); $this->assertNotEmpty($courseTypeArray['slug']); $this->assertRegExp('/^[a-z0-9-]+$/', $courseTypeArray['slug']); $this->assertNotEmpty($courseTypeArray['name']); $this->assertNotEmpty($courseTypeArray['description_short']); $this->assertNotEmpty($courseTypeArray['description_long']); $this->assertNotEmpty($courseTypeArray['image']); }
/** * @param $countryArray * * @author Andreas Glaser */ private function assertCountryArray($countryArray) { $this->assertArrayHasKey('id', $countryArray); $this->assertArrayHasKey('slug', $countryArray); $this->assertArrayHasKey('name', $countryArray); $this->assertArrayHasKey('iso2', $countryArray); $this->assertArrayHasKey('iso3', $countryArray); $this->assertArrayHasKey('phone_country_code', $countryArray); $this->assertArrayHasKey('currency', $countryArray); $this->assertArrayHasKey('coordinate', $countryArray); $this->assertTrue(ValueHelper::isInteger($countryArray['id'])); $this->assertNotEmpty($countryArray['slug']); $this->assertRegExp('/^[a-z0-9-]+$/', $countryArray['slug']); $this->assertNotEmpty($countryArray['name']); $this->assertNotEmpty($countryArray['iso2']); $this->assertNotEmpty($countryArray['iso3']); $this->assertNotEmpty($countryArray['phone_country_code']); $this->assertNotEmpty($countryArray['currency']); $this->assertNotEmpty($countryArray['coordinate']); }
/** * @author Andreas Glaser */ public function testIsFalseLike() { $this->assertTrue(ValueHelper::isFalseLike(false)); $this->assertTrue(ValueHelper::isFalseLike(0)); $this->assertTrue(ValueHelper::isFalseLike(0.0)); $this->assertTrue(ValueHelper::isFalseLike('0')); $this->assertTrue(ValueHelper::isFalseLike(null)); $this->assertTrue(ValueHelper::isFalseLike([])); $this->assertFalse(ValueHelper::isFalseLike(1)); $this->assertFalse(ValueHelper::isFalseLike('abc')); $this->assertFalse(ValueHelper::isFalseLike(new \stdClass())); $this->assertFalse(ValueHelper::isFalseLike(true)); $this->assertFalse(ValueHelper::isFalseLike(' ')); }