Skip to content

Commit

Permalink
fixed #28 - Throw exception for incorrect URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pekral committed Mar 5, 2020
1 parent 913e6a7 commit 24feae8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 101 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
stopOnFailure="false">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix="Test.php">./tests</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/Http/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function createFromOutput(string $response, int $statusCode): Flex

$version = null;
/** @var string|null $message */
$message = null;
$message = $response;
$success = false;
$statistics = [];
$rowCount = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/Http/ResponseHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Consistence\ObjectPrototype;
use EcomailFlexibee\Config;
use EcomailFlexibee\Exception\EcomailFlexibeeNoEvidenceResult;
use EcomailFlexibee\Exception\EcomailFlexibeeRequestError;
use EcomailFlexibee\Http\Response\Response;
use EcomailFlexibee\Result\EvidenceResult;

Expand All @@ -24,11 +25,16 @@ public function __construct(Config $config)
/**
* @param \EcomailFlexibee\Http\Response\Response $response
* @return array<\EcomailFlexibee\Result\EvidenceResult>
* @throws \EcomailFlexibee\Exception\EcomailFlexibeeRequestError
*/
public function convertResponseToEvidenceResults(Response $response): array
{
$data = $response->getData();

if (isset($data['success']) && $data['success'] === 'false') {
throw new EcomailFlexibeeRequestError($data['message']);
}

if (!isset($data[$this->config->getEvidence()])) {
if (\count($data) === 0) {
$data = $response->getStatistics();
Expand All @@ -41,7 +47,7 @@ public function convertResponseToEvidenceResults(Response $response): array
return [new EvidenceResult($data)];
}

return \array_map(static function (array $data){
return \array_map(static function (array $data) {
return new EvidenceResult($data);
}, $data[$this->config->getEvidence()]);
}
Expand Down
87 changes: 10 additions & 77 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,17 @@ public function testAllAndChunkInEvidence(): void
public function testSearchInEvidence(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', false, null);
$result = $client->searchInEvidence('kod<>\'JAN\'', []);
$result = $client->searchInEvidence('(kod neq \'JAN\')', []);
Assert::assertTrue(count($result) > 0);
$this->expectException(EcomailFlexibeeRequestError::class);
$client->searchInEvidence('kod neq \'JAN\'', []);
}

$result = $client->searchInEvidence('(datSplat<\'2018-12-04\'%20and%20zuctovano=false)', []);
Assert::assertTrue(count($result) > 0);
public function testSearchInEvidenceWithInvalidUrl(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', false, null);
$this->expectException(EcomailFlexibeeRequestError::class);
$client->searchInEvidence(' bla.json', []);
}

public function testDryRunRequest(): void
Expand Down Expand Up @@ -238,54 +244,6 @@ public function testMakeCustomRequest(): void
Assert::assertArrayHasKey('properties', $data->getData());
}

public function testWithExampleFlexibeeData(): void
{
/** @var string $content */
$content = file_get_contents(sprintf('%s/_Resources/smlouva.xml', __DIR__));
/** @var \SimpleXMLElement $content */
$content = simplexml_load_string($content);
/** @var string $content */
$content = json_encode((array) $content);
$xmlData = json_decode($content, true);

foreach ($xmlData as $evidenceName => $evidenceData) {
if (in_array($evidenceName, ['@attributes'], true)) {
continue;
}

$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, $evidenceName, false, null);

if (array_key_exists('@attributes', $evidenceData)) {
unset($evidenceData['@attributes']);
}

if (array_key_exists('id', $evidenceData)) {
unset($evidenceData['id']);
}

try {
if ($evidenceName === 'smlouva' && isset($evidenceData['polozkySmlouvy'])) {
unset($evidenceData['polozkySmlouvy']['@attributes']);
$evidenceData['polozkySmlouvy']['kod'] = uniqid();

foreach ($evidenceData['polozkySmlouvy']["smlouva-polozka"] as &$item) {
$item['kod'] = uniqid();

}
}

$idEvidence = (int) $client->save($evidenceData, null)->getData()[0]['id'];
$client->deleteById($idEvidence);
} catch (EcomailFlexibeeRequestError $exception) {
if (mb_stripos($exception->getMessage(), 'již používá jiný záznam') !== false) {
continue;
}

throw $exception;
}
}
}

public function testRunBackendProcesses(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'pokladni-pohyb', false);
Expand Down Expand Up @@ -318,36 +276,11 @@ private function checkResponseStructure(EvidenceResult $result): void
public function testUnknownCompany(): void
{
$client = new Client(Config::HOST, 'xxx', Config::USERNAME, Config::PASSWORD, 'faktura-vydana', false);
$this->expectException(EcomailFlexibeeSaveFailed::class);
$this->expectException(EcomailFlexibeeRequestError::class);
$client->save([], null);

}

public function testCreateUserRelations(): void
{
$client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', false);
$evidenceData = [
'nazev' => $this->faker->firstName,
'kod' => uniqid(),
'typDokl' => 'code:FAKTURA',
];
$invoiceIdA = (int) $client->save($evidenceData, null)->getData()[0]['id'];
Assert::assertNotEmpty($invoiceIdA);
$evidenceData = [
'nazev' => $this->faker->firstName,
'kod' => uniqid(),
'typDokl' => 'code:FAKTURA',
];
$invoiceIdB = (int) $client->save($evidenceData, null)->getData()[0]['id'];
Assert::assertNotEmpty($invoiceIdB);

$client->addUserRelation($invoiceIdA, $invoiceIdB, 10.0, 1);
$relationsEvidenceResult = $client->getUserRelations($invoiceIdA);
Assert::assertNotEmpty($relationsEvidenceResult->getData());
$relationId = $relationsEvidenceResult->getData()[0]['id'];
Assert::assertNotEmpty($relationId);
}

public function testCreateSameEvidenceRecord(): void
{
$code = uniqid();
Expand Down
21 changes: 0 additions & 21 deletions tests/_Resources/smlouva.xml

This file was deleted.

0 comments on commit 24feae8

Please sign in to comment.