Added the basic tests to cover stats generation, added a possibility to execute the tests from host machine.

pull/316/head
Ruslan Baidan 2020-05-14 23:43:02 +02:00
parent 139348eed0
commit dcfccc04e9
No known key found for this signature in database
GPG Key ID: 4B7724C136BF1D89
7 changed files with 792 additions and 122 deletions

View File

@ -44,7 +44,7 @@
"ext-json": "*",
"ext-pdo": "*",
"monarc/frontoffice": "dev-feature/stats",
"monarc/core": "^2.9.15",
"monarc/core": "dev-feature/stats as 2.9.18",
"laminas/laminas-mvc": "^3.1",
"laminas/laminas-di": "^3.1",
"laminas/laminas-permissions-rbac": "^3.0",

752
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@ class ApiAdminUsersControllerTest extends AbstractFunctionalTestCase
/** @var ConnectedUserService */
private $connectedUserService;
/** @var AuthenticationService|MockObject */
/** @var AuthenticationService */
private $authenticationService;
protected function configureServiceManager(ServiceManager $serviceManager)

View File

@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace MonarcAppFo\Tests\Integration;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
abstract class AbstractIntegrationTestCase extends AbstractHttpControllerTestCase
{
protected function setUp(): void
{
$this->setApplicationConfig(require dirname(__DIR__) . '/../config/application.config.php');
parent::setUp();
$this->configureServiceManager($this->getApplicationServiceLocator());
}
protected function tearDown(): void
{
// TODO: clear the db data.
}
public static function setUpBeforeClass(): void
{
// Creates the DB with initial data, executes all the migrations.
shell_exec(getenv('TESTS_DIR') . '/scripts/setup_db.sh');
}
public static function tearDownAfterClass(): void
{
// TODO: drop the database or clear the phinxlog table and all the data.
}
protected function configureServiceManager(ServiceManager $serviceManager)
{
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace MonarcAppFo\Tests\Integration\Service;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Laminas\ServiceManager\ServiceManager;
use Monarc\Core\Service\AuthenticationService;
use Monarc\Core\Service\ConnectedUserService;
use Monarc\FrontOffice\Model\Table\SettingTable;
use Monarc\FrontOffice\Provider\StatsApiProvider;
use Monarc\FrontOffice\Service\Exception\StatsAlreadyCollectedException;
use Monarc\FrontOffice\Service\StatsAnrService;
use MonarcAppFo\Tests\Integration\AbstractIntegrationTestCase;
class StatsApiServiceTest extends AbstractIntegrationTestCase
{
/** @var MockHandler */
private $mockHandler;
protected function configureServiceManager(ServiceManager $serviceManager)
{
$serviceManager->setAllowOverride(true);
$this->mockHandler = new MockHandler();
$statsApiProvider = new StatsApiProvider(
$serviceManager->get(SettingTable::class),
[],
$this->mockHandler
);
$serviceManager->setService(StatsApiProvider::class, $statsApiProvider);
$serviceManager->setAllowOverride(false);
}
public function testItThrowsTheErrorWhenTheTheStatsAlreadyGeneratedForToday()
{
$this->expectException(StatsAlreadyCollectedException::class);
$this->expectExceptionMessage('The stats is already collected for today.');
$this->mockHandler->append(new Response(200, [], $this->getStatsResponse([['type' => 'risks']])));
/** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats();
}
public function testItDoesNotSendTheStatsWhenTheDataIsEmpty()
{
$this->mockHandler->append(new Response(200, [], $this->getStatsResponse()));
/** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats();
$this->assertEquals('GET', $this->mockHandler->getLastRequest()->getMethod());
}
public function testItCanGenerateTheStatsWhenItIsNotDoneForToday()
{
$this->mockHandler->append(new Response(200, [], $this->getStatsResponse()));
$this->mockHandler->append(new Response(200, [], '{"status": "ok"}'));
// TODO: import MyPrint to test if the exact Json is generated and compare with the last request history.
/** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats();
}
private function getStatsResponse(array $results = []): string
{
return json_encode([
'metadata' => [
'resultset' => [
'count' => \count($results),
'offset' => 0,
'limit' => 0,
],
],
'results' => $results,
]);
}
}

View File

@ -1,29 +1,35 @@
<?php
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
$appdir = dirname(__DIR__);
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'params' => array(
'host' => 'localhost',
'params' => [
'host' => '127.0.0.1',
'user' => 'sqlmonarcuser',
'password' => 'sqlmonarcuser',
'dbname' => 'monarc_common_test',
'port' => 3306,
),
// To execute tests from your host machine uncomment these lines:
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => '~/web/monarc/MonarcAppFO/vagrant/.vagrant/machines/default/virtualbox/private_key'
]
],
],
'orm_cli' => [
'params' => array(
'host' => 'localhost',
'params' => [
'host' => '127.0.0.1',
'user' => 'sqlmonarcuser',
'password' => 'sqlmonarcuser',
'dbname' => 'monarc_cli_test',
'port' => 3306,
),
// To execute tests from your host machine uncomment these lines:
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => '~/web/monarc/MonarcAppFO/vagrant/.vagrant/machines/default/virtualbox/private_key'
]
],
],
],
'entitymanager' => [

View File

@ -1,15 +1,18 @@
#!/bin/bash
DBUSER_MONARC='sqlmonarcuser'
DBHOST="127.0.0.1"
DBUSER_MONARC="sqlmonarcuser"
DBPASSWORD_MONARC="sqlmonarcuser"
# Comment/Uncomment and modify the following line to run tests from your host machine:
CONNECTION_OPTIONS="--ssl-key=~/web/monarc/MonarcAppFO/vagrant/.vagrant/machines/default/virtualbox/private_key"
mysql -u $DBUSER_MONARC -p$DBPASSWORD_MONARC -e "CREATE DATABASE IF NOT EXISTS monarc_cli_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" > /dev/null
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e "CREATE DATABASE IF NOT EXISTS monarc_cli_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" > /dev/null
# Check if the database is already exist we don't need to create the structure and import the data.
if ! mysql -u $DBUSER_MONARC -p$DBPASSWORD_MONARC -e 'use monarc_common_test'; then
mysql -u $DBUSER_MONARC -p$DBPASSWORD_MONARC -e "CREATE DATABASE monarc_common_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" > /dev/null
mysql -u $DBUSER_MONARC -p$DBPASSWORD_MONARC monarc_common_test < db-bootstrap/monarc_structure.sql > /dev/null
mysql -u $DBUSER_MONARC -p$DBPASSWORD_MONARC monarc_common_test < db-bootstrap/monarc_data.sql > /dev/null
if ! mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e 'use monarc_common_test'; then
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e "CREATE DATABASE monarc_common_test DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" > /dev/null
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_common_test < db-bootstrap/monarc_structure.sql > /dev/null
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_common_test < db-bootstrap/monarc_data.sql > /dev/null
fi
php bin/phinx migrate -c tests/migrations/phinx_core.php