Skip to content

Commit

Permalink
qa: modernize module
Browse files Browse the repository at this point in the history
- Use PSR-4 structure
- Use zend-coding-standard
- Use more modern PHPUnit versions
  - Update mocking usage
  - Use namespaced PHPUnit classes
- Update dependencies to latest stable
  - Update EventManager usage to make it compatible with both v2 and v3
  • Loading branch information
weierophinney committed Feb 3, 2020
1 parent 32c1334 commit 64ffab3
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 157 deletions.
41 changes: 25 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,44 @@
{
"name": "Matthew Weier O'Phinney",
"email": "[email protected]",
"homepage": "http://mwop.net/"
"homepage": "https://mwop.net/"
}
],
"autoload": {
"psr-0": {
"psr-4": {
"PhlySimplePage\\": "src/",
"PhlySimplePageTest\\": "test/"
},
"classmap": [
"./"
]
}
},
"require": {
"php": ">=5.3.3",
"zendframework/zend-console": "2.*",
"zendframework/zend-eventmanager": "2.*",
"zendframework/zend-http": "2.*",
"zendframework/zend-mvc": "2.*",
"zendframework/zend-stdlib": "2.*",
"zendframework/zend-view": "2.*"
"php": "^5.6 || ^7.0",
"zendframework/zend-cache": "^2.9",
"zendframework/zend-console": "^2.8",
"zendframework/zend-eventmanager": "^2.6.4 || ^3.2.1",
"zendframework/zend-http": "^2.9.1",
"zendframework/zend-mvc": "^2.7.9 || ^3.1.1",
"zendframework/zend-stdlib": "^2.7.7 || ^3.2.1",
"zendframework/zend-view": "^2.9.1"
},
"require-dev": {
"fabpot/php-cs-fixer": "*@dev",
"phpunit/PHPUnit": "~3.7.0",
"zendframework/zend-servicemanager": "2.*"
"zendframework/zend-coding-standard": "~1.0.0",
"phpunit/phpunit": "^5.7.25 || ^6.4.4",
"zendframework/zend-servicemanager": "^2.7.11 || ^3.4.0"
},
"extra": {
"zf": {
"module": "PhlySimplePage"
}
},
"scripts": {
"check": [
"@cs-check",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"upload-coverage": "coveralls -v"
}
}
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework coding standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>
</ruleset>
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="phly-simple-page">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
File renamed without changes.
File renamed without changes.
28 changes: 7 additions & 21 deletions Module.php → src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,6 @@
*/
class Module
{
/**
* Retrieve autoloader configuration for this module
*
* @return array
*/
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
));
}

/**
* Retrieve application configuration for this module
*
Expand All @@ -46,11 +32,11 @@ public function getConfig()
*/
public function getConsoleUsage()
{
return array(
return [
'phlysimplepage cache clear all' => 'Clear caches for all static pages',
'phlysimplepage cache clear --page=' => 'Clear caches for a single static page',
array('--page', 'Page name as matched via routing'),
);
['--page', 'Page name as matched via routing'],
];
}

/**
Expand All @@ -67,12 +53,12 @@ public function onBootstrap($e)
{
$app = $e->getTarget();
$events = $app->getEventManager();
$events->attach('route', array($this, 'onRoutePost'), -100);
$events->attach('route', [$this, 'onRoutePost'], -100);

$services = $app->getServiceManager();
if ($services->has('PhlySimplePage\PageCache')) {
$listener = $services->get('PhlySimplePage\PageCacheListener');
$events->attach($listener);
$listener->attach($events);
}
}

Expand All @@ -99,7 +85,7 @@ public function onRoutePost($e)
$app = $e->getTarget();
$events = $app->getEventManager();
$shared = $events->getSharedManager();
$shared->attach('PhlySimplePage\PageController', 'dispatch', array($this, 'onDispatchPost'), -1);
$shared->attach('PhlySimplePage\PageController', 'dispatch', [$this, 'onDispatchPost'], -1);
}

/**
Expand Down Expand Up @@ -143,6 +129,6 @@ public function onDispatchPost($e)
*/
public static function normalizeCacheKey($key)
{
return str_replace(array('/', '\\', '.'), '_', $key);
return str_replace(['/', '\\', '.'], '_', $key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(AbstractAdapter $cache)
*
* @param EventManagerInterface $events
*/
public function attach(EventManagerInterface $events)
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach('route', array($this, 'onRoutePost'), -99);
$this->listeners[] = $events->attach('finish', array($this, 'onFinishPost'), -10001);
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions src/PhlySimplePage/PageController.php → src/PageController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @link https://github.com/weierophinney/PhlySimplePage for the canonical source repository
* @copyright Copyright (c) 2012 Matthew Weier O'Phinney (http://mwop.net)
* @copyright Copyright (c) 2012-2020 Matthew Weier O'Phinney (https://mwop.net)
* @license https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
*/

Expand All @@ -16,7 +16,7 @@
use Zend\Mvc\Exception;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Router\RouteMatch;
use Zend\Stdlib\DispatchableInterface;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
Expand Down Expand Up @@ -118,19 +118,22 @@ public function getEvent()
public function dispatch(RequestInterface $request, ResponseInterface $response = null)
{
$event = $this->getEvent();
if (!$event) {
if (! $event) {
$event = new MvcEvent();
}

$event->setRequest($request);
if ($response) {
$event->setResponse($response);
}

$event->setTarget($this);
$event->setName(__FUNCTION__);

$results = $this->getEventManager()->trigger(__FUNCTION__, $event, function ($r) {
return ($r instanceof ResponseInterface);
});
$results = $this->getEventManager()
->triggerEventUntil(function ($r) {
return $r instanceof ResponseInterface;
}, $event);

if ($results->stopped()) {
return $results->last();
Expand Down Expand Up @@ -158,7 +161,7 @@ public function dispatch(RequestInterface $request, ResponseInterface $response
*/
public function onDispatch(EventInterface $e)
{
if (!$e instanceof MvcEvent) {
if (! $e instanceof MvcEvent) {
throw new Exception\DomainException(sprintf(
'%s requires an MvcEvent instance; received "%s"',
__CLASS__,
Expand Down
72 changes: 0 additions & 72 deletions test/Bootstrap.php

This file was deleted.

Loading

0 comments on commit 64ffab3

Please sign in to comment.