From 78235c185e7966e9a2cf69db825087039b07eb88 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 14 May 2019 15:59:52 +0200 Subject: [PATCH 01/11] fixed test templates --- .../TestApp/Template/Error/error500.php | 45 +++++++++++++++++++ .../Pages/{first_page.ctp => first_page.php} | 0 .../Template/Pages/{home.ctp => home.php} | 0 .../Template/Pages/{nohtml.ctp => nohtml.php} | 0 .../Pages/{nolinks.ctp => nolinks.php} | 0 .../{second_page.ctp => second_page.php} | 0 .../Pages/{third_page.ctp => third_page.php} | 0 .../default.ctp => layout/default.php} | 0 .../with_flash.ctp => layout/with_flash.php} | 0 9 files changed, 45 insertions(+) create mode 100644 tests/test_app/TestApp/Template/Error/error500.php rename tests/test_app/TestApp/Template/Pages/{first_page.ctp => first_page.php} (100%) rename tests/test_app/TestApp/Template/Pages/{home.ctp => home.php} (100%) rename tests/test_app/TestApp/Template/Pages/{nohtml.ctp => nohtml.php} (100%) rename tests/test_app/TestApp/Template/Pages/{nolinks.ctp => nolinks.php} (100%) rename tests/test_app/TestApp/Template/Pages/{second_page.ctp => second_page.php} (100%) rename tests/test_app/TestApp/Template/Pages/{third_page.ctp => third_page.php} (100%) rename tests/test_app/TestApp/Template/{Layout/default.ctp => layout/default.php} (100%) rename tests/test_app/TestApp/Template/{Layout/with_flash.ctp => layout/with_flash.php} (100%) diff --git a/tests/test_app/TestApp/Template/Error/error500.php b/tests/test_app/TestApp/Template/Error/error500.php new file mode 100644 index 0000000..826e686 --- /dev/null +++ b/tests/test_app/TestApp/Template/Error/error500.php @@ -0,0 +1,45 @@ +layout = 'error'; + +if (Configure::read('debug')) : + $this->layout = 'dev_error'; + + $this->assign('title', $message); + $this->assign('templateName', 'error500.php'); + + $this->start('file'); + ?> + queryString)) : ?> +

+ SQL Query: + queryString) ?> +

+ + params)) : ?> + SQL Query Params: + params) ?> + + + Error in: + getFile()), $error->getLine()) ?> + + element('auto_table_warning'); + + if (extension_loaded('xdebug')) : + xdebug_print_function_stack(); + endif; + + $this->end(); +endif; +?> +

+

+ : + +

