From be301a1e03a82ede58f42bdd31fc3a7076a838a0 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 14:30:27 +0300 Subject: [PATCH 01/18] require PHPStan --- .gitignore | 1 + composer.json | 3 ++- phpstan.neon.dist | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon.dist diff --git a/.gitignore b/.gitignore index d157f2c..6980ff2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ phpunit.xml composer.lock .php_cs .php_cs.cache +phpstan.neon \ No newline at end of file diff --git a/composer.json b/composer.json index c9782fb..7f84bcb 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "phpunit/phpunit": "~4.8", "scrutinizer/ocular": "~1.3", "satooshi/php-coveralls": "^1.0", - "friendsofphp/php-cs-fixer": "~2.2" + "friendsofphp/php-cs-fixer": "~2.2", + "phpstan/phpstan": "^0.11.16" } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..31f6166 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,5 @@ +parameters: + level: 1 + paths: + - src + - tests From 268326f5aa80684818d2eb396cffd4441c8cb5c2 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 14:39:28 +0300 Subject: [PATCH 02/18] correct configure bundle --- src/DependencyInjection/Configuration.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2faddc5..9ad3f24 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -10,6 +10,7 @@ namespace GpsLab\Bundle\PaginationBundle\DependencyInjection; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -30,19 +31,14 @@ public function getConfigTreeBuilder() $root = $tree_builder->root('gpslab_pagination'); } - $root - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('max_navigate') - ->defaultValue(5) - ->end() - ->scalarNode('parameter_name') - ->defaultValue('page') - ->end() - ->scalarNode('template') - ->defaultValue('GpsLabPaginationBundle::pagination.html.twig') - ->end() - ->end(); + if (!$root instanceof ArrayNodeDefinition) { + throw new \RuntimeException(sprintf('Config root node must be a "Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition", given "%s".', get_class($root))); + } + + $root->addDefaultsIfNotSet(); + $root->children()->scalarNode('max_navigate')->defaultValue(5); + $root->children()->scalarNode('parameter_name')->defaultValue('page'); + $root->children()->scalarNode('template')->defaultValue('GpsLabPaginationBundle::pagination.html.twig'); return $tree_builder; } From fc8b0f050ff2b658fb605d14d4dd935dfb39bdc3 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 14:54:19 +0300 Subject: [PATCH 03/18] use real Request object in BuilderTest --- src/Service/Builder.php | 7 +-- tests/Service/BuilderTest.php | 92 ++++++++++++++--------------------- 2 files changed, 37 insertions(+), 62 deletions(-) diff --git a/src/Service/Builder.php b/src/Service/Builder.php index f28442b..3b8b221 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -10,7 +10,6 @@ namespace GpsLab\Bundle\PaginationBundle\Service; -use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\QueryBuilder; use GpsLab\Bundle\PaginationBundle\Exception\IncorrectPageNumberException; use GpsLab\Bundle\PaginationBundle\Exception\OutOfRangeException; @@ -70,8 +69,6 @@ public function paginate($total_pages = 1, $current_page = 1) * @param int $per_page Entities per page * @param int $current_page The current page number * - * @throws NonUniqueResultException - * * @return Configuration */ public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1) @@ -126,8 +123,6 @@ public function paginateRequest( * @param string $parameter_name Name of URL parameter for page number * @param int $reference_type The type of reference (one of the constants in UrlGeneratorInterface) * - * @throws NonUniqueResultException - * * @return Configuration */ public function paginateRequestQuery( @@ -185,7 +180,7 @@ private function configureFromRequest( $reference_type ) { $route = $request->get('_route'); - $route_params = array_merge($request->query->all(), $request->get('_route_params')); + $route_params = array_merge($request->query->all(), $request->get('_route_params', [])); unset($route_params[$parameter_name]); return $configuration diff --git a/tests/Service/BuilderTest.php b/tests/Service/BuilderTest.php index 3b0ed18..ec26ebb 100644 --- a/tests/Service/BuilderTest.php +++ b/tests/Service/BuilderTest.php @@ -15,7 +15,6 @@ use GpsLab\Bundle\PaginationBundle\Service\Builder; use GpsLab\Bundle\PaginationBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Routing\Router; -use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -26,11 +25,6 @@ class BuilderTest extends TestCase */ private $router; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|Request - */ - private $request; - /** * @var \PHPUnit_Framework_MockObject_MockObject|AbstractQuery */ @@ -41,6 +35,11 @@ class BuilderTest extends TestCase */ private $query_builder; + /** + * @var Request + */ + private $request; + /** * @var array */ @@ -52,11 +51,10 @@ class BuilderTest extends TestCase protected function setUp() { $this->router = $this->getMockNoConstructor('Symfony\Bundle\FrameworkBundle\Routing\Router'); - $this->request = $this->getMockNoConstructor('Symfony\Component\HttpFoundation\Request'); $this->query = $this->getMockAbstract('Doctrine\ORM\AbstractQuery', ['getSingleScalarResult']); $this->query_builder = $this->getMockNoConstructor('Doctrine\ORM\QueryBuilder'); - $this->request->query = new ParameterBag($this->query_params); + $this->request = new Request($this->query_params); } public function testDefaultPageLink() @@ -158,7 +156,9 @@ public function testPaginateQueryOutOfRange() */ public function testPaginateRequestIncorrectPage() { - $this->currentPage('foo', 'page'); + $this->request = new Request(array_merge($this->query_params, [ + 'page' => 'foo', + ])); $builder = new Builder($this->router, 5, 'page'); $builder->paginateRequest($this->request, 10); @@ -169,7 +169,9 @@ public function testPaginateRequestIncorrectPage() */ public function testPaginateRequestLowPageNumber() { - $this->currentPage(0, 'p'); + $this->request = new Request(array_merge($this->query_params, [ + 'p' => 0, + ])); $builder = new Builder($this->router, 5, 'page'); $builder->paginateRequest($this->request, 10, 'p'); @@ -180,7 +182,9 @@ public function testPaginateRequestLowPageNumber() */ public function testPaginateRequestOutOfRange() { - $this->currentPage(150, 'p'); + $this->request = new Request(array_merge($this->query_params, [ + 'p' => 150, + ])); $builder = new Builder($this->router, 5, 'page'); $builder->paginateRequest($this->request, 10, 'p'); @@ -195,20 +199,12 @@ public function testPaginateRequest() $route_params = ['foo' => 'baz', '_route_params' => 123]; $all_params = array_merge($this->query_params, $route_params); $reference_type = UrlGeneratorInterface::ABSOLUTE_URL; - - $this->currentPage(null, 'p'); - $this->request - ->expects($this->at(1)) - ->method('get') - ->with('_route') - ->willReturn($route) - ; - $this->request - ->expects($this->at(2)) - ->method('get') - ->with('_route_params') - ->willReturn($route_params) - ; + $this->request = new Request(array_merge($this->query_params, [ + 'p' => null, + ]), [], [ + '_route' => $route, + '_route_params' => $route_params, + ]); $that = $this; $this->router @@ -247,7 +243,9 @@ public function testPaginateRequest() */ public function testPaginateRequesQuerytIncorrectPage() { - $this->currentPage('foo', 'page'); + $this->request = new Request(array_merge($this->query_params, [ + 'page' => 'foo', + ])); $this->countQuery(10); $builder = new Builder($this->router, 5, 'page'); @@ -259,7 +257,9 @@ public function testPaginateRequesQuerytIncorrectPage() */ public function testPaginateRequestQueryLowPageNumber() { - $this->currentPage(0, 'p'); + $this->request = new Request(array_merge($this->query_params, [ + 'p' => 0, + ])); $this->countQuery(10); $builder = new Builder($this->router, 5, 'page'); @@ -271,7 +271,9 @@ public function testPaginateRequestQueryLowPageNumber() */ public function testPaginateRequestQueryOutOfRange() { - $this->currentPage(150, 'p'); + $this->request = new Request(array_merge($this->query_params, [ + 'p' => 150, + ])); $this->countQuery(10); $builder = new Builder($this->router, 5, 'page'); @@ -289,20 +291,12 @@ public function testPaginateRequestQuery() $route_params = ['foo' => 'baz', '_route_params']; $all_params = array_merge($this->query_params, $route_params); $reference_type = UrlGeneratorInterface::ABSOLUTE_URL; - - $this->currentPage($current_page, 'p'); - $this->request - ->expects($this->at(1)) - ->method('get') - ->with('_route') - ->willReturn($route) - ; - $this->request - ->expects($this->at(2)) - ->method('get') - ->with('_route_params') - ->willReturn($route_params) - ; + $this->request = new Request(array_merge($this->query_params, [ + 'p' => $current_page, + ]), [], [ + '_route' => $route, + '_route_params' => $route_params, + ]); $this->countQuery($total); $this->query_builder @@ -356,20 +350,6 @@ public function testPaginateRequestQuery() ); } - /** - * @param int $current_page - * @param string $parameter_name - */ - private function currentPage($current_page, $parameter_name = 'page') - { - $this->request - ->expects($this->at(0)) - ->method('get') - ->with($parameter_name) - ->willReturn($current_page) - ; - } - /** * @param int $total */ From 189aec008a2ff78752c8f2d4e7d3c83d45653e44 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 14:55:33 +0300 Subject: [PATCH 04/18] PHPStan level up to 3 --- phpstan.neon.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 31f6166..749a32d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 1 + level: 3 paths: - src - tests From 854f4142a74b9b5a07eb5916a0d44c807f1852f2 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 14:57:09 +0300 Subject: [PATCH 05/18] fix PHPStan errors for level 4 --- phpstan.neon.dist | 2 +- src/Service/Configuration.php | 2 +- src/Service/View.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 749a32d..3d47d11 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 3 + level: 4 paths: - src - tests diff --git a/src/Service/Configuration.php b/src/Service/Configuration.php index f25e3c7..17c3626 100644 --- a/src/Service/Configuration.php +++ b/src/Service/Configuration.php @@ -35,7 +35,7 @@ class Configuration private $current_page = 1; /** - * @var View + * @var View|null */ private $view; diff --git a/src/Service/View.php b/src/Service/View.php index 5968f80..af86d7d 100644 --- a/src/Service/View.php +++ b/src/Service/View.php @@ -36,7 +36,7 @@ class View implements \IteratorAggregate private $prev; /** - * @var Node + * @var Node|null */ private $current; @@ -144,7 +144,7 @@ public function getLast() } /** - * @return ArrayCollection + * @return ArrayCollection|Node[] */ public function getIterator() { From 622f286215947c23fd3853168fa3b6f1484c9d0e Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:00:20 +0300 Subject: [PATCH 06/18] change access for private class properties in tests --- tests/Service/ConfigurationTest.php | 2 +- tests/Service/NavigateRangeTest.php | 4 ++-- tests/Service/ViewTest.php | 6 +++--- tests/Twig/Extension/PaginationExtensionTest.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Service/ConfigurationTest.php b/tests/Service/ConfigurationTest.php index fc9b8ce..db24880 100644 --- a/tests/Service/ConfigurationTest.php +++ b/tests/Service/ConfigurationTest.php @@ -18,7 +18,7 @@ class ConfigurationTest extends TestCase /** * @var Configuration */ - protected $config; + private $config; protected function setUp() { diff --git a/tests/Service/NavigateRangeTest.php b/tests/Service/NavigateRangeTest.php index aec436d..de95a89 100644 --- a/tests/Service/NavigateRangeTest.php +++ b/tests/Service/NavigateRangeTest.php @@ -19,12 +19,12 @@ class NavigateRangeTest extends TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject|Configuration */ - protected $config; + private $config; /** * @var NavigateRange */ - protected $range; + private $range; protected function setUp() { diff --git a/tests/Service/ViewTest.php b/tests/Service/ViewTest.php index fe0cd2d..a0d91b7 100644 --- a/tests/Service/ViewTest.php +++ b/tests/Service/ViewTest.php @@ -22,17 +22,17 @@ class ViewTest extends TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject|Configuration */ - protected $config; + private $config; /** * @var \PHPUnit_Framework_MockObject_MockObject|NavigateRange */ - protected $range; + private $range; /** * @var View */ - protected $view; + private $view; protected function setUp() { diff --git a/tests/Twig/Extension/PaginationExtensionTest.php b/tests/Twig/Extension/PaginationExtensionTest.php index aba496a..27727a6 100644 --- a/tests/Twig/Extension/PaginationExtensionTest.php +++ b/tests/Twig/Extension/PaginationExtensionTest.php @@ -19,12 +19,12 @@ class PaginationExtensionTest extends TestCase /** * @var PaginationExtension */ - protected $extension; + private $extension; /** * @var string */ - protected $template = 'foo'; + private $template = 'foo'; protected function setUp() { From 06d4e291bd772138cdab95f1242cd87579017f33 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:17:25 +0300 Subject: [PATCH 07/18] not run PHPStan for tests because it require phpstan/phpstan-phpunit and PHP >= 7.0 --- phpstan.neon.dist | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 3d47d11..d94e019 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,4 +2,5 @@ parameters: level: 4 paths: - src - - tests + # require phpstan/phpstan-phpunit and PHP >= 7.0 + #- tests From f21310e669224ad51a851eaac0cb0607c66e3d1c Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:19:30 +0300 Subject: [PATCH 08/18] fix PHPStan errors for level 7 --- phpstan.neon.dist | 2 +- src/Service/Builder.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index d94e019..a3282cb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 4 + level: 7 paths: - src # require phpstan/phpstan-phpunit and PHP >= 7.0 diff --git a/src/Service/Builder.php b/src/Service/Builder.php index 3b8b221..4d3cce7 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -80,7 +80,7 @@ public function paginateQuery(QueryBuilder $query, $per_page, $current_page = 1) ->getSingleScalarResult() ; - $total_pages = ceil($total / $per_page); + $total_pages = (int) ceil($total / $per_page); $current_page = $this->validateCurrentPage($current_page, $total_pages); $query @@ -154,12 +154,12 @@ private function validateCurrentPage($current_page, $total_pages) return 1; } - if (!is_numeric($current_page)) { + if (!ctype_digit($current_page)) { throw IncorrectPageNumberException::incorrect($current_page); } if ($current_page < 1 || $current_page > $total_pages) { - throw OutOfRangeException::out($current_page, $total_pages); + throw OutOfRangeException::out((int) $current_page, $total_pages); } return (int) $current_page; From 7a689792e9a7cd07b3bf89bbec88a5b79456a575 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:20:11 +0300 Subject: [PATCH 09/18] run phpstan analyse in CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 24d3263..ed23494 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,6 +55,7 @@ before_script: - COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction --no-scripts --no-progress script: + - vendor/bin/phpstan analyse - vendor/bin/phpunit --coverage-clover build/coverage-clover.xml after_script: From a951ec7528b7ed4060911f9e28f3c46c89d04e5a Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:46:08 +0300 Subject: [PATCH 10/18] not require PHPStan --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index e99a5d2..2b5c4c5 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,6 @@ "scrutinizer/ocular": "~1.3", "satooshi/php-coveralls": "^1.0", "friendsofphp/php-cs-fixer": "~2.2", - "phpstan/phpstan": "^0.11.16", "roave/security-advisories": "dev-master" } } From bd5b5fb4177fa6b02e41c92e52fc4663078e3c21 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:46:45 +0300 Subject: [PATCH 11/18] install PHPStan by Travis CI --- .travis.yml | 6 +++++- phpstan.neon.dist | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index ed23494..a246a42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,6 +43,8 @@ matrix: env: TWIG_VERSION=1.* - php: 7.0 env: TWIG_VERSION=2.* + - php: 7.0 + env: PHPSTAN_VERSION=0.11.* PHPSTAN_PHPUNIT_VERSION=0.9.* PHPUNIT_VERSION=6.3.* before_install: - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; @@ -52,10 +54,12 @@ before_script: - if [ "$DOCTRINE_VERSION" != "" ]; then composer require "doctrine/orm:${DOCTRINE_VERSION}" --dev --no-update; fi; - if [ "$TWIG_VERSION" != "" ]; then composer require "twig/twig:${TWIG_VERSION}" --dev --no-update; fi; - if [ "$PHPUNIT_VERSION" != "" ]; then composer require "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update; fi; + - if [ "$PHPSTAN_VERSION" != "" ]; then composer require "phpstan/phpstan:${PHPSTAN_VERSION}" --dev --no-update; fi; + - if [ "$PHPSTAN_PHPUNIT_VERSION" != "" ]; then composer require "phpstan/phpstan-phpunit:${PHPSTAN_PHPUNIT_VERSION}" --dev --no-update; fi; - COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction --no-scripts --no-progress script: - - vendor/bin/phpstan analyse + - if [ "$PHPSTAN_VERSION" != "" ]; then ./vendor/bin/phpstan analyse; fi; - vendor/bin/phpunit --coverage-clover build/coverage-clover.xml after_script: diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a3282cb..7c87780 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,5 +2,4 @@ parameters: level: 7 paths: - src - # require phpstan/phpstan-phpunit and PHP >= 7.0 - #- tests + - tests From 56ebd908ed8e0b1bc7ec7c43534c370f76a46a27 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 15:53:23 +0300 Subject: [PATCH 12/18] correct resolve type of $current_page --- src/Service/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/Builder.php b/src/Service/Builder.php index 4d3cce7..4c05835 100644 --- a/src/Service/Builder.php +++ b/src/Service/Builder.php @@ -154,7 +154,7 @@ private function validateCurrentPage($current_page, $total_pages) return 1; } - if (!ctype_digit($current_page)) { + if (!is_int($current_page) && (!is_string($current_page) || !ctype_digit($current_page))) { throw IncorrectPageNumberException::incorrect($current_page); } From a09741426faf4bbe7b2813282af3cb86f1539cb9 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 16:08:44 +0300 Subject: [PATCH 13/18] run PHPStan on PHP 7.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a246a42..c668067 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,7 +43,7 @@ matrix: env: TWIG_VERSION=1.* - php: 7.0 env: TWIG_VERSION=2.* - - php: 7.0 + - php: 7.1 env: PHPSTAN_VERSION=0.11.* PHPSTAN_PHPUNIT_VERSION=0.9.* PHPUNIT_VERSION=6.3.* before_install: From 2796bb9fc0371baa58a12713e40249df833b0902 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 16:49:41 +0300 Subject: [PATCH 14/18] change version of PHPStan in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c668067..9e2ac0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,7 +44,7 @@ matrix: - php: 7.0 env: TWIG_VERSION=2.* - php: 7.1 - env: PHPSTAN_VERSION=0.11.* PHPSTAN_PHPUNIT_VERSION=0.9.* PHPUNIT_VERSION=6.3.* + env: PHPSTAN_VERSION=0.9.* PHPSTAN_PHPUNIT_VERSION=0.9.* PHPUNIT_VERSION=6.3.* before_install: - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; From 495fa7827bf3bd854115f4b244607c3c49096626 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 16:52:58 +0300 Subject: [PATCH 15/18] add hook for run PHPStan with PHPUnit --- tests/TestCase.php | 69 +++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 44e3911..cbf0e8b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,38 +8,49 @@ * @license http://opensource.org/licenses/MIT */ -namespace GpsLab\Bundle\PaginationBundle\Tests; +// hook for support PHPUnit 5.7 and newer versions +// NEXT_MAJOR: remove this hook #21 +namespace { + use PHPUnit\Framework\TestCase; -class TestCase extends \PHPUnit_Framework_TestCase -{ - /** - * @param string $class_name - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - protected function getMockNoConstructor($class_name) - { - return $this - ->getMockBuilder($class_name) - ->disableOriginalConstructor() - ->disableOriginalClone() - ->getMock() - ; + if (!class_exists('PHPUnit_Framework_TestCase') && class_exists('PHPUnit\Framework\TestCase')) { + class PHPUnit_Framework_TestCase extends TestCase + { + } } +} - /** - * @param string $class_name - * @param array $methods - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - protected function getMockAbstract($class_name, array $methods) +namespace GpsLab\Bundle\PaginationBundle\Tests +{ + class TestCase extends \PHPUnit_Framework_TestCase { - return $this - ->getMockBuilder($class_name) - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMockForAbstractClass() - ; + /** + * @param string $class_name + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getMockNoConstructor($class_name) + { + return $this + ->getMockBuilder($class_name) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->getMock(); + } + + /** + * @param string $class_name + * @param array $methods + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getMockAbstract($class_name, array $methods) + { + return $this + ->getMockBuilder($class_name) + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMockForAbstractClass(); + } } } From 37acca26b19c30d477b3265db442dd09a3878687 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 17:07:35 +0300 Subject: [PATCH 16/18] abs() return float if argument number is of type float, otherwise it is integer --- src/Service/NavigateRange.php | 12 ++++++------ tests/TestCase.php | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Service/NavigateRange.php b/src/Service/NavigateRange.php index 62b6cb2..89ceb24 100644 --- a/src/Service/NavigateRange.php +++ b/src/Service/NavigateRange.php @@ -81,9 +81,9 @@ private function definitionOffset() private function adjustmentLargeLeftOffset() { if ($this->config->getCurrentPage() - $this->left_offset < 1) { - $offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset); - $this->left_offset = $this->left_offset - $offset; - $this->right_offset = $this->right_offset + $offset; + $offset = abs($this->config->getCurrentPage() - 1 - $this->left_offset); + $this->left_offset -= $offset; + $this->right_offset += $offset; } } @@ -93,13 +93,13 @@ private function adjustmentLargeLeftOffset() private function adjustmentLargeRightOffset() { if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) { - $offset = (int) abs( + $offset = abs( $this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset ); - $this->left_offset = $this->left_offset + $offset; - $this->right_offset = $this->right_offset - $offset; + $this->left_offset += $offset; + $this->right_offset -= $offset; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cbf0e8b..a6f69ad 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,8 +8,8 @@ * @license http://opensource.org/licenses/MIT */ -// hook for support PHPUnit 5.7 and newer versions -// NEXT_MAJOR: remove this hook #21 +// hook for support PHPUnit 5.7 and newer versions #21 + namespace { use PHPUnit\Framework\TestCase; From 99b716e9b8304d4610dc1f1248b94487e20cd0d3 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 17:43:23 +0300 Subject: [PATCH 17/18] not test unit tests with PHPStan --- .travis.yml | 3 +-- phpstan.neon.dist | 1 - tests/TestCase.php | 67 +++++++++++++++++++--------------------------- 3 files changed, 28 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e2ac0e..4ea660a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -44,7 +44,7 @@ matrix: - php: 7.0 env: TWIG_VERSION=2.* - php: 7.1 - env: PHPSTAN_VERSION=0.9.* PHPSTAN_PHPUNIT_VERSION=0.9.* PHPUNIT_VERSION=6.3.* + env: PHPSTAN_VERSION=0.11.* before_install: - if [ -n "$GH_TOKEN" ]; then composer config github-oauth.github.com ${GH_TOKEN}; fi; @@ -55,7 +55,6 @@ before_script: - if [ "$TWIG_VERSION" != "" ]; then composer require "twig/twig:${TWIG_VERSION}" --dev --no-update; fi; - if [ "$PHPUNIT_VERSION" != "" ]; then composer require "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update; fi; - if [ "$PHPSTAN_VERSION" != "" ]; then composer require "phpstan/phpstan:${PHPSTAN_VERSION}" --dev --no-update; fi; - - if [ "$PHPSTAN_PHPUNIT_VERSION" != "" ]; then composer require "phpstan/phpstan-phpunit:${PHPSTAN_PHPUNIT_VERSION}" --dev --no-update; fi; - COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-interaction --no-scripts --no-progress script: diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 7c87780..1d5823a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,4 +2,3 @@ parameters: level: 7 paths: - src - - tests diff --git a/tests/TestCase.php b/tests/TestCase.php index a6f69ad..c48e8ea 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,49 +8,36 @@ * @license http://opensource.org/licenses/MIT */ -// hook for support PHPUnit 5.7 and newer versions #21 +namespace GpsLab\Bundle\PaginationBundle\Tests; -namespace { - use PHPUnit\Framework\TestCase; - - if (!class_exists('PHPUnit_Framework_TestCase') && class_exists('PHPUnit\Framework\TestCase')) { - class PHPUnit_Framework_TestCase extends TestCase - { - } - } -} - -namespace GpsLab\Bundle\PaginationBundle\Tests +class TestCase extends \PHPUnit_Framework_TestCase { - class TestCase extends \PHPUnit_Framework_TestCase + /** + * @param string $class_name + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getMockNoConstructor($class_name) { - /** - * @param string $class_name - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - protected function getMockNoConstructor($class_name) - { - return $this - ->getMockBuilder($class_name) - ->disableOriginalConstructor() - ->disableOriginalClone() - ->getMock(); - } + return $this + ->getMockBuilder($class_name) + ->disableOriginalConstructor() + ->disableOriginalClone() + ->getMock(); + } - /** - * @param string $class_name - * @param array $methods - * - * @return \PHPUnit_Framework_MockObject_MockObject - */ - protected function getMockAbstract($class_name, array $methods) - { - return $this - ->getMockBuilder($class_name) - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMockForAbstractClass(); - } + /** + * @param string $class_name + * @param array $methods + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getMockAbstract($class_name, array $methods) + { + return $this + ->getMockBuilder($class_name) + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMockForAbstractClass(); } } From 26172e470abe22cd83600a30566c849a01c314a2 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 30 Oct 2019 18:07:31 +0300 Subject: [PATCH 18/18] ignore not reproduced case in Configuration --- src/DependencyInjection/Configuration.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9ad3f24..a8e8aee 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -31,9 +31,15 @@ public function getConfigTreeBuilder() $root = $tree_builder->root('gpslab_pagination'); } + // @codeCoverageIgnoreStart if (!$root instanceof ArrayNodeDefinition) { - throw new \RuntimeException(sprintf('Config root node must be a "Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition", given "%s".', get_class($root))); + throw new \RuntimeException(sprintf( + 'Config root node must be a "%s", given "%s".', + 'Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition', + get_class($root) + )); } + // @codeCoverageIgnoreEnd $root->addDefaultsIfNotSet(); $root->children()->scalarNode('max_navigate')->defaultValue(5);