You are here

class RestProcessorTest in Radioactivity 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/RestProcessorTest.php \Drupal\Tests\radioactivity\Unit\RestProcessorTest

@coversDefaultClass \Drupal\radioactivity\RestProcessor @group radioactivity

Hierarchy

Expanded class hierarchy of RestProcessorTest

File

tests/src/Unit/RestProcessorTest.php, line 13

Namespace

Drupal\Tests\radioactivity\Unit
View source
class RestProcessorTest extends UnitTestCase {

  /**
   * The Rest Processor under test.
   *
   * @var \Drupal\radioactivity\RestProcessor
   */
  private $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $workDirectory = vfsStream::setup('radioactivity');
    $this->sut = new RestProcessor([
      'payload_file' => $workDirectory
        ->url() . '/radioactivity-payload.json',
    ]);
  }

  /**
   * @covers ::processData
   */
  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);
  }

  /**
   * @covers ::processData
   */
  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);
  }

  /**
   * @covers ::getData
   */
  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);
  }

  /**
   * @covers ::getData
   */
  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);
  }

  /**
   * @covers ::getData
   */
  public function testGetEmptyData() {
    $response = $this->sut
      ->getData();
    $this
      ->assertEquals('[]', $response);
  }

  /**
   * @covers ::clearData
   */
  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);
  }

  /**
   * @covers ::error
   */
  public function testErrorResponse() {
    $response = $this->sut
      ->error();
    $this
      ->assertEquals('{"status":"error","message":"Nothing to do."}', $response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RestProcessorTest::$sut private property The Rest Processor under test.
RestProcessorTest::setUp protected function Overrides UnitTestCase::setUp
RestProcessorTest::testClearData public function @covers ::clearData
RestProcessorTest::testErrorResponse public function @covers ::error
RestProcessorTest::testGetData public function @covers ::getData
RestProcessorTest::testGetEmptyData public function @covers ::getData
RestProcessorTest::testGetMultiData public function @covers ::getData
RestProcessorTest::testProcessData public function @covers ::processData
RestProcessorTest::testProcessFaultyData public function @covers ::processData
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.