Fixed the tests execution and covered the current filter classes.

pull/316/head
Ruslan Baidan 2020-06-30 21:18:06 +02:00
parent 4614a8ee0c
commit deeb0863d9
No known key found for this signature in database
GPG Key ID: 4B7724C136BF1D89
10 changed files with 4545 additions and 11 deletions

View File

@ -18,18 +18,15 @@ abstract class AbstractFunctionalTestCase extends AbstractHttpControllerTestCase
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

@ -9,7 +9,6 @@ use Monarc\Core\Model\Entity\UserRole;
use Monarc\FrontOffice\Controller\ApiAdminUsersController;
use Monarc\FrontOffice\Model\Entity\User;
use MonarcAppFo\Tests\Functional\AbstractFunctionalTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Laminas\Http\Header\HeaderInterface;
use Laminas\ServiceManager\ServiceManager;

View File

@ -147,7 +147,37 @@ class StatsApiServiceTest extends AbstractIntegrationTestCase
$this->mockHandler->getLastRequest()->getBody()->getContents()
);
}
/*
public function testItThrowsTheExceptionIfUserIsNotLoggedIn()
{
}
public function testItThrowsTheExceptionIfUserDeosNotHaveTheRightsToGetTheStats()
{
}
public function testItAddsToTheFilterAlloedForTheUserAnrUuids()
{
}
public function testItAllowsToFilterBySpecificAnrsForCeoRole()
{
}
public function testItAllowsToGetStatsWithoutAnrsLimitationForCeoRole()
{
}
public function testItCanSendDifferentAgregationParamsToGetTheStats()
{
}
*/
private function getStatsResponse(array $results = []): string
{
return json_encode([

View File

@ -0,0 +1,104 @@
<?php declare(strict_types=1);
namespace MonarcAppFo\Tests\Unit\Stats\Validator;
use Doctrine\ORM\EntityNotFoundException;
use Laminas\InputFilter\InputFilter;
use Monarc\FrontOffice\Model\Entity\Anr;
use Monarc\FrontOffice\Model\Table\AnrTable;
use Monarc\FrontOffice\Stats\Validator\GetStatsQueryParamsValidator;
use Monarc\FrontOffice\Validator\FieldValidator\AnrExistenceValidator;
use MonarcAppFo\Tests\Unit\AbstractUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
class GetStatsQueryParamsValidatorTest extends AbstractUnitTestCase
{
/** @var AnrTable|MockObject */
private $anrTable;
/** @var GetStatsQueryParamsValidator */
private $getStatsQueryParamsValidator;
public function setUp(): void
{
parent::setUp();
$this->anrTable = $this->createMock(AnrTable::class);
$this->getStatsQueryParamsValidator = new GetStatsQueryParamsValidator(
new InputFilter(),
$this->anrTable
);
}
public function testItIsNotValidWhenDateToLowerThenDateFromOrBiggerThenCurrentDate()
{
static::assertFalse($this->getStatsQueryParamsValidator->isValid([
'dateFrom' => '2020-01-01',
'dateTo' => '2019-12-01',
]));
static::assertEquals(
[
'dateFrom' => ['"dateFrom" should be lower or equal to "dateTo".'],
'dateTo' => ['"dateTo" should be bigger or equal to "dateFrom".'],
],
$this->getStatsQueryParamsValidator->getErrorMessages()
);
static::assertFalse($this->getStatsQueryParamsValidator->isValid([
'dateFrom' => '3020-01-01',
'dateTo' => '3019-12-01',
]));
static::assertEquals(
[
'dateFrom' => ['"dateFrom" should be lower or equal to current date.'],
'dateTo' => ['"dateTo" should be lower or equal to current date.'],
],
$this->getStatsQueryParamsValidator->getErrorMessages()
);
}
public function testItIsValidWhenTheDatesAreTheSameOrDateFromIsLessThenDateTo()
{
static::assertTrue($this->getStatsQueryParamsValidator->isValid([
'dateFrom' => '2019-12-01',
'dateTo' => '2020-06-01',
]));
static::assertEmpty($this->getStatsQueryParamsValidator->getErrorMessages());
static::assertEquals(
['dateFrom' => '2019-12-01', 'dateTo' => '2020-06-01', 'anrs' => []],
$this->getStatsQueryParamsValidator->getValidData()
);
static::assertTrue($this->getStatsQueryParamsValidator->isValid([
'dateFrom' => '2019-12-01',
'dateTo' => '2019-12-01',
]));
static::assertEmpty($this->getStatsQueryParamsValidator->getErrorMessages());
static::assertEquals(
['dateFrom' => '2019-12-01', 'dateTo' => '2019-12-01', 'anrs' => []],
$this->getStatsQueryParamsValidator->getValidData()
);
}
public function testItThrowAnExceptionWhenSomeAnrsAreNotPresentedInTheTable()
{
$this->anrTable->expects($this->at(0))->method('findById')->willReturn(new Anr());
$this->anrTable->expects($this->at(1))->method('findById')->willReturn(new Anr());
$this->anrTable->expects($this->at(2))->method('findById')->willThrowException(new EntityNotFoundException());
static::assertFalse($this->getStatsQueryParamsValidator->isValid([
'anrs' => [1, 2, 3, 7],
]));
static::assertEquals(
[
'anrs' => [AnrExistenceValidator::ANR_DOES_NOT_EXIST => 'Anr with the ID (3) does not exist.']
],
$this->getStatsQueryParamsValidator->getErrorMessages()
);
}
}

View File

@ -5,5 +5,6 @@ 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"
#CONNECTION_OPTIONS=""
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/data/clean_client_database.sql > /dev/null
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/scripts/clean_client_database.sql > /dev/null

View File

@ -0,0 +1,44 @@
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 `snapshots`;
TRUNCATE TABLE `themes`;
TRUNCATE TABLE `threats`;
TRUNCATE TABLE `user_tokens`;
TRUNCATE TABLE `users`;
TRUNCATE TABLE `users_anrs`;
TRUNCATE TABLE `users_roles`;
TRUNCATE TABLE `vulnerabilities`;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -5,8 +5,9 @@ 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"
#CONNECTION_OPTIONS=""
for i in {0..5}
do
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/data/insert_my_print_anrs.sql > /dev/null
mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS monarc_cli_test < tests/scripts/insert_my_print_anrs.sql > /dev/null
done

File diff suppressed because it is too large Load Diff

View File

@ -5,16 +5,22 @@ 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"
#CONNECTION_OPTIONS=""
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 -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e 'use monarc_common_test'; then
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
php bin/phinx migrate -c tests/migrations/phinx_frontoffice.php
if [[ ! $(mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e 'SHOW TABLES LIKE "phinxlog"' monarc_common_test) ]]
then
php bin/phinx migrate -c tests/migrations/phinx_core.php
fi
if [[ ! $(mysql -h $DBHOST -u $DBUSER_MONARC -p$DBPASSWORD_MONARC $CONNECTION_OPTIONS -e 'SHOW TABLES LIKE "phinxlog"' monarc_cli_test) ]]
then
php bin/phinx migrate -c tests/migrations/phinx_frontoffice.php
fi

View File

@ -84,7 +84,7 @@ sudo mysql -u root -p$DBPASSWORD_ADMIN -e "FLUSH PRIVILEGES;"
sudo systemctl restart mariadb.service > /dev/null
echo -e "\n--- Installing PHP-specific packages… ---\n"
sudo apt-get -y install php apache2 libapache2-mod-php php-curl php-gd php-mysql php-pear php-apcu php-xml php-mbstring php-intl php-imagick php-zip php-xdebug > /dev/null
sudo apt-get -y install php apache2 libapache2-mod-php php-curl php-gd php-mysql php-pear php-apcu php-xml php-mbstring php-intl php-imagick php-zip php-xdebug php-bcmath > /dev/null
echo -e "\n--- Configuring PHP… ---\n"
for key in upload_max_filesize post_max_size max_execution_time max_input_time memory_limit