Skip to content

Commit

Permalink
Close cmb69#25: Abstract over the "test helpers"
Browse files Browse the repository at this point in the history
We introduce our own TestCase base class, which offers the possibility
to mock functions and static methods, and to change the value of global
constants.  We keep it simple and use tcz/phpunit-mockfunction to
provide most of the needed functionality.  We also introduce some
factory methods which can be used to create instances *instead* of
using the `new` operator, which we cannot mock.
  • Loading branch information
cmb69 committed Dec 7, 2017
1 parent 82f3960 commit 6556801
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 213 deletions.
12 changes: 10 additions & 2 deletions classes/InfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@

class InfoController
{
/**
* @return self
*/
public static function create()
{
return new self;
}

public function defaultAction()
{
global $pth;

(new View('pfw'))
View::create('pfw')
->template('info')
->data([
'logo' => "{$pth['folder']['plugins']}pfw/pfw.png",
'version' => Plugin::VERSION,
'checks' => (new SystemCheckService)
'checks' => SystemCheckService::create()
->minPhpVersion('5.4.0')
->minXhVersion('1.6.3')
->writable("{$pth['folder']['plugins']}pfw/css/")
Expand Down
2 changes: 1 addition & 1 deletion classes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function handleAdministration()
switch ($admin) {
case '':
ob_start();
(new InfoController)->defaultAction();
InfoController::create()->defaultAction();
$o .= ob_get_clean();
break;
default:
Expand Down
8 changes: 8 additions & 0 deletions classes/SystemCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
*/
class SystemCheckService
{
/**
* @return self
*/
public static function create()
{
return new self;
}

/**
* @var array
*/
Expand Down
70 changes: 70 additions & 0 deletions classes/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* Copyright 2017 Christoph M. Becker
*
* This file is part of Pfw_XH.
*
* Pfw_XH is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pfw_XH 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pfw_XH. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Pfw;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use PHPUnit_Extensions_MockFunction;
use PHPUnit_Extensions_MockStaticMethod;

abstract class TestCase extends PHPUnitTestCase
{
/**
* Mocks a function in the scope of an object.
*
* @param string $function
* @param object $scopeObject
* @return PHPUnit_Extensions_MockFunction
*/
protected function mockFunction($function, $scopeObject = null)
{
return new PHPUnit_Extensions_MockFunction($function, $scopeObject);
}

/**
* Mocks a static method in the scope of an object.
*
* @param string $class
* @param string $method
* @param object $scopeObject
* @return PHPUnit_Extensions_MockStaticMethod
*/
protected function mockStaticMethod($class, $method, $scopeObject = null)
{
return new PHPUnit_Extensions_MockStaticMethod("$class::$method", $scopeObject);
}

/**
* (Re)defines a constant.
*
* @param string $name
* @param mixed $value
* @return void
*/
protected function setConstant($name, $value)
{
if (defined($name)) {
runkit_constant_redefine($name, $value);
} else {
define($name, $value);
}
}
}
9 changes: 9 additions & 0 deletions classes/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
*/
class View
{
/**
* @param string $pluginname
* @return self
*/
public function create($pluginname)
{
return new self($pluginname);
}

/**
* @var string
*/
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"phpmd/phpmd": "2.6.*",
"bartlett/php-compatinfo": "5.0.*",
"phpunit/phpunit": "5.7.*",
"tcz/phpunit-mockfunction": "1.0.*",
"mikey179/vfsStream": "1.6.*"
}
}
Loading

0 comments on commit 6556801

Please sign in to comment.