Skip to content

Commit

Permalink
Add maintenance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Nov 20, 2023
1 parent b53b247 commit 004a444
Show file tree
Hide file tree
Showing 9 changed files with 658 additions and 9 deletions.
3 changes: 0 additions & 3 deletions src/Helper/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ public function parse(): self

$i = 0;
while (($data = fgetcsv($fp, $this->limit, $this->delimiter)) !== false) {
if ($data === null) {
continue;
}
// skip all empty lines
if ($data[0] !== null) {
if ($i === 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Middleware/MaintenanceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,12 @@ protected function hasValidBypassCookie(ServerRequestInterface $request, array $
if (!isset($data['secret'])) {
return false;
}


$secret = $data['secret'];
$name = $this->getCookieName();
$cookieValue = (new RequestData($request))->cookie($name);
if ($cookieValue === null) {
if (empty($cookieValue)) {
return false;
}

Expand Down
213 changes: 208 additions & 5 deletions tests/Console/Command/MaintenanceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Platine\Test\Framework\Console\Command;

use Platine\Config\Config;
use Platine\Console\Application as ConsoleApp;
use Platine\Console\IO\Interactor;
use Platine\Framework\App\Application;
use Platine\Framework\Console\Command\MaintenanceCommand;
use Platine\Test\Framework\Console\BaseCommandTestCase;
use RuntimeException;

use function Platine\Test\Framework\Fixture\getTestMaintenanceDriver;

/*
* @group core
* @group framework
Expand Down Expand Up @@ -55,7 +54,7 @@ public function testExecuteWrongArgument(): void
$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$this->expectExceptionMessage(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid argument type [not-found], must be one of [up, down, status]');

$o->parse(['platine', 'not-found']);
Expand Down Expand Up @@ -103,7 +102,7 @@ public function testExecuteWrongRetryValue(): void
$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$this->expectExceptionMessage(RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid retry value [0], must be an integer greather than zero');

$o->parse(['platine', 'down', '-r=0']);
Expand All @@ -112,4 +111,208 @@ public function testExecuteWrongRetryValue(): void
$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();
}

public function testExecuteWrongRefreshValue(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url()
]);


$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid refresh value [0], must be an integer greather than zero');

$o->parse(['platine', 'down', '-e=0']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();
}

public function testExecuteWrongHttpStatusValue(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url()
]);


$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid HTTP status value [100], must be between 200 and 505');

$o->parse(['platine', 'down', '-c=100']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();
}

public function testExecuteStatusDown(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => true,
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'status']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Application is down.
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}

public function testExecuteDownAlreadyInMaintenance(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => true,
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'down']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Application is already down.
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}

public function testExecuteDownSuccess(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => false,
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'down', '-r=100', '-e=1000', '-c=300', '-m="Hello World"']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Application is now in maintenance mode.
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}

public function testExecuteOnlineSuccess(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => true,
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'up']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Application is now online
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}

public function testExecuteOnlineError(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => true,
'maintenance' => getTestMaintenanceDriver(true, true),
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'up']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Failed to disable maintenance mode: Maintenance deactivate error.
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}

public function testExecuteDownError(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$app = $this->getMockInstance(Application::class, [
'getAppPath' => $dir->url(),
'isInMaintenance' => false,
'maintenance' => getTestMaintenanceDriver(true, false),
]);

$mockInfo = $this->getConsoleApp();

$o = new MaintenanceCommand($app, $mockInfo[3]);
$o->bind($mockInfo[0]);

$o->parse(['platine', 'down']);
$this->assertEquals('maintenance', $o->getName());

$o->interact($mockInfo[1], $mockInfo[2]);
$o->execute();

$expected = 'APPLICATION MAINTENANCE MANAGEMENT
Failed to enable maintenance mode: Maintenance activate error.
';
$this->assertEquals($expected, $this->getConsoleOutputContent());
}
}
10 changes: 10 additions & 0 deletions tests/Env/EnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public function testGetUsingResolved(): void
$this->assertEquals('foo', Env::get('server_key1'));
$this->assertEquals('foo/bar', Env::get('server_key2'));
}

public function testGetUsingArgumentValue(): void
{
global $mock_preg_replace_callback_to_null;
$mock_preg_replace_callback_to_null = true;
$_SERVER['server_key1'] = 'foo';
$_SERVER['server_key2'] = '${server_keyX}/bar';
$this->assertEquals('foo', Env::get('server_key1'));
$this->assertEquals('${server_keyX}/bar', Env::get('server_key2'));
}
}
58 changes: 58 additions & 0 deletions tests/Http/Maintenance/Driver/FileMaintenanceDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Platine\Test\Framework\Http\Maintenance\Driver;

use org\bovigo\vfs\vfsStream;
use Platine\Config\Config;
use Platine\Dev\PlatineTestCase;
use Platine\Filesystem\Adapter\Local\LocalAdapter;
use Platine\Filesystem\Filesystem;
use Platine\Framework\Http\Maintenance\Driver\FileMaintenanceDriver;

/*
* @group core
* @group framework
*/
class FileMaintenanceDriverTest extends PlatineTestCase
{
protected $vfsRoot;
protected $vfsPath;

protected function setUp(): void
{
parent::setUp();
//need setup for each test
$this->vfsRoot = vfsStream::setup();
$this->vfsPath = vfsStream::newDirectory('my_tests')->at($this->vfsRoot);
}

public function testDefault(): void
{
$dir = $this->createVfsDirectory('app', $this->vfsRoot);
$localAdapter = new LocalAdapter($dir->url());
$filesystem = new Filesystem($localAdapter);

$config = $this->getMockInstanceMap(Config::class, [
'get' => [
['maintenance.storages.file.path', '', $dir->url() . '/maintenance']
]
]);

$o = new FileMaintenanceDriver($config, $filesystem);
$this->assertFalse($o->active());

$o->activate(['foo' => 'bar']);

$this->assertTrue($o->active());

$data = $o->data();
$this->assertCount(1, $data);
$this->assertArrayHasKey('foo', $data);
$this->assertEquals('bar', $data['foo']);

$o->deactivate();
$this->assertFalse($o->active());
}
}
Loading

0 comments on commit 004a444

Please sign in to comment.