Skip to content

Commit

Permalink
redoing for new hidev
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Apr 29, 2017
1 parent 63a63dd commit b002387
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 201 deletions.
81 changes: 81 additions & 0 deletions src/components/Phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* PHPUnit plugin for HiDev.
*
* @link https://github.com/hiqdev/hidev-phpunit
* @package hidev-phpunit
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
*/

namespace hidev\phpunit\components;

use hidev\base\File;
use hidev\handlers\BaseHandler;
use Yii;

/**
* PHPunit.
*/
class Phpunit extends \hidev\base\Component
{
protected $_version;

protected $_bootstrapFile;

public $colors;
public $coverageText;
public $coverageClover;

public function touchBootstrap()
{
if (!$this->getBootstrapFile()->exists()) {
$this->getBootstrapFile()->save();
}
}

public function getBootstrapFile()
{
if ($this->_bootstrapFile === null) {
$this->_bootstrapFile = Yii::createObject([
'class' => File::class,
'template' => 'phpunit/bootstrap.twig',
'path' => 'tests/_bootstrap.php',
]);
}

return $this->_bootstrapFile;
}

public function prepareArgs($args = [])
{
if ($this->coverageText) {
$args[] = '--coverage-text';
}
if ($this->coverageClover) {
$args[] = '--coverage-clover=' . (is_string($this->coverageClover) ? $this->coverageClover : 'coverage.clover');
}
if ($this->colors) {
$args[] = '--colors' . (version_compare($this->getVersion(), '4.7.0', '>=') ? '=' . $this->colors : '');
}

return $args;
}

public function getVersion()
{
if ($this->_version === null) {
$this->_version = $this->detectVersion();
}

return $this->_version;
}

public function detectVersion()
{
$lines = $this->exec('phpunit', ['--version']);

return explode(' ', $lines[0], 3)[1];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
*/

namespace hidev\phpunit\controllers;
namespace hidev\phpunit\components;

/**
* Goal for phpunit.xml.dist config file.
* `phpunit.xml` config file.
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class PhpunitConfigController extends \hidev\controllers\FileController
class PhpunitXml extends \hidev\components\ConfigFile
{
protected $_file = [
'path' => 'phpunit.xml.dist',
Expand Down
53 changes: 33 additions & 20 deletions src/config/hidev.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,41 @@
*/

return [
'components' => [
'config' => [
'phpunit' => [
'class' => 'hidev\phpunit\controllers\PhpunitController',
],
'phpunit.xml.dist' => [
'class' => 'hidev\phpunit\controllers\PhpunitConfigController',
],
'binaries' => [
'phpunit' => [
'package' => 'phpunit/phpunit',
'version' => '^4.8',
'download' => 'https://phar.phpunit.de/phpunit-old.phar',
],
'phpunit-skelgen' => [
'package' => 'phpunit/phpunit-skeleton-generator',
'version' => '^2.0',
'download' => 'https://phar.phpunit.de/phpunit-skelgen.phar',
'controllerMap' => [
'phpunit' => [
'class' => \hidev\phpunit\console\PhpunitController::class,
],
'phpunit.xml' => [
'class' => \hidev\phpunit\console\PhpunitXmlController::class,
],
'phpunit.xml.dist' => [
'class' => \hidev\phpunit\console\PhpunitXmlController::class,
],
],
'components' => [
'phpunit' => [
'class' => \hidev\phpunit\components\Phpunit::class,
],
'phpunit.xml' => [
'class' => \hidev\phpunit\components\PhpunitXml::class,
],
'view' => [
'theme' => [
'pathMap' => [
'@hidev/views' => ['@hidev/phpunit/views'],
],
],
'views' => [
'@hidev/phpunit/views',
],
'binaries' => [
'phpunit' => [
'package' => 'phpunit/phpunit',
'version' => '^4.8',
'download' => 'https://phar.phpunit.de/phpunit-old.phar',
],
'phpunit-skelgen' => [
'package' => 'phpunit/phpunit-skeleton-generator',
'version' => '^2.0',
'download' => 'https://phar.phpunit.de/phpunit-skelgen.phar',
],
],
],
Expand Down
142 changes: 142 additions & 0 deletions src/console/PhpunitController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* PHPUnit plugin for HiDev.
*
* @link https://github.com/hiqdev/hidev-phpunit
* @package hidev-phpunit
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
*/

namespace hidev\phpunit\console;

use hidev\base\File;
use hidev\handlers\BaseHandler;
use Yii;

/**
* PHPunit.
*/
class PhpunitController extends \hidev\base\Controller
{
protected $_before = ['phpunit.xml.dist'];

public $force;

public function getComponent()
{
return $this->take('phpunit');
}

/**
* Generates `tests/_bootstrap.php` and runs tests.
*/
public function actionIndex()
{
$this->getComponent()->touchBootstrap();

return $this->doRun();
}

protected function doRun()
{
$args = $this->getComponent()->prepareArgs();

return $this->passthru('phpunit', $args);
}

/**
* Generates skeleton class for fake.
*/
public function actionGenfake($file)
{
$path = $this->buildFakePath($file);
if (!$this->force && file_exists($path)) {
Yii::warning("already exists: $path");
return 1;
}

return $this->genFake($file, $path);
}

protected function genFake($file, $path)
{
$text = $this->getView()->render('phpunit/fake.twig', [
'class' => $this->buildClass($file),
'namespace' => $this->buildTestNamespace(),
'name' => $file,
]);
BaseHandler::write($path, $text);
}

/**
* Generates skeleton class for test.
*/
public function actionGentest($file)
{
$path = $this->buildTestPath($file);
if (!$this->force && file_exists($path)) {
Yii::warning("already exists: $path");
return 1;
}

return $this->genSkel($file);
}

protected static function prepareFile($file)
{
return substr($file, -4) === '.php' ? substr($file, 0, -4) : $file;
}

protected function genSkel($file)
{
$destPath = $this->buildTestPath($file);
$dir = dirname($destPath);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}

return $this->passthru('phpunit-skelgen', [
'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--',
$this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $destPath,
]);
}

protected function buildNamespace($dir = '')
{
return $this->take('package')->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : '');
}

protected function buildTestNamespace($dir = 'tests\\unit')
{
return $this->buildNamespace($dir);
}

protected function buildClass($file, $dir = '', $postfix = '')
{
return $this->buildNamespace($dir) . '\\' . strtr(static::prepareFile($file), '/', '\\') . $postfix;
}

protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test')
{
return $this->buildClass($file, $dir, $postfix);
}

protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '')
{
return $dir . DIRECTORY_SEPARATOR . $prefix . static::prepareFile($file) . $postfix . '.php';

//## XXX getting absolute path, think if needed
//return strncmp($path, '/', 1) === 0 ? $path : Yii::getAlias("@root/$path");
}

protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test')
{
return $this->buildPath($file, $dir, $prefix, $postfix);
}

protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '')
{
return $this->buildPath($file, $dir, $prefix, $postfix);
}
}
23 changes: 23 additions & 0 deletions src/console/PhpunitXmlController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* PHPUnit plugin for HiDev.
*
* @link https://github.com/hiqdev/hidev-phpunit
* @package hidev-phpunit
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
*/

namespace hidev\phpunit\console;

/**
* `phpunit.xml` generation.
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class PhpunitXmlController extends \hidev\base\Controller
{
public function actionIndex()
{
$this->take('phpunit.xml')->actionSave();
}
}
Loading

0 comments on commit b002387

Please sign in to comment.