Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T15726 align flash filter #15734

Merged
merged 13 commits into from
Oct 24, 2021
Prev Previous commit
Next Next commit
more tests; alignment with phalcon
  • Loading branch information
niden committed Oct 23, 2021
commit d9840983ecaacb3c103eac4eb440bfe70d112b34
6 changes: 4 additions & 2 deletions tests/unit/Filter/Filter/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

namespace Phalcon\Tests\Unit\Filter\Filter;

use Phalcon\Filter;
use Phalcon\Filter\Exception;
use Phalcon\Filter\Filter;
use UnitTester;

class ConstructCest
{
/**
* Tests Phalcon\Filter\Locator :: __construct() - empty
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-01-19
* @since 2020-09-09
*/
public function filterFilterConstruct(UnitTester $I)
{
Expand Down
33 changes: 11 additions & 22 deletions tests/unit/Filter/Filter/CustomCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Phalcon\Tests\Unit\Filter\Filter;

use Phalcon\Filter;
use Phalcon\Filter\Filter;
use Phalcon\Tests\Fixtures\Filter\Sanitize\IPv4;
use UnitTester;

Expand All @@ -22,27 +22,26 @@ class CustomCest
/**
* Tests Phalcon\Filter :: custom has()
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-01-19
* @since 2020-09-09
*/
public function filterFilterCustomHas(UnitTester $I)
{
$I->wantToTest('Filter\Filter - custom has()');

$I->assertTrue(
(new Filter(['ipv4' => IPv4::class]))->has('ipv4')
);
$actual = (new Filter(['ipv4' => IPv4::class]))->has('ipv4');
$I->assertTrue($actual);
}

/**
* Tests Phalcon\Filter :: custom sanitizer
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-01-19
* @author Phalcon Team <team@phalcon.io>
* @since 2020-09-09
*/
public function filterFilterCustomSanitizer(UnitTester $I)
{
Expand All @@ -51,18 +50,8 @@ public function filterFilterCustomSanitizer(UnitTester $I)
/** @var IPv4 $sanitizer */
$sanitizer = (new Filter(['ipv4' => IPv4::class]))->get('ipv4');

$I->assertInstanceOf(
IPv4::class,
$sanitizer
);

$I->assertEquals(
'127.0.0.1',
$sanitizer('127.0.0.1')
);

$I->assertFalse(
$sanitizer('127.0.0')
);
$I->assertInstanceOf(IPv4::class, $sanitizer);
$I->assertEquals('127.0.0.1', $sanitizer('127.0.0.1'));
$I->assertFalse($sanitizer('127.0.0'));
}
}
22 changes: 11 additions & 11 deletions tests/unit/Filter/Filter/GetSetHasCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Phalcon\Tests\Unit\Filter\Filter;

use Closure;
use Phalcon\Filter;
use Phalcon\Filter\Filter;
use Phalcon\Tests\Fixtures\Service\HelloService;
use UnitTester;

Expand All @@ -23,10 +23,10 @@ class GetSetHasCest
/**
* Tests Phalcon\Filter :: get()/set()/has() - has()
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-01-19
* @since 2020-09-09
*/
public function filterFilterGetSetHasHas(UnitTester $I)
{
Expand All @@ -46,10 +46,10 @@ public function filterFilterGetSetHasHas(UnitTester $I)
/**
* Tests Phalcon\Filter :: get()/set()/has() - get()
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-09-09
*/
public function filterFilterLocatorGetSetHasGet(UnitTester $I)
{
Expand All @@ -72,10 +72,10 @@ public function filterFilterLocatorGetSetHasGet(UnitTester $I)
/**
* Tests Phalcon\Filter :: get()/set()/has() - get() same
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-09-09
*/
public function filterFilterGetSetHasGetSame(UnitTester $I)
{
Expand All @@ -98,10 +98,10 @@ public function filterFilterGetSetHasGetSame(UnitTester $I)
/**
* Tests Phalcon\Filter :: get()/set()/has() - set()
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-09-09
*/
public function filterFilterGetSetHasSet(UnitTester $I)
{
Expand All @@ -124,10 +124,10 @@ function () {
/**
* Tests Phalcon\Filter :: get()/set()/has() - set() closure
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-09-25
* @since 2020-09-09
*/
public function filterFilterGetSetHasSetClosure(UnitTester $I)
{
Expand Down
130 changes: 67 additions & 63 deletions tests/unit/Filter/Filter/SanitizeMultipleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Phalcon\Tests\Unit\Filter\Filter;

use Codeception\Example;
use Phalcon\Filter\FilterFactory;
use UnitTester;

Expand All @@ -24,95 +25,48 @@
class SanitizeMultipleCest
{
/**
* Tests sanitizing string with filters
* Tests sanitizing values
*
* @author Phalcon Team <team@phalcon.io>
* @since 2014-09-30
*/
public function filterFilterSanitizeStringWithMultipleFilters(UnitTester $I)
{
$locator = new FilterFactory();
$filter = $locator->newInstance();

$value = ' lol<<< ';
$expected = 'lol';
$actual = $filter->sanitize($value, ['string', 'trim']);
$I->assertEquals($expected, $actual);
}

/**
* Tests sanitizing array with filters
* @dataProvider getExamples
*
* @author Phalcon Team <team@phalcon.io>
* @since 2014-09-30
*/
public function filterFilterSanitizeArray(UnitTester $I)
{
$locator = new FilterFactory();
$filter = $locator->newInstance();

$value = [' 1 ', ' 2', '3 '];
$expected = ['1', '2', '3'];
$actual = $filter->sanitize($value, 'trim');
$I->assertEquals($expected, $actual);
}

/**
* Tests sanitizing array with multiple filters
* @param UnitTester $I
* @param Example $example
*
* @author Phalcon Team <team@phalcon.io>
* @since 2014-09-30
* @since 2021-10-23
*/
public function filterFilterSanitizeArrayWithMultipleFilters(UnitTester $I)
public function filterFilterSanitize(UnitTester $I, Example $example)
{
$locator = new FilterFactory();
$filter = $locator->newInstance();

$value = [' <a href="a">1</a> ', ' <h1>2</h1>', '<p>3</p>'];
$expected = ['1', '2', '3'];
$actual = $filter->sanitize($value, ['trim', 'striptags']);
$I->assertEquals($expected, $actual);
}
$I->wantToTest('Filter\Filter - sanitize() - ' . $example['label']);

/**
* Tests sanitizing array with multiple filters and more parameters
*
* @author Phalcon Team <team@phalcon.io>
* @since 2014-09-30
*/
public function filterFilterSanitizeWithMultipleFiltersMoreParameters(UnitTester $I)
{
$locator = new FilterFactory();
$filter = $locator->newInstance();

$value = ' mary had a little lamb ';
$filters = [
'trim',
'replace' => [' ', '-'],
'remove' => ['mary'],
];
$expected = '-had-a-little-lamb';
$actual = $filter->sanitize($value, $filters);
$source = $example['source'];
$expected = $example['expected'];
$filters = $example['filters'];
$actual = $filter->sanitize($source, $filters);
$I->assertEquals($expected, $actual);
}

/**
* Tests sanitizing array with multiple filters and one not existing
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-02-22
* @since 2020-09-09
*/
public function filterFilterSanitizeWithMultipleFiltersNotExisting(UnitTester $I)
{
$locator = new FilterFactory();
$filter = $locator->newInstance();

$value = ' mary had a little lamb ';
$filters = [
$value = ' mary had a little lamb ';
$filters = [
'trim',
'something',
];
$expected = 'had a little lamb';

$error = [];
set_error_handler(
Expand All @@ -135,4 +89,54 @@ function ($number, $message, $file, $line) use (&$error) {
$error['message']
);
}

/**
* @return array<array-key, array<string, mixed>>
*/
private function getExamples(): array
{
return [
[
'label' => 'null value',
'source' => null,
'filters' => [
'string',
'trim',
],
'expected' => null,
],
[
'label' => 'string with filters',
'source' => ' lol<<< ',
'filters' => [
'string',
'trim',
],
'expected' => 'lol',
],
[
'label' => 'array with filters',
'source' => [' 1 ', ' 2', '3 '],
'filters' => 'trim',
'expected' => ['1', '2', '3'],
],
[
'label' => 'array with multiple filters',
'source' => [' <a href="a">1</a> ', ' <h1>2</h1>', '<p>3</p>'],
'filters' => ['striptags', 'trim'],
'expected' => ['1', '2', '3'],
],
[
'label' => 'multiple filters and more parameters',
'source' => ' mary had a little lamb ',
'filters' => [
'trim',
'replace' => [' ', '-'],
'remove' => ['mary'],
],
'expected' => '-had-a-little-lamb',
],
];
}

}
15 changes: 8 additions & 7 deletions tests/unit/Filter/FilterFactory/NewInstanceCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Phalcon\Tests\Unit\Filter\FilterFactory;

use Codeception\Example;
use Phalcon\Filter;
use Phalcon\Filter\Filter;
use Phalcon\Filter\FilterFactory;
use Phalcon\Filter\FilterInterface;
use Phalcon\Filter\Sanitize\AbsInt;
Expand Down Expand Up @@ -45,28 +45,29 @@ class NewInstanceCest
/**
* Tests Phalcon\Filter\FilterFactory :: newInstance()
*
* @param UnitTester $I
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-09-09
*/
public function filterFilterFactoryNewInstance(UnitTester $I)
{
$I->wantToTest('Filter\FilterFactory - newInstance()');
$factory = new FilterFactory();
$factory = new FilterFactory();

$I->assertInstanceOf(FilterInterface::class, $factory->newInstance());
}

/**
* Tests Phalcon\Filter\FilterFactory :: newInstance() - services
*
* @param UnitTester $I
* @param Example $example
* @param UnitTester $I
* @param Example $example
*
* @dataProvider getData
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2020-09-09
*/
public function filterFilterFactoryNewInstanceServices(UnitTester $I, Example $example)
{
Expand Down
Loading