Covered the stats aggregation with the tests.

pull/316/head
Ruslan Baidan 2020-05-18 23:56:07 +02:00
parent dcfccc04e9
commit c1db6b2c3b
No known key found for this signature in database
GPG Key ID: 4B7724C136BF1D89
7 changed files with 5835 additions and 12 deletions

View File

@ -7,20 +7,20 @@ use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
abstract class AbstractIntegrationTestCase extends AbstractHttpControllerTestCase abstract class AbstractIntegrationTestCase extends AbstractHttpControllerTestCase
{ {
/** @var string */
protected $testPath;
protected function setUp(): void protected function setUp(): void
{ {
$this->setApplicationConfig(require dirname(__DIR__) . '/../config/application.config.php'); $this->testPath = getenv('TESTS_DIR');
$this->setApplicationConfig(require dirname($this->testPath) . '/config/application.config.php');
parent::setUp(); parent::setUp();
$this->configureServiceManager($this->getApplicationServiceLocator()); $this->configureServiceManager($this->getApplicationServiceLocator());
} }
protected function tearDown(): void
{
// TODO: clear the db data.
}
public static function setUpBeforeClass(): void public static function setUpBeforeClass(): void
{ {
// Creates the DB with initial data, executes all the migrations. // Creates the DB with initial data, executes all the migrations.
@ -29,7 +29,12 @@ abstract class AbstractIntegrationTestCase extends AbstractHttpControllerTestCas
public static function tearDownAfterClass(): void public static function tearDownAfterClass(): void
{ {
// TODO: drop the database or clear the phinxlog table and all the data. shell_exec(getenv('TESTS_DIR') . '/scripts/clean_client_database.sh');
}
protected static function createMyPrintTestData(): void
{
shell_exec(getenv('TESTS_DIR') . '/scripts/insert_my_print_anrs.sh');
} }
protected function configureServiceManager(ServiceManager $serviceManager) protected function configureServiceManager(ServiceManager $serviceManager)

View File

@ -5,8 +5,7 @@ namespace MonarcAppFo\Tests\Integration\Service;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Laminas\ServiceManager\ServiceManager; use Laminas\ServiceManager\ServiceManager;
use Monarc\Core\Service\AuthenticationService; use Monarc\FrontOffice\Model\Table\AnrTable;
use Monarc\Core\Service\ConnectedUserService;
use Monarc\FrontOffice\Model\Table\SettingTable; use Monarc\FrontOffice\Model\Table\SettingTable;
use Monarc\FrontOffice\Provider\StatsApiProvider; use Monarc\FrontOffice\Provider\StatsApiProvider;
use Monarc\FrontOffice\Service\Exception\StatsAlreadyCollectedException; use Monarc\FrontOffice\Service\Exception\StatsAlreadyCollectedException;
@ -18,6 +17,13 @@ class StatsApiServiceTest extends AbstractIntegrationTestCase
/** @var MockHandler */ /** @var MockHandler */
private $mockHandler; private $mockHandler;
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::createMyPrintTestData();
}
protected function configureServiceManager(ServiceManager $serviceManager) protected function configureServiceManager(ServiceManager $serviceManager)
{ {
$serviceManager->setAllowOverride(true); $serviceManager->setAllowOverride(true);
@ -51,20 +57,60 @@ class StatsApiServiceTest extends AbstractIntegrationTestCase
/** @var StatsAnrService $statsAnrService */ /** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class); $statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats(); $statsAnrService->collectStats([99, 78]);
$this->assertEquals('GET', $this->mockHandler->getLastRequest()->getMethod()); $this->assertEquals('GET', $this->mockHandler->getLastRequest()->getMethod());
} }
public function testItCanGenerateTheStatsWhenItIsNotDoneForToday() public function testItCanGenerateTheStatsForAllTheAnrs()
{ {
/** @var AnrTable $anrTable */
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
$anrs = $anrTable->findAll();
$anrUuid = [];
foreach ($anrs as $anr) {
$anrUuid[] = $anr->getUuid();
}
$this->mockHandler->append(new Response(200, [], $this->getStatsResponse())); $this->mockHandler->append(new Response(200, [], $this->getStatsResponse()));
$this->mockHandler->append(new Response(200, [], '{"status": "ok"}')); $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 */ /** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class); $statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats(); $statsAnrService->collectStats();
$this->assertJsonStringEqualsJsonString(
$this->getExpectedStatsDataJson($anrUuid),
$this->mockHandler->getLastRequest()->getBody()->getContents()
);
}
public function testItGenerateTheStatsOnlyForPassedAnrs()
{
$anrIdsToGenerateTheStats = [1, 2, 3];
/** @var AnrTable $anrTable */
$anrTable = $this->getApplicationServiceLocator()->get(AnrTable::class);
$anrs = $anrTable->findAll();
$anrUuid = [];
foreach ($anrs as $num => $anr) {
if ($num + 1 > \count($anrIdsToGenerateTheStats)) {
break;
}
$anrUuid[] = $anr->getUuid();
}
$this->mockHandler->append(new Response(200, [], $this->getStatsResponse()));
$this->mockHandler->append(new Response(200, [], '{"status": "ok"}'));
/** @var StatsAnrService $statsAnrService */
$statsAnrService = $this->getApplicationServiceLocator()->get(StatsAnrService::class);
$statsAnrService->collectStats($anrIdsToGenerateTheStats);
$this->assertJsonStringEqualsJsonString(
$this->getExpectedStatsDataJson($anrUuid),
$this->mockHandler->getLastRequest()->getBody()->getContents()
);
} }
private function getStatsResponse(array $results = []): string private function getStatsResponse(array $results = []): string
@ -80,4 +126,23 @@ class StatsApiServiceTest extends AbstractIntegrationTestCase
'results' => $results, 'results' => $results,
]); ]);
} }
private function getExpectedStatsDataJson(array $anrUuid): string
{
$allStatsData = json_decode(
file_get_contents($this->testPath . '/data/expected_stats_data_for_all_anrs.json'),
true
);
$expectedStats = [];
foreach ($allStatsData as $num => $statsData) {
if (!isset($anrUuid[$num])) {
break;
}
$statsData['anr_uuid'] = $anrUuid[$num];
$expectedStats[] = $statsData;
}
return json_encode($expectedStats);
}
} }

