class EmitControllerTest in Radioactivity 8.3
Same name and namespace in other branches
- 4.0.x tests/src/Unit/EmitControllerTest.php \Drupal\Tests\radioactivity\Unit\EmitControllerTest
@coversDefaultClass \Drupal\radioactivity\Controller\EmitController @group radioactivity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\radioactivity\Unit\EmitControllerTest
Expanded class hierarchy of EmitControllerTest
File
- tests/
src/ Unit/ EmitControllerTest.php, line 20
Namespace
Drupal\Tests\radioactivity\UnitView source
class EmitControllerTest extends UnitTestCase {
/**
* The request received by the controller.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $container;
/**
* The incident storage factory.
*
* @var \Drupal\radioactivity\StorageFactory
*/
protected $incidentStorageFactory;
/**
* The default incident storage.
*
* @var \Drupal\radioactivity\DefaultIncidentStorage
*/
protected $defaultIncidentStorage;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Initiate the Settings singleton used by this test.
new Settings([
'hash_salt' => 'liesjeleerdelotjelopen',
]);
$this->request = $this
->prophesize(Request::class);
$this->incidentStorageFactory = $this
->prophesize(StorageFactory::class);
$this->defaultIncidentStorage = $this
->prophesize(IncidentStorageInterface::class);
$this->incidentStorageFactory
->get('default')
->willReturn($this->defaultIncidentStorage
->reveal());
$this->container = $this
->prophesize(ContainerInterface::class);
\Drupal::setContainer($this->container
->reveal());
$this->container
->get('radioactivity.storage')
->willReturn($this->incidentStorageFactory
->reveal());
}
/**
* @covers ::emit
*/
public function testEmitEmptyRequest() {
$this->request
->getContent()
->willReturn(NULL);
$controller = EmitController::create($this->container
->reveal());
$response = $controller
->emit($this->request
->reveal());
$this->defaultIncidentStorage
->addIncident()
->shouldNotBeCalled();
$this
->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
$this
->assertEquals('{"status":"error","message":"Empty request."}', $response
->getContent());
}
/**
* @covers ::emit
*/
public function testEmitValidRequest() {
$postData = Json::encode([
[
'fn' => 'field_name',
'et' => 'entity_type',
'id' => '99',
'e' => 5.5,
'h' => '5aa2ff01ac75da55751051a55021092768d079c5',
],
]);
$this->request
->getContent()
->willReturn($postData);
$sut = EmitController::create($this->container
->reveal());
$response = $sut
->emit($this->request
->reveal());
$this->defaultIncidentStorage
->addIncident(Argument::type(IncidentInterface::class))
->shouldBeCalledTimes(1);
$this
->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
$this
->assertEquals('{"status":"ok","message":"1 incidents added."}', $response
->getContent());
}
/**
* @covers ::emit
*/
public function testEmitInvalidRequest() {
$postData = Json::encode([
[
'fn' => 'field_name',
'et' => 'entity_type',
'id' => '99',
'e' => 5.5,
'h' => '',
],
]);
$this->request
->getContent()
->willReturn($postData);
$controller = EmitController::create($this->container
->reveal());
$response = $controller
->emit($this->request
->reveal());
$this->defaultIncidentStorage
->addIncident(Argument::type(IncidentInterface::class))
->shouldNotBeCalled(0);
$this
->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
$this
->assertEquals('{"status":"error","message":"invalid incident (0)."}', $response
->getContent());
}
/**
* @covers ::emit
*/
public function testEmitMultipleRequest() {
$postData = Json::encode([
[
'fn' => 'field_name',
'et' => 'entity_type',
'id' => '99',
'e' => 5.5,
'h' => '5aa2ff01ac75da55751051a55021092768d079c5',
],
[
'fn' => 'field_name',
'et' => 'entity_type',
'id' => '99',
'e' => 3.3,
'h' => '',
],
]);
$this->request
->getContent()
->willReturn($postData);
$controller = EmitController::create($this->container
->reveal());
$response = $controller
->emit($this->request
->reveal());
$this->defaultIncidentStorage
->addIncident(Argument::type(IncidentInterface::class))
->shouldBeCalledTimes(1);
$this
->assertEquals('Symfony\\Component\\HttpFoundation\\JsonResponse', get_class($response));
$this
->assertEquals('{"status":"error","message":"invalid incident (1)."}', $response
->getContent());
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EmitControllerTest:: |
protected | property | The dependency injection container. | |
EmitControllerTest:: |
protected | property | The default incident storage. | |
EmitControllerTest:: |
protected | property | The incident storage factory. | |
EmitControllerTest:: |
protected | property | The request received by the controller. | |
EmitControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EmitControllerTest:: |
public | function | @covers ::emit | |
EmitControllerTest:: |
public | function | @covers ::emit | |
EmitControllerTest:: |
public | function | @covers ::emit | |
EmitControllerTest:: |
public | function | @covers ::emit | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |