View source
<?php
namespace Drupal\Tests\radioactivity\Unit;
use Drupal\radioactivity\RestProcessor;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
class RestProcessorTest extends UnitTestCase {
private $sut;
protected function setUp() : void {
parent::setUp();
$workDirectory = vfsStream::setup('radioactivity');
$this->sut = new RestProcessor([
'payload_file' => $workDirectory
->url() . '/radioactivity-payload.json',
]);
}
public function testProcessData() {
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]';
$response = $this->sut
->processData($data);
$this
->assertEquals('{"status":"ok","message":"Inserted."}', $response);
}
public function testProcessFaultyData() {
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10}]';
$response = $this->sut
->processData($data);
$this
->assertEquals('{"status":"error","message":"Invalid json."}', $response);
}
public function testGetData() {
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]';
$this->sut
->processData($data);
$response = $this->sut
->getData();
$this
->assertEquals('[[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]]', $response);
}
public function testGetMultiData() {
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]';
$this->sut
->processData($data);
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"87654321"}]';
$this->sut
->processData($data);
$response = $this->sut
->getData();
$this
->assertEquals('[[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}],' . PHP_EOL . '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"87654321"}]]', $response);
}
public function testGetEmptyData() {
$response = $this->sut
->getData();
$this
->assertEquals('[]', $response);
}
public function testClearData() {
$data = '[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]';
$this->sut
->processData($data);
$response = $this->sut
->getData();
$this
->assertEquals('[[{"fn":"field_ra","et":"node","id":"66","e":10,"h":"12345678"}]]', $response);
$response = $this->sut
->clearData();
$this
->assertEquals('{"status":"ok","message":"Cleared."}', $response);
$response = $this->sut
->getData();
$this
->assertEquals('[]', $response);
}
public function testErrorResponse() {
$response = $this->sut
->error();
$this
->assertEquals('{"status":"error","message":"Nothing to do."}', $response);
}
}