class AdvbanMiddlewareTest in Advanced ban 8
@coversDefaultClass \Drupal\advban\AdvbanMiddleware @group Advban
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\advban\Unit\AdvbanMiddlewareTest
Expanded class hierarchy of AdvbanMiddlewareTest
File
- tests/
src/ Unit/ AdvbanMiddlewareTest.php, line 15
Namespace
Drupal\Tests\advban\UnitView source
class AdvbanMiddlewareTest extends UnitTestCase {
/**
* The mocked wrapped kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $kernel;
/**
* The mocked ban IP manager.
*
* @var \Drupal\advban\AdvbanIpManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $banManager;
/**
* The tested ban middleware.
*
* @var \Drupal\advban\AdvbanMiddleware
*/
protected $banMiddleware;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$this->banManager = $this
->createMock('Drupal\\advban\\AdvbanIpManagerInterface');
$this->banMiddleware = new AdvbanMiddleware($this->kernel, $this->banManager);
}
/**
* Tests a banned IP.
*/
public function testBannedIp() {
$banned_ip = '17.0.0.2';
$this->banManager
->expects($this
->once())
->method('isBanned')
->with($banned_ip)
->willReturn([
'is_banned' => TRUE,
'expiry_date' => 0,
]);
$request = Request::create('/test-path');
$request->server
->set('REMOTE_ADDR', $banned_ip);
$response = $this->banMiddleware
->handle($request);
$this
->assertEquals(403, $response
->getStatusCode());
}
/**
* Tests an unbanned IP.
*/
public function testUnbannedIp() {
$unbanned_ip = '18.0.0.2';
$this->banManager
->expects($this
->once())
->method('isBanned')
->with($unbanned_ip)
->willReturn([
'is_banned' => FALSE,
'expiry_date' => 0,
]);
$request = Request::create('/test-path');
$request->server
->set('REMOTE_ADDR', $unbanned_ip);
$expected_response = new Response(200);
$this->kernel
->expects($this
->once())
->method('handle')
->with($request, HttpKernelInterface::MASTER_REQUEST, TRUE)
->willReturn($expected_response);
$response = $this->banMiddleware
->handle($request);
$this
->assertSame($expected_response, $response);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdvbanMiddlewareTest:: |
protected | property | The mocked ban IP manager. | |
AdvbanMiddlewareTest:: |
protected | property | The tested ban middleware. | |
AdvbanMiddlewareTest:: |
protected | property | The mocked wrapped kernel. | |
AdvbanMiddlewareTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AdvbanMiddlewareTest:: |
public | function | Tests a banned IP. | |
AdvbanMiddlewareTest:: |
public | function | Tests an unbanned IP. | |
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. |