cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,238 +1,253 @@
<?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test\Export;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Traversable;
/**
* Helper which creates a set of test for aggregators.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @internal
*/
abstract class AbstractAggregatorTest extends KernelTestCase
{
/**
* Create an aggregator instance which will be used in tests.
*
* This method is always used after an eventuel `setUp` method.
*
* @return \Chill\MainBundle\Export\AggregatorInterface
*/
abstract public function getAggregator();
/**
* Create possible combinaison of data (produced by the form).
*
* This data will be used to generate data providers using this data.
*
* This method is executed before the `setUp` method.
*
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
public abstract function getFormData();
/**
* get an array of query builders that the aggregator will use.
*
* Those query builders will be used to test aggregator behaviour on this
* query builder.
*
* This method is executed before the `setUp` method.
*
* @return \Doctrine\DBAL\Query\QueryBuilder[]
*/
public abstract function getQueryBuilders();
/**
* prepare data for `testGetQueryKeys`
*/
public function dataProviderGetQueryKeys()
{
foreach ($this->getFormData() as $data) {
yield array($data);
}
}
/**
* prepare date for method `testGetResultsAndLabels`
*/
public function dataProviderGetResultsAndLabels()
{
foreach ($this->getQueryBuilders() as $qb) {
foreach ($this->getFormData() as $data) {
yield array(clone $qb, $data);
}
}
}
/**
* provide data for `testAlterQuery`
* provide data for `testAlterQuery`.
*/
public function dataProviderAlterQuery()
{
foreach ($this->getQueryBuilders() as $qb) {
foreach ($this->getFormData() as $data) {
yield array(clone $qb, $data);
yield [clone $qb, $data];
}
}
}
/**
* prepare data for `testGetQueryKeys`.
*/
public function dataProviderGetQueryKeys()
{
foreach ($this->getFormData() as $data) {
yield [$data];
}
}
/**
* prepare date for method `testGetResultsAndLabels`.
*/
public function dataProviderGetResultsAndLabels()
{
foreach ($this->getQueryBuilders() as $qb) {
foreach ($this->getFormData() as $data) {
yield [clone $qb, $data];
}
}
}
/**
* Create an aggregator instance which will be used in tests.
*
* This method is always used after an eventuel `setUp` method.
*
* @return \Chill\MainBundle\Export\AggregatorInterface
*/
abstract public function getAggregator();
/**
* Create possible combinaison of data (produced by the form).
*
* This data will be used to generate data providers using this data.
*
* This method is executed before the `setUp` method.
*
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
abstract public function getFormData();
/**
* get an array of query builders that the aggregator will use.
*
* Those query builders will be used to test aggregator behaviour on this
* query builder.
*
* This method is executed before the `setUp` method.
*
* @return \Doctrine\DBAL\Query\QueryBuilder[]
*/
abstract public function getQueryBuilders();
/**
* test the alteration of query by the filter.
*
* @dataProvider dataProviderAlterQuery
*
* @param type $data
*/
public function testAlterQuery(QueryBuilder $query, $data)
{
// retains informations about query
$nbOfFrom = $query->getDQLPart('from') !== null ?
count($query->getDQLPart('from')) : 0;
$nbOfWhere = $query->getDQLPart('where') !== null ?
$query->getDQLPart('where')->count() : 0;
$nbOfSelect = $query->getDQLPart('select') !== null ?
count($query->getDQLPart('select')) : 0;
$this->getAggregator()->alterQuery($query, $data);
$this->assertGreaterThanOrEqual(
$nbOfFrom,
$query->getDQLPart('from') !== null ? count($query->getDQLPart('from')) : 0,
"Test that there are equal or more 'from' clause after that the filter has
altered the query"
);
$this->assertGreaterThanOrEqual(
$nbOfWhere,
$query->getDQLPart('where') !== null ? $query->getDQLPart('where')->count() : 0,
"Test that there are equal or more 'where' clause after that the filter has"
. 'altered the query'
);
$this->assertGreaterThanOrEqual(
$nbOfSelect,
$query->getDQLPart('select') !== null ? count($query->getDQLPart('select')) : 0,
"Test that the filter has no altered the 'select' part of the query"
);
}
/**
* Test the `applyOn` method.
*/
public function testApplyOn()
{
$filter = $this->getAggregator();
$this->assertInternalType('string', $filter->applyOn(),
"test that the internal type of \"applyOn\" is a string");
$this->assertNotEmpty($filter->applyOn(),
"test that the \"applyOn\" method return a non-empty string");
$this->assertInternalType(
'string',
$filter->applyOn(),
'test that the internal type of "applyOn" is a string'
);
$this->assertNotEmpty(
$filter->applyOn(),
'test that the "applyOn" method return a non-empty string'
);
}
/**
* test the `getTitle` method
*/
public function testGetTitle()
{
$title = $this->getAggregator()->getTitle();
$this->assertInternalType('string', $title);
$this->assertNotEmpty($title,
"test that the title is not empty");
}
/**
* Test that the query keys are strings
*
* @param array $data
* Test that the query keys are strings.
*
* @dataProvider dataProviderGetQueryKeys
*/
public function testGetQueryKeys(array $data)
{
$queryKeys = $this->getAggregator()->getQueryKeys($data);
$this->assertInternalType('array', $queryKeys,
"test that the query keys returned are an array");
$this->assertContainsOnly("string", $queryKeys,
"test that the query keys returned by `getQueryKeys` are only strings");
$this->assertGreaterThanOrEqual(1, count($queryKeys),
"test that there are at least one query key returned");
$this->assertInternalType(
'array',
$queryKeys,
'test that the query keys returned are an array'
);
$this->assertContainsOnly(
'string',
$queryKeys,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
count($queryKeys),
'test that there are at least one query key returned'
);
}
/**
*
* Test that
*
* Test that.
*
* - the results have a correct form (are arrays or traversable)
* - each key in a row are present in getQueryKeys ;
* - each returned object of the `getLabels` method is callable
* - each result can be converted to string using this callable
* - each of this callable can provide a string for '_header'
*
* @param QueryBuilder $qb
* @param array $data
*
*
* @dataProvider dataProviderGetResultsAndLabels
*/
public function testGetResultsAndLabels(QueryBuilder $qb, array $data)
{
// it is more convenient to group the `getResult` and `getLabels` test
// due to the fact that testing both methods use the same tools.
// limit the result for the query for performance reason
$qb->setMaxResults(1);
$queryKeys = $this->getAggregator()->getQueryKeys($data);
$this->getAggregator()->alterQuery($qb, $data);
$results = $qb->getQuery()->getResult(AbstractQuery::HYDRATE_ARRAY);
if (count($results) === 0) {
$this->markTestIncomplete("The result is empty. We cannot process tests "
. "on results");
$this->markTestIncomplete('The result is empty. We cannot process tests '
. 'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue( $result instanceof \Traversable || is_array($result),
"test that each row in the result is traversable or an array");
$this->assertTrue(
$result instanceof Traversable || is_array($result),
'test that each row in the result is traversable or an array'
);
foreach ($queryKeys as $key) {
$this->assertContains($key, array_keys($result),
"test that each key is present in `getQueryKeys`");
$closure = $this->getAggregator()->getLabels($key, array($result[$key]), $data);
$this->assertTrue(is_callable($closure, false),
"test that the `getLabels` for key is a callable");
$this->assertTrue(is_string((string) call_user_func($closure, $result[$key])),
sprintf("test that the callable return by `getLabels` for key %s "
. "is a string or an be converted to a string", $key));
$this->assertContains(
$key,
array_keys($result),
'test that each key is present in `getQueryKeys`'
);
$closure = $this->getAggregator()->getLabels($key, [$result[$key]], $data);
$this->assertTrue(
is_callable($closure, false),
'test that the `getLabels` for key is a callable'
);
$this->assertTrue(
is_string((string) call_user_func($closure, $result[$key])),
sprintf('test that the callable return by `getLabels` for key %s '
. 'is a string or an be converted to a string', $key)
);
$this->assertTrue(
// conditions
is_string((string) call_user_func($closure, '_header'))
&& !empty(call_user_func($closure, '_header'))
&& call_user_func($closure, '_header') !== '_header',
// message
sprintf("Test that the callable return by `getLabels` for key %s "
. "can provide an header", $key)
);
sprintf('Test that the callable return by `getLabels` for key %s '
. 'can provide an header', $key)
);
}
}
/**
* test the alteration of query by the filter
*
* @dataProvider dataProviderAlterQuery
* @param QueryBuilder $query
* @param type $data
* test the `getTitle` method.
*/
public function testAlterQuery(QueryBuilder $query, $data)
public function testGetTitle()
{
// retains informations about query
$nbOfFrom = $query->getDQLPart('from') !== null ?
count($query->getDQLPart('from')) : 0;
$nbOfWhere = $query->getDQLPart('where') !== null ?
$query->getDQLPart('where')->count() : 0;
$nbOfSelect = $query->getDQLPart('select') !== null ?
count($query->getDQLPart('select')) : 0;
$this->getAggregator()->alterQuery($query, $data);
$this->assertGreaterThanOrEqual(
$nbOfFrom,
$query->getDQLPart('from') !== null ? count($query->getDQLPart('from')) : 0,
"Test that there are equal or more 'from' clause after that the filter has
altered the query");
$this->assertGreaterThanOrEqual(
$nbOfWhere,
$query->getDQLPart('where') !== null ? $query->getDQLPart('where')->count() : 0,
"Test that there are equal or more 'where' clause after that the filter has"
. "altered the query");
$this->assertGreaterThanOrEqual(
$nbOfSelect,
$query->getDQLPart('select') !== null ? count($query->getDQLPart('select')) : 0,
"Test that the filter has no altered the 'select' part of the query");
$title = $this->getAggregator()->getTitle();
$this->assertInternalType('string', $title);
$this->assertNotEmpty(
$title,
'test that the title is not empty'
);
}
}

View File

@@ -1,28 +1,22 @@
<?php
/*
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test\Export;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\NativeQuery;
use Symfony\Component\Security\Core\Role\Role;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\QueryBuilder;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Role\Role;
use Traversable;
/**
* This class provide a set of tests for exports.
@@ -30,41 +24,35 @@ use Symfony\Component\HttpFoundation\Request;
* The tests provided by this class will check basic things, like
* the type of value are conform to the expected, etc.
*
* @author julien.fastre@champs-libres.coop
* @internal
*/
abstract class AbstractExportTest extends WebTestCase
{
use PrepareClientTrait;
public function dataProviderGetQueryKeys()
{
foreach ($this->getFormData() as $data) {
yield [$data];
}
}
/**
* Create an instance of the report to test
*
* @return \Chill\MainBundle\Export\ExportInterface an instance of the export to test
* create data for `ìnitiateQuery` method.
*/
public abstract function getExport();
public function dataProviderInitiateQuery()
{
$acl = $this->getAcl();
foreach ($this->getModifiersCombination() as $modifiers) {
foreach ($this->getFormData() as $data) {
yield [$modifiers, $acl, $data];
}
}
}
/**
* Create possible combinaison of data (produced by the form).
*
* This data will be used to generate data providers using this data.
*
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
public abstract function getFormData();
/**
* get the possible modifiers which could apply in combination to this
* export.
* .
*
* @return array of string[] an array which contains an array of possible modifiers. Example : `array( array('modifier_1', 'modifier_2'), array('modifier_1'), ...)`
*/
abstract public function getModifiersCombination();
/**
* Return an array usable as ACL
* Return an array usable as ACL.
*
* If this method is overridden, the returned result must be an array
* with this form :
@@ -77,11 +65,10 @@ abstract class AbstractExportTest extends WebTestCase
* )
* );
* ```
*
*/
public function getACL()
{
if (static::$kernel === null) {
if (null === static::$kernel) {
static::bootKernel();
}
@@ -94,169 +81,103 @@ abstract class AbstractExportTest extends WebTestCase
->findAll();
if (count($centers) === 0) {
throw new \RuntimeException("No center found. Did you forget to "
. "run `doctrine:fixtures:load` command before ?");
}
if (count($circles) === 0) {
throw new \RuntimeException("No circle found. Did you forget to "
. "run `doctrine:fixtures:load` command before ?");
throw new RuntimeException('No center found. Did you forget to '
. 'run `doctrine:fixtures:load` command before ?');
}
return array([
if (count($circles) === 0) {
throw new RuntimeException('No circle found. Did you forget to '
. 'run `doctrine:fixtures:load` command before ?');
}
return [[
'center' => $centers[0],
'circles' => [
$circles
]]);
}
/**
* Test that the getType method return a string
*/
public function testGetType()
{
$export = $this->getExport();
$this->assertInternalType('string', $export->getType(),
"Assert that the `getType` method return a string");
$this->assertNotEmpty($export->getType(), "Assert that the `getType` method"
. " does not return an empty string.");
}
/**
* Test that the description is not empty
*/
public function testGetDescription()
{
$export = $this->getExport();
$this->assertInternalType('string', $export->getDescription(),
"Assert that the `getDescription` method return a string");
$this->assertNotEmpty($export->getDescription(),
"Assert that the `getDescription` method does not return an empty "
. "string.");
}
/**
* create data for `ìnitiateQuery` method
*/
public function dataProviderInitiateQuery()
{
$acl = $this->getAcl();
foreach($this->getModifiersCombination() as $modifiers) {
foreach($this->getFormData() as $data) {
yield array($modifiers, $acl, $data);
}
}
}
public function dataProviderGetQueryKeys()
{
foreach($this->getFormData() as $data) {
yield array($data);
}
$circles,
], ]];
}
/**
* Create an instance of the report to test.
*
* test that the query returned is a QueryBuilder or a NativeQuery.
*
* If the query is a QueryBuilder, test that select and from is not empty.
*
* If the query is a native sql, test the query is not empty (length is
* > 0).
*
* @dataProvider dataProviderInitiateQuery
* @return \Chill\MainBundle\Export\ExportInterface an instance of the export to test
*/
public function testInitiateQuery($modifiers, $acl, $data)
{
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
$this->assertTrue($query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf("Assert that the returned query is an instance of %s or %s",
QueryBuilder::class, Query::class));
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(1, count($query->getDQLPart('select')),
"assert there is at least one 'select' part");
$this->assertGreaterThanOrEqual(1, count($query->getDQLPart('from')),
"assert there is at least one 'from' part");
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty($query->getSQL(),
"check that the SQL query is not empty");
}
}
abstract public function getExport();
/**
* Test that supportsModifier return :
* Create possible combinaison of data (produced by the form).
*
* - an array of string, if the query is a QueryBuilder ;
* - nothing, if the query is a native SQL
* This data will be used to generate data providers using this data.
*
* @dataProvider dataProviderInitiateQuery
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
public function testSupportsModifier($modifiers, $acl, $data)
{
$export = $this->getExport();
$query = $export->initiateQuery($modifiers, $acl, $data);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly('string', $export->supportsModifiers(),
"Test that the `supportsModifiers` method returns only strings");
} elseif ($query instanceof NativeQuery) {
$this->assertTrue($export->supportsModifiers() === null ||
count($export->supportsModifiers()) === 0,
"Test that the `supportsModifier` methods returns null or an empty array");
}
}
abstract public function getFormData();
/**
* Test required role is an instance of Role
* get the possible modifiers which could apply in combination to this
* export.
* .
*
* @return array of string[] an array which contains an array of possible modifiers. Example : `array( array('modifier_1', 'modifier_2'), array('modifier_1'), ...)`
*/
public function testRequiredRole()
{
$role = $this->getExport()->requiredRole();
$this->assertInstanceOf(Role::class, $role,
sprintf("test that the returned value of `requiredRole` is an instance "
. "of %s", Role::class));
}
abstract public function getModifiersCombination();
/**
* Test the formatters type are string
* Test the formatters type are string.
*/
public function testGetAllowedFormattersType()
{
$formattersTypes = $this->getExport()->getAllowedFormattersTypes();
$this->assertContainsOnly("string", $formattersTypes,
"Test that the method `getAllowedFormattersTypes` returns an array of string");
$this->assertContainsOnly(
'string',
$formattersTypes,
'Test that the method `getAllowedFormattersTypes` returns an array of string'
);
}
/**
* Test that the query keys are strings
* Test that the description is not empty.
*/
public function testGetDescription()
{
$export = $this->getExport();
$this->assertInternalType(
'string',
$export->getDescription(),
'Assert that the `getDescription` method return a string'
);
$this->assertNotEmpty(
$export->getDescription(),
'Assert that the `getDescription` method does not return an empty '
. 'string.'
);
}
/**
* Test that the query keys are strings.
*
* @param array $data
* @dataProvider dataProviderGetQueryKeys
*/
public function testGetQueryKeys(array $data)
{
$queryKeys = $this->getExport()->getQueryKeys($data);
$this->assertContainsOnly("string", $queryKeys,
"test that the query keys returned by `getQueryKeys` are only strings");
$this->assertGreaterThanOrEqual(1, count($queryKeys),
"test that there are at least one query key returned");
$this->assertContainsOnly(
'string',
$queryKeys,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
count($queryKeys),
'test that there are at least one query key returned'
);
}
/**
*
* Test that
* Test that.
*
* - the results have a correct form (are arrays or traversable)
* - each key in a row are present in getQueryKeys ;
@@ -266,7 +187,6 @@ abstract class AbstractExportTest extends WebTestCase
*
* @param string[] $modifiers
* @param array $acl
* @param array $data
*
* @dataProvider dataProviderInitiateQuery
*/
@@ -276,7 +196,7 @@ abstract class AbstractExportTest extends WebTestCase
// due to the fact that testing both methods use the same tools.
$queryKeys = $this->getExport()->getQueryKeys($data);
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
// limit the result for the query for performance reason (only for QueryBuilder,
// not possible in NativeQuery)
@@ -284,33 +204,45 @@ abstract class AbstractExportTest extends WebTestCase
$query->setMaxResults(1);
}
$results = $this->getExport()->getResult($query, $data);
$results = $this->getExport()->getResult($query, $data);
$this->assertInternalType('array', $results,
"assert that the returned result is an array");
$this->assertInternalType(
'array',
$results,
'assert that the returned result is an array'
);
if (count($results) === 0) {
$this->markTestIncomplete("The result is empty. We cannot process tests "
. "on results");
$this->markTestIncomplete('The result is empty. We cannot process tests '
. 'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue( $result instanceof \Traversable || is_array($result),
"test that each row in the result is traversable or an array");
$this->assertTrue(
$result instanceof Traversable || is_array($result),
'test that each row in the result is traversable or an array'
);
foreach ($result as $key => $value) {
$this->assertContains($key, $queryKeys,
"test that each key is present in `getQueryKeys`");
$this->assertContains(
$key,
$queryKeys,
'test that each key is present in `getQueryKeys`'
);
$closure = $this->getExport()->getLabels($key, array($value), $data);
$closure = $this->getExport()->getLabels($key, [$value], $data);
$this->assertTrue(is_callable($closure, false),
"test that the `getLabels` for key is a callable");
$this->assertTrue(is_string((string) call_user_func($closure, $value)),
sprintf("test that the callable return by `getLabels` for key %s "
. "is a string or an be converted to a string", $key));
$this->assertTrue(
is_callable($closure, false),
'test that the `getLabels` for key is a callable'
);
$this->assertTrue(
is_string((string) call_user_func($closure, $value)),
sprintf('test that the callable return by `getLabels` for key %s '
. 'is a string or an be converted to a string', $key)
);
$this->assertTrue(
// conditions
@@ -318,24 +250,85 @@ abstract class AbstractExportTest extends WebTestCase
&& !empty(call_user_func($closure, '_header'))
&& call_user_func($closure, '_header') !== '_header',
// message
sprintf("Test that the callable return by `getLabels` for key %s "
. "can provide an header", $key)
);
sprintf('Test that the callable return by `getLabels` for key %s '
. 'can provide an header', $key)
);
}
}
/**
* Test that the getType method return a string.
*/
public function testGetType()
{
$export = $this->getExport();
$this->assertInternalType(
'string',
$export->getType(),
'Assert that the `getType` method return a string'
);
$this->assertNotEmpty($export->getType(), 'Assert that the `getType` method'
. ' does not return an empty string.');
}
/**
* test that the query returned is a QueryBuilder or a NativeQuery.
*
* If the query is a QueryBuilder, test that select and from is not empty.
*
* If the query is a native sql, test the query is not empty (length is
* > 0).
*
* @dataProvider dataProviderInitiateQuery
*
* @param mixed $modifiers
* @param mixed $acl
* @param mixed $data
*/
public function testInitiateQuery($modifiers, $acl, $data)
{
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
$this->assertTrue(
$query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf(
'Assert that the returned query is an instance of %s or %s',
QueryBuilder::class,
Query::class
)
);
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(
1,
count($query->getDQLPart('select')),
"assert there is at least one 'select' part"
);
$this->assertGreaterThanOrEqual(
1,
count($query->getDQLPart('from')),
"assert there is at least one 'from' part"
);
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty(
$query->getSQL(),
'check that the SQL query is not empty'
);
}
}
/**
* Test that the translated title of the export is present the list,
* and that the list of exports (under `/fr/exports/`) is still successfull
* and that the list of exports (under `/fr/exports/`) is still successfull.
*/
public function testListExportPage()
{
/* @var $client \Symfony\Component\BrowserKit\Client */
$client = $this->getClient();
$export = $this->getExport();
$prophet= new \Prophecy\Prophet;
$prophet = new \Prophecy\Prophet();
$container = static::$kernel->getContainer();
// store the locale in a request
@@ -343,16 +336,68 @@ abstract class AbstractExportTest extends WebTestCase
$request->setLocale('fr');
$container->get('request_stack')->push($request);
// translate the title
$title = $container->get('translator')->trans($export->getTitle());
$title = $container->get('translator')->trans($export->getTitle());
// performs the request to /fr/exports
$crawler = $client->request('GET', '/fr/exports/');
// and finally make tests
$this->assertTrue($client->getResponse()->isSuccessful(),
"test that the response of /fr/exports/ is successful");
$this->assertContains($title, $crawler->text(),
"test that the page /fr/exports/ contains the title of the "
. "exports ('$title')");
$this->assertTrue(
$client->getResponse()->isSuccessful(),
'test that the response of /fr/exports/ is successful'
);
$this->assertContains(
$title,
$crawler->text(),
'test that the page /fr/exports/ contains the title of the '
. "exports ('{$title}')"
);
}
/**
* Test required role is an instance of Role.
*/
public function testRequiredRole()
{
$role = $this->getExport()->requiredRole();
$this->assertInstanceOf(
Role::class,
$role,
sprintf('test that the returned value of `requiredRole` is an instance '
. 'of %s', Role::class)
);
}
/**
* Test that supportsModifier return :.
*
* - an array of string, if the query is a QueryBuilder ;
* - nothing, if the query is a native SQL
*
* @dataProvider dataProviderInitiateQuery
*
* @param mixed $modifiers
* @param mixed $acl
* @param mixed $data
*/
public function testSupportsModifier($modifiers, $acl, $data)
{
$export = $this->getExport();
$query = $export->initiateQuery($modifiers, $acl, $data);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly(
'string',
$export->supportsModifiers(),
'Test that the `supportsModifiers` method returns only strings'
);
} elseif ($query instanceof NativeQuery) {
$this->assertTrue(
$export->supportsModifiers() === null
|| count($export->supportsModifiers()) === 0,
'Test that the `supportsModifier` methods returns null or an empty array'
);
}
}
}