View File

@ -0,0 +1,39 @@
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `anrs`;
TRUNCATE TABLE `amvs`;
TRUNCATE TABLE `anrs_objects`;
TRUNCATE TABLE `anrs_objects_categories`;
TRUNCATE TABLE `assets`;
TRUNCATE TABLE `deliveries`;
TRUNCATE TABLE `instances`;
TRUNCATE TABLE `instances_consequences`;
TRUNCATE TABLE `instances_risks`;
TRUNCATE TABLE `instances_risks_op`;
TRUNCATE TABLE `interviews`;
TRUNCATE TABLE `measures`;
TRUNCATE TABLE `measures_amvs`;
TRUNCATE TABLE `measures_measures`;
TRUNCATE TABLE `measures_rolf_risks`;
TRUNCATE TABLE `objects`;
TRUNCATE TABLE `objects_categories`;
TRUNCATE TABLE `objects_objects`;
TRUNCATE TABLE `questions`;
TRUNCATE TABLE `questions_choices`;
TRUNCATE TABLE `recommandations`;
TRUNCATE TABLE `recommandations_risks`;
TRUNCATE TABLE `recommandations_sets`;
TRUNCATE TABLE `referentials`;
TRUNCATE TABLE `rolf_risks_tags`;
TRUNCATE TABLE `rolf_risks`;
TRUNCATE TABLE `rolf_tags`;
TRUNCATE TABLE `scales`;
TRUNCATE TABLE `scales_comments`;
TRUNCATE TABLE `scales_impact_types`;
TRUNCATE TABLE `soa`;
TRUNCATE TABLE `soacategory`;
TRUNCATE TABLE `themes`;
TRUNCATE TABLE `threats`;
TRUNCATE TABLE `vulnerabilities`;
SET FOREIGN_KEY_CHECKS = 1;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
#!/bin/bash
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 -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/data/clean_client_database.sql > /dev/null

View File

@ -0,0 +1,9 @@
#!/bin/bash
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 -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/data/insert_my_print_anrs.sql > /dev/null