Skip to content

Commit

Permalink
Merge pull request #18 from BurdaMagazinOrg/feature/DRPLDCX-68/unit-t…
Browse files Browse the repository at this point in the history
…ests-dcx-integration

DRPLDCX-68 Test for article archiving
  • Loading branch information
chrfritsch committed May 12, 2016
2 parents a0c1cd9 + 8b4fc14 commit 7af88fc
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/JsonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getJson($id, $params = NULL) {
$http_status = $this->api_client->getObject($url, $params, $json);

if (200 !== $http_status) {
$message = $this->t('Error getting %url. Status code was %code.', ['%url' => $url, '%code' => $http_status]);
$message = $this->t('Error getting "@url". Status code was @code.', ['@url' => $url, '@code' => $http_status]);
throw new \Exception($message);
}

Expand Down Expand Up @@ -428,6 +428,7 @@ public function archiveArticle($url, $info, $dcx_id) {
];
}

$response_body = NULL;
if (NULL != $dcx_id) {
$json = $this->getJson($dcx_id);
$modcount = $json['properties']['_modcount'];
Expand Down Expand Up @@ -476,7 +477,7 @@ public function archiveArticle($url, $info, $dcx_id) {
}

if ($error) {
throw new \Exception($this->t("Unable to archive %url, %message ", ['%url' => $url, '%message' => $message]));
throw new \Exception($this->t('Unable to archive @url, "@message"', ['@url' => $url, '@message' => $message]));
}

return $dcx_id;
Expand Down
15 changes: 15 additions & 0 deletions tests/src/DcxApiClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Drupal\Tests\dcx_integration;

interface DcxApiClientInterface {

public function getObject($url, array $params, &$data);

public function createObject($url, array $params, array $data, &$response_body);

public function setObject($url, array $params, array $data, &$response_body);

public function deleteObject($url, array $params, &$response_body);

}
15 changes: 0 additions & 15 deletions tests/src/DummyClass.php

This file was deleted.

43 changes: 43 additions & 0 deletions tests/src/DummyDcxApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Drupal\Tests\dcx_integration;

/**
* Dummy API client class which allows introspection of called methods and
* arguments for testing.
*/
class DummyDcxApiClient /* implement DcxApiClientInterface */{

/**
*
* @var string name of the last method call on this object
*/
public $method;
/**
*
* @var mixed arguments of the last call
*/
public $args;

/**
* Magic method which is triggered when invoking inaccessible methods in an
* object context.
*/
public function __call($method, $args) {
if (isset($this->$method)) {
$this->method = $method;
$this->args = $args;

$func = $this->$method;
return call_user_func_array($func, $args);
}
}

public function createObject($url, array $params, array $data, &$response_body) {
$this->method = __FUNCTION__; // __METHOD__ would return namespaced value
$this->args = func_get_args();

// Provides means to manipulate $response_body
$response_body = isset($this->expected_response_body)?$this->expected_response_body:$response_body;
}
}
83 changes: 71 additions & 12 deletions tests/src/Unit/DcxJsonClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\Tests\dcx_integration\Unit;

use Drupal\dcx_integration\JsonClient;
use Drupal\Tests\dcx_integration\DummyClass;
use Drupal\Tests\dcx_integration\DummyDcxApiClient;
use Drupal\Tests\UnitTestCase;

/**
Expand All @@ -13,28 +13,87 @@ class DcxJsonClientTest extends UnitTestCase {
protected $client;

protected $api_client;

function setUp() {
$jsonclientsettings = ['publication' => 'dummy_publication'];
$config_factory = $this->getConfigFactoryStub(['dcx_integration.jsonclientsettings' => $jsonclientsettings]);
$user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface');
$stringTranslation = $this->getStringTranslationStub();
$this->api_client = new DummyClass();
$this->api_client = new DummyDcxApiClient();
$this->api_client->getObject = function() {
return 200;
};

$this->client = new JsonClient($config_factory, $user, $stringTranslation, $this->api_client);
}

function testGetJson() {
$retval = $this->client->getJson('dcxapi:id');
list($url, $params, $bogus_response_reference) = $this->api_client->args;
$this->assertEquals($url, 'id', 'Client disposes "dcxapi:" part of the id.');
$this->assertNotEmpty($params, 'If no params are given, default params are used.');

$retval = $this->client->getJson('dcxapi:id', ['params']);
list($url, $params, $bogus_response_reference) = $this->api_client->args;
$this->assertArrayEquals(['params'], $params, 'If params are given, they are actually used');
$retval = $this->client->getJson('dcxapi:id');
list($url, $params, $bogus_response_reference) = $this->api_client->args;
$this->assertEquals($this->api_client->method, 'getObject', 'getObject method of API client is called.');
$this->assertEquals($url, 'id', 'Client disposes "dcxapi:" part of the id.');
$this->assertNotEmpty($params, 'If no params are given, default params are passed to the API client.');

$retval = $this->client->getJson('dcxapi:id', ['params']);
list($url, $params, $bogus_response_reference) = $this->api_client->args;
$this->assertArrayEquals(['params'], $params, 'If params are given, they are passed to the API client');

$this->api_client->getObject = function() {
return 23;
};

$this->setExpectedException('Exception', 'Error getting "id". Status code was 23.');
$retval = $this->client->getJson('dcxapi:id');
}

function testArchiveArticle_emptyResponse() {
// Expect empty response -> Exception
$this->api_client->expected_response_body = NULL;
$this->setExpectedException('Exception', 'Unable to archive node/1, "The operation yielded no result."');
$this->client->archiveArticle('node/1', [], NULL);
}

function testArchiveArticle_invalidResponse() {
// Expect invalid response -> Exception
$this->api_client->expected_response_body = 'invalid';
$this->setExpectedException('Exception', 'Unable to archive node/1, "The result operation has no type."');
$this->client->archiveArticle('node/1', [], NULL);
}

function testArchiveArticle_noSuccess() {
// Expect response without _type == success -> Exception
$this->api_client->expected_response_body = ['_type' => 'no success'];
$this->setExpectedException('Exception', 'Unable to archive node/1, "no success"');
$this->client->archiveArticle('node/1', [], NULL);
}

function testArchiveArticle_noLocation() {
// Expect response without key location -> Exception
$this->api_client->expected_response_body = ['_type' => 'dcx:success'];
$this->setExpectedException('Exception', 'Unable to archive node/1, "The operation was successful, but key location was not found."');
$this->client->archiveArticle('node/1', [], NULL);
}

function testArchiveArticle_invalidLocation() {
// Expect response without key location -> Exception
$this->api_client->expected_response_body = ['_type' => 'dcx:success', 'location' => 'invalid'];
$this->setExpectedException('Exception', 'Unable to archive node/1, "The operation was successful, but the location was not parseable."');
$this->client->archiveArticle('node/1', [], NULL);
}

function testArchiveArticle_newArticle() {
// Expect response without key location -> Exception
$this->api_client->expected_response_body = ['_type' => 'dcx:success', 'location' => '/dcx/api/document/docABC'];
$dcx_id = $this->client->archiveArticle('node/1', [], NULL);
$this->assertEquals($this->api_client->method, 'createObject');
$this->assertEquals($dcx_id, 'document/docABC');
}

// @TODO fails for strange reason
function X_testArchiveArticle_exisitingArticle() {
// Expect response without key location -> Exception
$this->api_client->expected_response_body = ['_type' => 'dcx:success', 'location' => '/dcx/api/document/docABC'];
$dcx_id = $this->client->archiveArticle('node/1', [], '123');
$this->assertEquals($this->api_client->method, 'setObject');
$this->assertEquals($dcx_id, 'document/docABC');
}
}

0 comments on commit 7af88fc

Please sign in to comment.