View File

@@ -1,190 +1,193 @@
<?php
/*
* Copyright (C) 2016 Champs Libres Cooperative <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test\Export;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Exception;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Helper to test filters
* Helper to test filters.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @internal
*/
abstract class AbstractFilterTest extends KernelTestCase
{
/**
*
* @var \Prophecy\Prophet
*/
protected $prophet;
/**
* Create a filter which will be used in tests.
*
* This method is always used after an eventuel `setUp` method.
*
* @return \Chill\MainBundle\Export\FilterInterface
*/
abstract public function getFilter();
/**
* Create possible combinaison of data (produced by the form).
*
* This data will be used to generate data providers using this data.
*
* As all data providers, this method is executed **before** calling
* the `setUp` method.
*
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
public abstract function getFormData();
/**
* Return an array with different minimal query builders
*
* As all data providers, this method is executed **before** calling
* the `setUp` method.
*
* @return QueryBuilder[] an array of query builder
*/
public abstract function getQueryBuilders();
public function dataProviderAlterQuery()
{
foreach ($this->getQueryBuilders() as $qb) {
foreach ($this->getFormData() as $data) {
yield array($qb, $data);
}
}
}
public function dataProviderDescriptionAction()
{
foreach ($this->getFormData() as $data) {
yield array($data);
}
}
public function setUp()
{
$this->prepareProphet();
}
public function testApplyOn()
public function dataProviderAlterQuery()
{
$filter = $this->getFilter();
$this->assertInternalType('string', $filter->applyOn());
foreach ($this->getQueryBuilders() as $qb) {
foreach ($this->getFormData() as $data) {
yield [$qb, $data];
}
}
}
public function dataProviderDescriptionAction()
{
foreach ($this->getFormData() as $data) {
yield [$data];
}
}
/**
* test the alteration of query by the filter
*
* Create a filter which will be used in tests.
*
* This method is always used after an eventuel `setUp` method.
*
* @return \Chill\MainBundle\Export\FilterInterface
*/
abstract public function getFilter();
/**
* Create possible combinaison of data (produced by the form).
*
* This data will be used to generate data providers using this data.
*
* As all data providers, this method is executed **before** calling
* the `setUp` method.
*
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/
abstract public function getFormData();
/**
* Return an array with different minimal query builders.
*
* As all data providers, this method is executed **before** calling
* the `setUp` method.
*
* @return QueryBuilder[] an array of query builder
*/
abstract public function getQueryBuilders();
/**
* test the alteration of query by the filter.
*
* @dataProvider dataProviderAlterQuery
* @param QueryBuilder $query
*
* @param type $data
*/
public function testAlterQuery(QueryBuilder $query, $data)
{
// retains informations about query
$nbOfFrom = $query->getDQLPart('from') !== null ?
$nbOfFrom = $query->getDQLPart('from') !== null ?
count($query->getDQLPart('from')) : 0;
$nbOfWhere = $query->getDQLPart('where') !== null ?
$nbOfWhere = $query->getDQLPart('where') !== null ?
$query->getDQLPart('where')->count() : 0;
$nbOfSelect = $query->getDQLPart('select') !== null ?
count($query->getDQLPart('select')) : 0;
$this->getFilter()->alterQuery($query, $data);
$this->assertGreaterThanOrEqual(
$nbOfFrom,
$nbOfFrom,
$query->getDQLPart('from') !== null ? count($query->getDQLPart('from')) : 0,
"Test that there are equal or more 'from' clause after that the filter has
altered the query"
);
);
$this->assertGreaterThanOrEqual(
$nbOfWhere,
$nbOfWhere,
$query->getDQLPart('where') !== null ? $query->getDQLPart('where')->count() : 0,
"Test that there are equal or more 'where' clause after that the filter has"
. "altered the query"
);
. 'altered the query'
);
$this->assertEquals(
$nbOfSelect,
$nbOfSelect,
$query->getDQLPart('select') !== null ? count($query->getDQLPart('select')) : 0,
"Test that the filter has no altered the 'select' part of the query"
);
);
}
public function testGetTitle()
public function testApplyOn()
{
$title = $this->getFilter()->getTitle();
$this->assertInternalType('string', $title);
$this->assertNotEmpty($title,
"test that the title is not empty");
$filter = $this->getFilter();
$this->assertInternalType('string', $filter->applyOn());
}
/**
*
* @dataProvider dataProviderDescriptionAction
*
* @param array $data
*/
public function testDescriptionAction($data)
{
$description = $this->getFilter()->describeAction($data);
$this->assertTrue(
is_string($description) || is_array($description),
"test that the description is a string or an array"
);
'test that the description is a string or an array'
);
if (is_string($description)) {
$this->assertNotEmpty($description,
"test that the description is not empty");
$this->assertNotEmpty(
$description,
'test that the description is not empty'
);
} elseif (is_array($description)) {
$this->assertInternalType('string', $description[0],
"test that the first element in the description array is a string");
$this->assertInternalType(
'string',
$description[0],
'test that the first element in the description array is a string'
);
// test that the message is translated
try {
if (static::$kernel === null) {
if (null === static::$kernel) {
static::bootKernel();
}
$catalogue = static::$kernel->getContainer()
->get('translator')
->getCatalogue();
} catch (\Exception $ex) {
} catch (Exception $ex) {
$this->markTestIncomplete(
sprintf("This test is incomplete due to %s thrown by 'translator' : %s, "
. "complete stack : %s", get_class($ex), $ex->getMessage(),
$ex->getTraceAsString()));
sprintf(
"This test is incomplete due to %s thrown by 'translator' : %s, "
. 'complete stack : %s',
get_class($ex),
$ex->getMessage(),
$ex->getTraceAsString()
)
);
}
$this->assertTrue($catalogue->has($description[0],
isset($description[2]) ? $description[2] : 'messages'),
sprintf("Test that the message returned by getDescriptionAction is "
. "present in the catalogue of translations. HINT : check that \"%s\" "
. "is correctly translated", $description[0]));
$this->assertTrue(
$catalogue->has(
$description[0],
$description[2] ?? 'messages'
),
sprintf('Test that the message returned by getDescriptionAction is '
. 'present in the catalogue of translations. HINT : check that "%s" '
. 'is correctly translated', $description[0])
);
}
}
public function testGetTitle()
{
$title = $this->getFilter()->getTitle();
$this->assertInternalType('string', $title);
$this->assertNotEmpty(
$title,
'test that the title is not empty'
);
}
}