\ No newline at end of file diff --git a/tests/test_app/TestApp/Template/Pages/first_page.ctp b/tests/test_app/TestApp/Template/Pages/first_page.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/first_page.ctp rename to tests/test_app/TestApp/Template/Pages/first_page.php diff --git a/tests/test_app/TestApp/Template/Pages/home.ctp b/tests/test_app/TestApp/Template/Pages/home.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/home.ctp rename to tests/test_app/TestApp/Template/Pages/home.php diff --git a/tests/test_app/TestApp/Template/Pages/nohtml.ctp b/tests/test_app/TestApp/Template/Pages/nohtml.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/nohtml.ctp rename to tests/test_app/TestApp/Template/Pages/nohtml.php diff --git a/tests/test_app/TestApp/Template/Pages/nolinks.ctp b/tests/test_app/TestApp/Template/Pages/nolinks.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/nolinks.ctp rename to tests/test_app/TestApp/Template/Pages/nolinks.php diff --git a/tests/test_app/TestApp/Template/Pages/second_page.ctp b/tests/test_app/TestApp/Template/Pages/second_page.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/second_page.ctp rename to tests/test_app/TestApp/Template/Pages/second_page.php diff --git a/tests/test_app/TestApp/Template/Pages/third_page.ctp b/tests/test_app/TestApp/Template/Pages/third_page.php similarity index 100% rename from tests/test_app/TestApp/Template/Pages/third_page.ctp rename to tests/test_app/TestApp/Template/Pages/third_page.php diff --git a/tests/test_app/TestApp/Template/Layout/default.ctp b/tests/test_app/TestApp/Template/layout/default.php similarity index 100% rename from tests/test_app/TestApp/Template/Layout/default.ctp rename to tests/test_app/TestApp/Template/layout/default.php diff --git a/tests/test_app/TestApp/Template/Layout/with_flash.ctp b/tests/test_app/TestApp/Template/layout/with_flash.php similarity index 100% rename from tests/test_app/TestApp/Template/Layout/with_flash.ctp rename to tests/test_app/TestApp/Template/layout/with_flash.php From 983469857778684b337a411ec4a78d6880fbc2b7 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 14 May 2019 16:24:04 +0200 Subject: [PATCH 02/11] updated for `cakephp` 4 and `phpunit` 8 --- .travis.yml | 7 ++--- CHANGELOG.md | 4 +++ composer.json | 11 ++++---- phpcs.xml.dist | 16 +++++++++++- phpunit.xml.dist | 1 - src/Command/LinkScannerCommand.php | 7 ++--- src/Event/LinkScannerCommandEventListener.php | 3 ++- .../LinkScannerEventListenerInterface.php | 1 + src/Plugin.php | 1 + src/ResultScan.php | 6 +++-- src/ScanEntity.php | 1 + src/TestSuite/IntegrationTestTrait.php | 3 ++- src/TestSuite/TestCase.php | 9 ++++--- src/Utility/LinkScanner.php | 7 ++--- .../Command/LinkScannerCommandTest.php | 5 ++-- tests/TestCase/ResultScanTest.php | 3 ++- tests/TestCase/ScanEntityTest.php | 5 ++-- tests/TestCase/Utility/LinkScannerTest.php | 26 +++++++++---------- tests/bootstrap.php | 3 +-- tests/test_app/TestApp/Application.php | 10 +++---- .../TestApp/Controller/AppController.php | 4 ++- .../TestApp/Controller/PagesController.php | 4 ++- tests/test_app/TestApp/config/routes.php | 2 +- version | 2 +- 24 files changed, 85 insertions(+), 56 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6321b94..f64f106 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,6 @@ language: php php: - - 5.6 - - 7.0 - - 7.1 - 7.2 - 7.3 @@ -11,10 +8,10 @@ matrix: fast_finish: true include: - - php: 7.0 + - php: 7.2 env: PHPCS=1 - - php: 7.0 + - php: 7.2 env: COVERAGE=1 install: diff --git a/CHANGELOG.md b/CHANGELOG.md index bcae63b..5bb1688 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # 1.x branch +## 1.1 branch +### 1.1.0 +* updated for `cakephp` 4 and `phpunit` 8. + ## 1.0 branch ### 1.0.5 * now it correctly handles the url with the trailing slash or the `www` prefix. diff --git a/composer.json b/composer.json index a3da5da..0c5efc9 100644 --- a/composer.json +++ b/composer.json @@ -9,14 +9,15 @@ "email": "mirko.pagliai@gmail.com" }], "require": { - "php": ">=5.5.9", - "cakephp/cakephp": "^3.7.1", - "mirko-pagliai/me-tools": "^2.18", + "php": ">=7.2", + "cakephp/cakephp": "4.x-dev", + "cakephp/chronos": "2.x-dev as 2.0.0", + "mirko-pagliai/me-tools": "dev-cakephp4", "mirko-pagliai/php-tools": "^1.2.3" }, "require-dev": { - "cakephp/cakephp-codesniffer": "^3.0", - "phpunit/phpunit": "^5.7|^6.0" + "cakephp/cakephp-codesniffer": "dev-next", + "phpunit/phpunit": "^8.0" }, "autoload": { "psr-4": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 6d4b1c5..70587b5 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -17,10 +17,24 @@ /webroot + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + *.min.css *.min.js - /config/ckeditor/build-config.js /config/Migrations /coverage /vendor diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4428ddc..3e890ad 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,6 @@ colors="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="./tests/bootstrap.php" > diff --git a/src/Command/LinkScannerCommand.php b/src/Command/LinkScannerCommand.php index 6ccd4b5..d22f2be 100644 --- a/src/Command/LinkScannerCommand.php +++ b/src/Command/LinkScannerCommand.php @@ -1,4 +1,5 @@ LinkScanner = $this->LinkScanner ?: new LinkScanner(); } @@ -48,7 +49,7 @@ public function initialize() * @see LinkScannerCommandEventListener::implementedEvents() * @uses $LinkScanner */ - public function execute(Arguments $args, ConsoleIo $io) + public function execute(Arguments $args, ConsoleIo $io): ?int { try { //Will trigger events provided by `LinkScannerCommandEventListener` @@ -95,7 +96,7 @@ public function execute(Arguments $args, ConsoleIo $io) * @return \Cake\Console\ConsoleOptionParser The built parser * @uses $LinkScanner */ - protected function buildOptionParser(ConsoleOptionParser $parser) + protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { $parser->setDescription(__d('link-scanner', 'Performs a complete scan')); diff --git a/src/Event/LinkScannerCommandEventListener.php b/src/Event/LinkScannerCommandEventListener.php index 9a86c64..1ac9522 100644 --- a/src/Event/LinkScannerCommandEventListener.php +++ b/src/Event/LinkScannerCommandEventListener.php @@ -1,4 +1,5 @@ buffered()->toArray(), $this->parseItems($items))); } @@ -70,7 +72,7 @@ public function append($items) * @return \Cake\Collection\CollectionInterface * @uses parseItems() */ - public function prepend($items) + public function prepend($items): CollectionInterface { return new ResultScan(array_merge($this->parseItems($items), $this->buffered()->toArray())); } diff --git a/src/ScanEntity.php b/src/ScanEntity.php index 99b9a43..2f71e2b 100644 --- a/src/ScanEntity.php +++ b/src/ScanEntity.php @@ -1,4 +1,5 @@ method('get')->will($this->returnCallback(function ($url) { if (is_string($url) && preg_match('/^http:\/\/localhost\/?(pages\/(.+))?$/', $url, $matches)) { - $url = ['controller' => 'Pages', 'action' => 'display', empty($matches[2]) ? 'home' : $matches[2]]; + $url = ['controller' => 'Pages', 'action' => 'display', $matches[2] ?? 'home']; } call_user_func([$this, 'get'], $url); diff --git a/src/TestSuite/TestCase.php b/src/TestSuite/TestCase.php index 97ce6c6..a8e015a 100644 --- a/src/TestSuite/TestCase.php +++ b/src/TestSuite/TestCase.php @@ -1,4 +1,5 @@ method('get')->will($this->returnValue($this->getResponseWithBody(null)->withStatus(404))); + $Client->method('get')->will($this->returnValue($this->getResponseWithBody('')->withStatus(404))); return $Client; } @@ -108,7 +109,7 @@ protected function getClientReturnsSampleResponse() * @param \LinkScanner\Utility\LinkScanner|null $LinkScanner `LinkScanner` instance or `null` * @return \Cake\Event\EventManager */ - protected function getEventManager(LinkScanner $LinkScanner = null) + protected function getEventManager(?LinkScanner $LinkScanner = null) { $LinkScanner = $LinkScanner ?: $this->LinkScanner; $eventManager = $LinkScanner->getEventManager(); @@ -129,7 +130,7 @@ protected function getEventManager(LinkScanner $LinkScanner = null) * `null` to create a new instance * @return \Cake\Http\Client\Response */ - protected function getResponseWithBody($body, Response $response = null) + protected function getResponseWithBody(string $body, ?Response $response = null) { $response = $response ?: new Response(); $stream = new Stream('php://memory', 'wb+'); diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index e9fb370..98a6e52 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -1,4 +1,5 @@ alreadyScanned[] = $url; $cacheKey = sprintf('response_%s', md5(serialize($url))); @@ -168,7 +169,7 @@ protected function _getResponse($url) $response = $this->getConfig('cache') ? Cache::read($cacheKey, 'LinkScanner') : null; if ($response && is_array($response)) { - list($response, $body) = $response; + [$response, $body] = $response; $stream = new Stream('php://memory', 'wb+'); $stream->write($body); @@ -464,7 +465,7 @@ public function scan() $this->_createLockFile(); $this->startTime = time(); - $maxNestingLevel = ini_set('xdebug.max_nesting_level', -1); + $maxNestingLevel = ini_set('xdebug.max_nesting_level', '-1'); try { $this->dispatchEvent('LinkScanner.scanStarted', [$this->startTime, $fullBaseUrl]); diff --git a/tests/TestCase/Command/LinkScannerCommandTest.php b/tests/TestCase/Command/LinkScannerCommandTest.php index d6de300..ccda19b 100644 --- a/tests/TestCase/Command/LinkScannerCommandTest.php +++ b/tests/TestCase/Command/LinkScannerCommandTest.php @@ -1,4 +1,5 @@ assertOutputRegExp('/Timeout in seconds for GET requests: \d+/'); //Moves to final lines - $messages = $this->_out->messages(); + $messages = array_values(array_filter($this->_out->messages())); $count = count($messages); while (key($messages) !== $count - 5) { next($messages); diff --git a/tests/TestCase/ResultScanTest.php b/tests/TestCase/ResultScanTest.php index 3ac0ec0..1016b56 100644 --- a/tests/TestCase/ResultScanTest.php +++ b/tests/TestCase/ResultScanTest.php @@ -1,4 +1,5 @@ $expectedValue) { $this->ScanEntity->set('code', $code); - $this->assertEquals($expectedValue, $this->ScanEntity->isOk()); + $this->assertEquals($expectedValue, $this->ScanEntity->isSuccess()); } $statusCodes = [ 200 => false, diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index d17fcf2..46e66e1 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -1,4 +1,5 @@ LinkScanner->setConfig('cache', true); foreach ([ - 'nolinks' => 200, - 'home' => 200, - 'noexisting' => 500, - ] as $pageName => $expectedStatusCode) { - $params = ['controller' => 'Pages', 'action' => 'display', $pageName]; - - $response = $getResponseMethod($params); + 'http://localhost/pages/nolinks' => 200, + 'http://localhost/pages/home' => 200, + 'http://localhost/pages/noexisting' => 500, + ] as $url => $expectedStatusCode) { + $response = $getResponseMethod($url); $this->assertEquals($expectedStatusCode, $response->getStatusCode()); $this->assertStringStartsWith('text/html', $response->getHeaderLine('content-type')); - $responseFromCache = $getResponseFromCache($params); + $responseFromCache = $getResponseFromCache($url); if ($response->isOk()) { $this->assertNotEmpty($responseFromCache); $this->assertInstanceof(Response::class, $responseFromCache); @@ -373,8 +372,7 @@ public function testScanFromTests() 'Found link: http://localhost/pages/sameredirect', 'Scanning http://localhost/pages/sameredirect', ]; - $params = ['controller' => 'Pages', 'action' => 'display', 'home']; - $LinkScanner = $this->getLinkScannerClientReturnsFromTests($params); + $LinkScanner = $this->getLinkScannerClientReturnsFromTests(); $LinkScanner->scan(); $this->assertEquals($expectedDebug, $this->debug); @@ -421,7 +419,7 @@ public function testScanFromTests() 'Found link: http://localhost/pages/sameredirect', 'Scanning http://localhost/pages/sameredirect', ]; - $LinkScanner = $this->getLinkScannerClientReturnsFromTests($params); + $LinkScanner = $this->getLinkScannerClientReturnsFromTests(); $LinkScanner->setConfig('followRedirects', true)->scan(); $this->assertEquals($expectedDebug, $this->debug); @@ -433,7 +431,7 @@ public function testScanFromTests() $this->assertEquals($expectedInternaLinks, $internalLinks->toList()); $this->assertEquals($expectedExternalLinks, $externalLinks->toList()); - $LinkScanner = $this->getLinkScannerClientReturnsFromTests($params); + $LinkScanner = $this->getLinkScannerClientReturnsFromTests(); $LinkScanner->setConfig('maxDepth', 1)->scan(); $this->assertCount(1, $LinkScanner->ResultScan); $item = $LinkScanner->ResultScan->first(); @@ -450,7 +448,7 @@ public function testScanFromTests() */ public function testScanNoOtherLinks() { - $LinkScanner = $this->getLinkScannerClientReturnsFromTests(['controller' => 'Pages', 'action' => 'display', 'nolinks']); + $LinkScanner = $this->getLinkScannerClientReturnsFromTests('http://localhost/pages/nolinks'); $this->assertEventNotFired('LinkScanner.foundLinkToBeScanned', $this->getEventManager($LinkScanner->scan())); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d2e42e4..22b9a22 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,5 @@ addPlugin(LinkScanner::class); } @@ -37,10 +39,8 @@ public function bootstrap() * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to set in your App Class * @return \Cake\Http\MiddlewareQueue */ - public function middleware($middlewareQueue) + public function middleware($middlewareQueue): MiddlewareQueue { - $middlewareQueue->add(new RoutingMiddleware($this)); - - return $middlewareQueue; + return $middlewareQueue->add(new RoutingMiddleware($this)); } } diff --git a/tests/test_app/TestApp/Controller/AppController.php b/tests/test_app/TestApp/Controller/AppController.php index 2f38a4d..34dc990 100644 --- a/tests/test_app/TestApp/Controller/AppController.php +++ b/tests/test_app/TestApp/Controller/AppController.php @@ -1,11 +1,13 @@ viewBuilder()->setLayout(false); + $this->viewBuilder()->disableAutoLayout(); } $count = count($path); diff --git a/tests/test_app/TestApp/config/routes.php b/tests/test_app/TestApp/config/routes.php index b4770f3..f7a027f 100644 --- a/tests/test_app/TestApp/config/routes.php +++ b/tests/test_app/TestApp/config/routes.php @@ -1,8 +1,8 @@ Date: Tue, 14 May 2019 16:41:13 +0200 Subject: [PATCH 03/11] updated for `cakephp` 4 and `phpunit` 8 --- src/Event/LinkScannerCommandEventListener.php | 21 ++++++++-------- .../LinkScannerEventListenerInterface.php | 16 ++++++------ src/ResultScan.php | 12 ++++----- src/ScanEntity.php | 2 +- src/TestSuite/IntegrationTestTrait.php | 4 +-- src/TestSuite/TestCase.php | 15 +++++------ src/Utility/LinkScanner.php | 25 +++++++++---------- 7 files changed, 48 insertions(+), 47 deletions(-) diff --git a/src/Event/LinkScannerCommandEventListener.php b/src/Event/LinkScannerCommandEventListener.php index 1ac9522..86f8970 100644 --- a/src/Event/LinkScannerCommandEventListener.php +++ b/src/Event/LinkScannerCommandEventListener.php @@ -70,7 +70,7 @@ public function implementedEvents(): array 'scanStarted', ]; - return array_combine(array_map(function ($event) { + return array_combine(array_map(function (string $event) { return 'LinkScanner.' . $event; }, $events), $events); } @@ -83,7 +83,7 @@ public function implementedEvents(): array * @uses $args * @uses $io */ - public function afterScanUrl(Event $event, Response $response) + public function afterScanUrl(Event $event, Response $response): bool { if (!$this->args->getOption('verbose')) { return true; @@ -103,7 +103,7 @@ public function afterScanUrl(Event $event, Response $response) * @return bool * @uses $io */ - public function beforeScanUrl(Event $event, $url) + public function beforeScanUrl(Event $event, string $url): bool { $this->io->verbose(__d('link-scanner', 'Checking {0} ...', $url), 0); @@ -117,7 +117,7 @@ public function beforeScanUrl(Event $event, $url) * @return bool * @uses $io */ - public function foundLinkToBeScanned(Event $event, $link) + public function foundLinkToBeScanned(Event $event, string $link): bool { $this->io->verbose(__d('link-scanner', 'Link found: {0}', $link)); @@ -131,7 +131,7 @@ public function foundLinkToBeScanned(Event $event, $link) * @return bool * @uses $io */ - public function foundRedirect(Event $event, $url) + public function foundRedirect(Event $event, string $url): bool { $this->io->verbose(__d('link-scanner', 'Redirect found: {0}', $url)); @@ -145,7 +145,7 @@ public function foundRedirect(Event $event, $url) * @return bool * @uses $io */ - public function resultsExported(Event $event, $filename) + public function resultsExported(Event $event, string $filename): bool { $this->io->success(__d('link-scanner', 'Results have been exported to {0}', $filename)); @@ -156,10 +156,11 @@ public function resultsExported(Event $event, $filename) * `LinkScanner.resultsImported` event * @param \Cake\Event\Event $event An `Event` instance * @param string $filename Filename - * @return void + * @return bool */ - public function resultsImported(Event $event, $filename) + public function resultsImported(Event $event, string $filename): bool { + return true; } /** @@ -172,7 +173,7 @@ public function resultsImported(Event $event, $filename) * @uses $args * @uses $io */ - public function scanCompleted(Event $event, $startTime, $endTime, ResultScan $ResultScan) + public function scanCompleted(Event $event, int $startTime, int $endTime, ResultScan $ResultScan): bool { if ($this->args->getOption('verbose')) { $this->io->hr(); @@ -201,7 +202,7 @@ public function scanCompleted(Event $event, $startTime, $endTime, ResultScan $Re * @uses $args * @uses $io */ - public function scanStarted(Event $event, $startTime, $fullBaseUrl) + public function scanStarted(Event $event, int $startTime, string $fullBaseUrl): bool { if ($this->args->getOption('verbose')) { $this->io->hr(); diff --git a/src/Event/LinkScannerEventListenerInterface.php b/src/Event/LinkScannerEventListenerInterface.php index d66e3cf..7bcc031 100644 --- a/src/Event/LinkScannerEventListenerInterface.php +++ b/src/Event/LinkScannerEventListenerInterface.php @@ -29,7 +29,7 @@ interface LinkScannerEventListenerInterface extends EventListenerInterface * @param \Cake\Http\Client\Response $response A `Response` instance * @return bool */ - public function afterScanUrl(Event $event, Response $response); + public function afterScanUrl(Event $event, Response $response): bool; /** * `LinkScanner.beforeScanUrl` event @@ -37,7 +37,7 @@ public function afterScanUrl(Event $event, Response $response); * @param string $url Url * @return bool */ - public function beforeScanUrl(Event $event, $url); + public function beforeScanUrl(Event $event, string $url): bool; /** * `LinkScanner.foundLinkToBeScanned` event @@ -45,7 +45,7 @@ public function beforeScanUrl(Event $event, $url); * @param string $link Link * @return bool */ - public function foundLinkToBeScanned(Event $event, $link); + public function foundLinkToBeScanned(Event $event, string $link): bool; /** * `LinkScanner.foundRedirect` event @@ -53,7 +53,7 @@ public function foundLinkToBeScanned(Event $event, $link); * @param string $redirect Redirect * @return bool */ - public function foundRedirect(Event $event, $redirect); + public function foundRedirect(Event $event, string $redirect): bool; /** * `LinkScanner.resultsExported` event @@ -61,7 +61,7 @@ public function foundRedirect(Event $event, $redirect); * @param string $filename Filename * @return bool */ - public function resultsExported(Event $event, $filename); + public function resultsExported(Event $event, string $filename): bool; /** * `LinkScanner.resultsImported` event @@ -69,7 +69,7 @@ public function resultsExported(Event $event, $filename); * @param string $filename Filename * @return bool */ - public function resultsImported(Event $event, $filename); + public function resultsImported(Event $event, string $filename): bool; /** * `LinkScanner.scanCompleted` event @@ -79,7 +79,7 @@ public function resultsImported(Event $event, $filename); * @param \LinkScanner\ResultScan $ResultScan A `ResultScan` instance * @return bool */ - public function scanCompleted(Event $event, $startTime, $endTime, ResultScan $ResultScan); + public function scanCompleted(Event $event, int $startTime, int $endTime, ResultScan $ResultScan): bool; /** * `LinkScanner.scanStarted` event @@ -88,5 +88,5 @@ public function scanCompleted(Event $event, $startTime, $endTime, ResultScan $Re * @param string $fullBaseUrl Full base url * @return bool */ - public function scanStarted(Event $event, $startTime, $fullBaseUrl); + public function scanStarted(Event $event, int $startTime, string $fullBaseUrl): bool; } diff --git a/src/ResultScan.php b/src/ResultScan.php index 45da93d..7bb63e4 100644 --- a/src/ResultScan.php +++ b/src/ResultScan.php @@ -27,10 +27,10 @@ class ResultScan extends Collection * Internal method to parse items. * * Ensures that each item is a `ScanEntity` and has all the properties it needs - * @param array|\Traversable $items Array of items + * @param iterable $items Items * @return array Parsed items */ - protected function parseItems($items) + protected function parseItems(iterable $items): array { $items = $items instanceof Traversable ? $items->toArray() : $items; @@ -41,10 +41,10 @@ protected function parseItems($items) /** * Constructor - * @param array|\Traversable $items Items + * @param iterable $items Items * @uses parseItems() */ - public function __construct($items = []) + public function __construct(iterable $items = []) { parent::__construct($this->parseItems($items)); } @@ -54,7 +54,7 @@ public function __construct($items = []) * * Returns a new `ResultScan` instance as the result of concatenating the * list of elements in this collection with the passed list of elements - * @param array|\Traversable $items Items + * @param iterable $items Items * @return \Cake\Collection\CollectionInterface * @uses parseItems() */ @@ -68,7 +68,7 @@ public function append($items): CollectionInterface * * Returns a new `ResultScan` instance as the result of concatenating the * passed list of elements with the list of elements in this collection - * @param array|\Traversable $items Items + * @param iterable $items Items * @return \Cake\Collection\CollectionInterface * @uses parseItems() */ diff --git a/src/ScanEntity.php b/src/ScanEntity.php index 2f71e2b..1a7da83 100644 --- a/src/ScanEntity.php +++ b/src/ScanEntity.php @@ -46,7 +46,7 @@ public function __construct(array $properties = []) * @return mixed * @see \Cake\Http\Client\Response */ - public function __call($name, $arguments) + public function __call(string $name, $arguments) { if (method_exists(Response::class, $name)) { $response = (new Response()) diff --git a/src/TestSuite/IntegrationTestTrait.php b/src/TestSuite/IntegrationTestTrait.php index fc0197d..8ae8d0a 100644 --- a/src/TestSuite/IntegrationTestTrait.php +++ b/src/TestSuite/IntegrationTestTrait.php @@ -33,7 +33,7 @@ trait IntegrationTestTrait * the test app * @return \Cake\Http\Client|\PHPUnit_Framework_MockObject_MockObject */ - protected function getClientReturnsFromTests() + protected function getClientReturnsFromTests(): object { $Client = $this->getMockBuilder(Client::class) ->setMethods(['get']) @@ -74,7 +74,7 @@ protected function getClientReturnsFromTests() * @return \LinkScanner\Utility\LinkScanner|\PHPUnit_Framework_MockObject_MockObject * @uses getClientReturnsFromTests() */ - protected function getLinkScannerClientReturnsFromTests($fullBaseUrl = null) + protected function getLinkScannerClientReturnsFromTests($fullBaseUrl = null): object { $fullBaseUrl = $fullBaseUrl ?: Configure::read('App.fullBaseUrl', 'http://localhost'); $fullBaseUrl = is_string($fullBaseUrl) ? $fullBaseUrl : Router::url($fullBaseUrl, true); diff --git a/src/TestSuite/TestCase.php b/src/TestSuite/TestCase.php index a8e015a..1407887 100644 --- a/src/TestSuite/TestCase.php +++ b/src/TestSuite/TestCase.php @@ -15,6 +15,7 @@ use Cake\Cache\Cache; use Cake\Event\EventList; +use Cake\Event\EventManager; use Cake\Http\Client; use Cake\Http\Client\Response; use LinkScanner\Utility\LinkScanner; @@ -45,7 +46,7 @@ public function tearDown(): void * defaults to global event manager * @return void */ - public function assertEventNotFired($name, $eventManager = null) + public function assertEventNotFired(string $name, ?EventManager $eventManager = null): void { $eventManager = $eventManager ?: EventManager::instance(); $eventHasFired = $eventManager->getEventList()->hasEvent($name); @@ -56,10 +57,10 @@ public function assertEventNotFired($name, $eventManager = null) /** * Returns a stub of `Client`, where the `get()` method always returns a * response with error (404 status code) - * @return \Cake\Http\Client + * @return \Cake\Http\Client|\PHPUnit_Framework_MockObject_MockObject * @uses getResponseWithBody() */ - protected function getClientReturnsErrorResponse() + protected function getClientReturnsErrorResponse(): object { $Client = $this->getMockBuilder(Client::class) ->setMethods(['get']) @@ -75,10 +76,10 @@ protected function getClientReturnsErrorResponse() /** * Returns a stub of `Client`, where the `get()` method returns a sample * response which is read from `examples/responses` files - * @return \Cake\Http\Client + * @return \Cake\Http\Client|\PHPUnit_Framework_MockObject_MockObject * @uses getResponseWithBody() */ - protected function getClientReturnsSampleResponse() + protected function getClientReturnsSampleResponse(): object { $Client = $this->getMockBuilder(Client::class) ->setMethods(['get']) @@ -109,7 +110,7 @@ protected function getClientReturnsSampleResponse() * @param \LinkScanner\Utility\LinkScanner|null $LinkScanner `LinkScanner` instance or `null` * @return \Cake\Event\EventManager */ - protected function getEventManager(?LinkScanner $LinkScanner = null) + protected function getEventManager(?LinkScanner $LinkScanner = null): EventManager { $LinkScanner = $LinkScanner ?: $this->LinkScanner; $eventManager = $LinkScanner->getEventManager(); @@ -130,7 +131,7 @@ protected function getEventManager(?LinkScanner $LinkScanner = null) * `null` to create a new instance * @return \Cake\Http\Client\Response */ - protected function getResponseWithBody(string $body, ?Response $response = null) + protected function getResponseWithBody(string $body, ?Response $response = null): Response { $response = $response ?: new Response(); $stream = new Stream('php://memory', 'wb+'); diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 134e3c0..1ee5d8f 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -25,7 +25,6 @@ use Cake\Http\Client\Response; use Exception; use InvalidArgumentException; -use LinkScanner\Http\Client\ScanResponse; use LinkScanner\ResultScan; use LinkScanner\ScanEntity; use RuntimeException; @@ -111,7 +110,7 @@ class LinkScanner implements Serializable * @uses $Client * @uses $ResultScan */ - public function __construct($Client = null, $ResultScan = null) + public function __construct($Client = null, ?ResultScan $ResultScan = null) { $this->Client = $Client ?: new Client(['redirect' => true]); $this->ResultScan = $ResultScan ?: new ResultScan(); @@ -129,7 +128,7 @@ public function __construct($Client = null, $ResultScan = null) * @param string $name Property name * @return mixed */ - public function __get($name) + public function __get(string $name) { return $this->$name; } @@ -140,7 +139,7 @@ public function __get($name) * @throws \RuntimeException * @uses $lockFile */ - protected function _createLockFile() + protected function _createLockFile(): bool { is_true_or_fail(!$this->getConfig('lockFile') || !file_exists($this->lockFile), __d( 'link-scanner', @@ -213,7 +212,7 @@ protected function _getResponse(string $url): Response * @uses $currentDepth * @uses $hostname */ - protected function _recursiveScan($url, $referer = null) + protected function _recursiveScan(string $url, ?string $referer = null): void { $response = $this->_singleScan($url, $referer); @@ -258,13 +257,13 @@ protected function _recursiveScan($url, $referer = null) * - `LinkScanner.foundRedirect`: will be triggered if a redirect is found. * @param string $url Url to scan * @param string|null $referer Referer of this url - * @return \LinkScanner\Http\Client\ScanResponse|null + * @return \Cake\Http\Client\Response|null * @uses _getResponse() * @uses canBeScanned() * @uses $ResultScan * @uses $hostname */ - protected function _singleScan($url, $referer = null) + protected function _singleScan(string $url, ?string $referer = null): ?Response { $url = clean_url($url, true, true); if (!$this->canBeScanned($url)) { @@ -309,7 +308,7 @@ protected function _singleScan($url, $referer = null) * @uses $alreadyScanned * @uses $hostname */ - protected function canBeScanned($url) + protected function canBeScanned(string $url): bool { if (!is_url($url) || in_array($url, $this->alreadyScanned) || (!$this->getConfig('externalLinks') && is_external_url($url, $this->hostname))) { @@ -330,7 +329,7 @@ protected function canBeScanned($url) * @return string * @uses $Client */ - public function serialize() + public function serialize(): string { //Unsets the event class and event manager. For the `Client` instance, // it takes only configuration and cookies @@ -347,7 +346,7 @@ public function serialize() * @return void * @uses $Client */ - public function unserialize($serialized) + public function unserialize($serialized): void { //Resets the event list and the Client instance $properties = unserialize($serialized); @@ -377,7 +376,7 @@ public function unserialize($serialized) * @uses $hostname * @uses $startTime */ - public function export($filename = null) + public function export(?string $filename = null): string { is_true_or_fail( !$this->ResultScan->isEmpty(), @@ -404,11 +403,11 @@ public function export($filename = null) * - `LinkScanner.resultsImported`: will be triggered when the results have * been exported. * @param string $filename Filename from which to import - * @return \LinkScanner\Utility\LinkScanner + * @return $this * @see unserialize() * @throws \RuntimeException */ - public static function import($filename) + public static function import(string $filename) { try { if (!Folder::isAbsolute($filename)) { From cef67d7db4140233dcd86fe3a16ff15ad6cb3af0 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 14 May 2019 16:48:03 +0200 Subject: [PATCH 04/11] fixed --- src/Utility/LinkScanner.php | 2 +- tests/TestCase/Utility/LinkScannerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utility/LinkScanner.php b/src/Utility/LinkScanner.php index 1ee5d8f..36e1316 100644 --- a/src/Utility/LinkScanner.php +++ b/src/Utility/LinkScanner.php @@ -472,7 +472,7 @@ public function scan() $this->endTime = time(); $this->dispatchEvent('LinkScanner.scanCompleted', [$this->startTime, $this->endTime, $this->ResultScan]); } finally { - ini_set('xdebug.max_nesting_level', $maxNestingLevel); + ini_set('xdebug.max_nesting_level', (string)$maxNestingLevel); @unlink($this->lockFile); } diff --git a/tests/TestCase/Utility/LinkScannerTest.php b/tests/TestCase/Utility/LinkScannerTest.php index 610c786..46e66e1 100644 --- a/tests/TestCase/Utility/LinkScannerTest.php +++ b/tests/TestCase/Utility/LinkScannerTest.php @@ -103,7 +103,7 @@ public function testGetResponse() $response = Cache::read(sprintf('response_%s', md5(serialize($url))), 'LinkScanner'); if ($response && is_array($response)) { - list($response, $body) = $response; + [$response, $body] = $response; $stream = new Stream('php://memory', 'wb+'); $stream->write($body); From 9014ae25844528118b9f3d8e862add53b3597c98 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 15 May 2019 16:58:55 +0200 Subject: [PATCH 05/11] updated --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0c5efc9..600bb9f 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "cakephp/cakephp": "4.x-dev", "cakephp/chronos": "2.x-dev as 2.0.0", "mirko-pagliai/me-tools": "dev-cakephp4", - "mirko-pagliai/php-tools": "^1.2.3" + "mirko-pagliai/php-tools": "dev-php7" }, "require-dev": { "cakephp/cakephp-codesniffer": "dev-next", From e96412964c6e6806c67f62c2b47a84934a78ba28 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 17 Jul 2019 14:06:19 +0200 Subject: [PATCH 06/11] fixed --- tests/TestCase/Command/LinkScannerCommandTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Command/LinkScannerCommandTest.php b/tests/TestCase/Command/LinkScannerCommandTest.php index ccda19b..d9f477e 100644 --- a/tests/TestCase/Command/LinkScannerCommandTest.php +++ b/tests/TestCase/Command/LinkScannerCommandTest.php @@ -29,8 +29,9 @@ */ class LinkScannerCommandTest extends TestCase { - use ConsoleIntegrationTestTrait; - use IntegrationTestTrait; + use ConsoleIntegrationTestTrait, IntegrationTestTrait { + ConsoleIntegrationTestTrait::configApplication insteadof IntegrationTestTrait; + } /** * @var \LinkScanner\Utility\LinkScanner @@ -112,6 +113,7 @@ public function testScan() $this->Command->run(['--verbose'], $this->io); $this->assertErrorContains('404'); } + /** * Test for `scan()` method, with cache enabled * @test From 2e1412a9defd2653800191c6616445f0d69c6d19 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Thu, 18 Jul 2019 15:20:43 +0200 Subject: [PATCH 07/11] fixed --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0a858a..f64f106 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,13 +14,9 @@ matrix: - php: 7.2 env: COVERAGE=1 - - php: 7.0 - env: CAKEPHP_VERSION="3.7.*" - install: - composer self-update - composer install --prefer-dist --no-interaction - - if [[ ! -z "$CAKEPHP_VERSION" ]]; then composer require --update-with-dependencies cakephp/cakephp:${CAKEPHP_VERSION}; fi script: - if [[ $PHPCS != '1' && $COVERAGE != '1' ]]; then vendor/bin/phpunit; fi From 237f61e0ee1def124b13fdd006d4aa0b23007da8 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 10 Sep 2019 17:26:42 +0200 Subject: [PATCH 08/11] fixed for sniffer --- src/Event/LinkScannerCommandEventListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/LinkScannerCommandEventListener.php b/src/Event/LinkScannerCommandEventListener.php index 97316b9..9837e22 100644 --- a/src/Event/LinkScannerCommandEventListener.php +++ b/src/Event/LinkScannerCommandEventListener.php @@ -218,7 +218,7 @@ public function scanStarted(Event $event, int $startTime, string $fullBaseUrl): $this->io->hr(); $cache = Cache::getConfig('LinkScanner'); - list($method, $message) = ['info', __d('link-scanner', 'The cache is disabled')]; + [$method, $message] = ['info', __d('link-scanner', 'The cache is disabled')]; if (!$this->args->getOption('no-cache') && Cache::enabled() && !empty($cache['duration'])) { $method = 'success'; $message = __d('link-scanner', 'The cache is enabled and its duration is `{0}`', $cache['duration']); From a87bf1f5efba10a9b2461843781c64be82a97519 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Fri, 6 Dec 2019 17:24:06 +0100 Subject: [PATCH 09/11] fixed --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 600bb9f..317ab28 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,12 @@ "type": "cakephp-plugin", "license": "MIT", "authors": [{ - "name": "Mirko Pagliai", - "email": "mirko.pagliai@gmail.com" - }], + "name": "Mirko Pagliai", + "email": "mirko.pagliai@gmail.com" + }], "require": { "php": ">=7.2", "cakephp/cakephp": "4.x-dev", - "cakephp/chronos": "2.x-dev as 2.0.0", "mirko-pagliai/me-tools": "dev-cakephp4", "mirko-pagliai/php-tools": "dev-php7" }, From 897b70ee0632d9434664990f1b9c55264978746a Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Thu, 2 Jan 2020 13:36:34 +0100 Subject: [PATCH 10/11] updated --- composer.json | 8 ++++---- phpcs.xml.dist | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 317ab28..5f7580b 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,12 @@ }], "require": { "php": ">=7.2", - "cakephp/cakephp": "4.x-dev", - "mirko-pagliai/me-tools": "dev-cakephp4", - "mirko-pagliai/php-tools": "dev-php7" + "cakephp/cakephp": "^4.0", + "mirko-pagliai/me-tools": "^2.19", + "mirko-pagliai/php-tools": "^1.3" }, "require-dev": { - "cakephp/cakephp-codesniffer": "dev-next", + "cakephp/cakephp-codesniffer": "^4.0", "phpunit/phpunit": "^8.0" }, "autoload": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index ea95e91..2bb84c4 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -8,4 +8,7 @@ /tests/test_app/TestApp/Template/Error/error500.php + + 0 + From f746a81c4f59dab608a0ee8903d2a6ad6969f0fc Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Thu, 2 Jan 2020 13:59:12 +0100 Subject: [PATCH 11/11] updated --- README.md | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 67d8212..d212238 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Build Status](https://api.travis-ci.org/mirko-pagliai/cakephp-link-scanner.svg?branch=master)](https://travis-ci.org/mirko-pagliai/cakephp-link-scanner) [![Build status](https://ci.appveyor.com/api/projects/status/hqk7fxtad6r75wk3?svg=true)](https://ci.appveyor.com/project/mirko-pagliai/cakephp-link-scanner) [![codecov](https://codecov.io/gh/mirko-pagliai/cakephp-link-scanner/branch/master/graph/badge.svg)](https://codecov.io/gh/mirko-pagliai/cakephp-link-scanner) -[![CodeFactor](https://www.codefactor.io/repository/github/mirko-pagliai/cakephp-link-scanner/badge/cakephp4)](https://www.codefactor.io/repository/github/mirko-pagliai/cakephp-link-scanner/overview/cakephp4) +[![CodeFactor](https://www.codefactor.io/repository/github/mirko-pagliai/cakephp-link-scanner/badge)](https://www.codefactor.io/repository/github/mirko-pagliai/cakephp-link-scanner) *LinkScanner* is a CakePHP plugin for recursively scanning links: starting from a full base url, it performs GET requests, checks the status codes, inspects the @@ -13,7 +13,7 @@ response bodies and, if it finds other links, it continues recursively scanning. ![gif of terminal](https://github.com/mirko-pagliai/cakephp-link-scanner/raw/master/docs/tty.gif) Did you like this plugin? Its development requires a lot of time for me. -Please consider the possibility of making [a donation](//paypal.me/mirkopagliai): +Please consider the possibility of making [a donation](//paypal.me/mirkopagliai): even a coffee is enough! Thank you. [![Make a donation](https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_carte.jpg)](//paypal.me/mirkopagliai) @@ -24,10 +24,19 @@ You can install the plugin via composer: $ composer require --prefer-dist mirko-pagliai/cakephp-link-scanner ``` -**NOTE: the latest version available requires at least CakePHP 3.7.1**. - +**NOTE: the latest version available requires at least CakePHP 4**. + +Instead, the [cakephp3](//github.com/mirko-pagliai/cakephp-link-scanner/tree/cakephp3) +branch is compatible with all previous versions of CakePHP from version 3.7.1. +This branch coincides with the current version of *cakephp-link-scanner*. + +In this case, you can install the package as well: + +```bash +$ composer require --prefer-dist mirko-pagliai/cakephp-link-scanner:dev-cakephp3 + Then you have to load the plugin. For more information on how to load the plugin, -please refer to the [Cookbook](//book.cakephp.org/3.0/en/plugins.html#loading-a-plugin). +please refer to the [Cookbook](//book.cakephp.org/4.0/en/plugins.html#loading-a-plugin). Simply, you can execute the shell command to enable the plugin: ```bash @@ -37,7 +46,7 @@ This would update your application's bootstrap method. ## Configuration It's not essential, but it may be useful to set the `App.fullBaseUrl` value -correctly [refer to the Cookbook](//book.cakephp.org/3.0/en/development/configuration.html#general-configuration), +correctly [refer to the Cookbook](//book.cakephp.org/4.0/en/development/configuration.html#general-configuration), especially if you plan to use the plugin mainly on your app, so as not to have to indicate the full base url which to start the scan every time. @@ -54,6 +63,6 @@ In addition, you can refer to our [API](//mirko-pagliai.github.io/cakephp-link-s * allow to export results as html and/or xml. ## Versioning -For transparency and insight into our release cycle and to maintain backward -compatibility, *Assets* will be maintained under the +For transparency and insight into our release cycle and to maintain backward +compatibility, *Assets* will be maintained under the [Semantic Versioning guidelines](//semver.org).