Skip to content

Commit

Permalink
Merge pull request #10 from markstory/5x-upgrade
Browse files Browse the repository at this point in the history
Upgrade to cake 5
  • Loading branch information
markstory authored Apr 21, 2024
2 parents 243fe45 + e66c24c commit 054fce8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 80 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.2']
php-version: ['8.1', '8.2']
prefer-lowest: ['']
include:
- php-version: '7.4'
- php-version: '8.1'
prefer-lowest: 'prefer-lowest'

steps:
Expand Down Expand Up @@ -55,17 +55,17 @@ jobs:
fi
- name: Setup problem matchers for PHPUnit
if: matrix.php-version == '7.4'
if: matrix.php-version == '8.1'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
if [[ ${{ matrix.php-version }} == '8.1' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Submit code coverage
if: matrix.php-version == '7.4'
if: matrix.php-version == '8.1'
uses: codecov/codecov-action@v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.phpunit.result.cache
/vendor
/composer.lock
9 changes: 0 additions & 9 deletions .stickler.yml

This file was deleted.

16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
}
},
"require": {
"php": ">=7.4.0",
"cakephp/cakephp": "^4.0",
"cakephp/plugin-installer": "*",
"geshi/geshi": "^1.0"
"php": ">=8.1",
"cakephp/cakephp": "^5.0",
"cakephp/plugin-installer": "^2.0.1",
"geshi/geshi": "^1.0.9.1"
},
"require-dev": {
"phpunit/phpunit": "^8",
"cakephp/cakephp-codesniffer": "^4.0"
"phpunit/phpunit": "^10.1.0 <=10.5.3",
"cakephp/cakephp-codesniffer": "^5.0"
},
"scripts": {
"cs-check": "phpcs --colors -p src/ tests/",
"cs-fix": "phpcbf --colors -p src/ tests/"
},
"prefer-stable": true,
"config": {
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Ignore vendor tests in code coverage reports -->
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class Plugin extends BasePlugin
*
* @var string
*/
protected $name = 'Geshi';
protected ?string $name = 'Geshi';
}
49 changes: 8 additions & 41 deletions src/View/Helper/GeshiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

use Cake\View\Helper;
use Cake\View\StringTemplateTrait;
use Cake\View\View;
use GeSHi;
use InvalidArgumentException;

/**
* Expose Geshi Syntax highlighting in a CakePHP application.
Expand Down Expand Up @@ -47,7 +45,7 @@ class GeshiHelper extends Helper
*
* @param array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'configPath' => '',
'features' => [],
'validContainers' => ['pre'],
Expand All @@ -74,38 +72,7 @@ class GeshiHelper extends Helper
*
* @var \GeSHi|null
*/
protected $_geshi = null;

/**
* Magic getter for backwards compatibility with public variables.
*
* @param string $name The attribute to read
* @return mixed
*/
public function __get(string $name)
{
if (!array_key_exists($name, $this->_defaultConfig)) {
throw new InvalidArgumentException("Invalid configuration key {$name}");
}

return $this->getConfig($name);
}

/**
* Magic setter for backwards compatibility with public variables.
*
* @param string $name The attribute to set
* @param mixed $value The attribute value
* @return void
*/
public function __set(string $name, $value)
{
if (!array_key_exists($name, $this->_defaultConfig)) {
throw new InvalidArgumentException("Invalid configuration key {$name}");
}

$this->setConfig($name, $value, false);
}
protected ?GeSHi $_geshi = null;

/**
* Highlight a block of HTML containing defined blocks. Converts blocks from plain text
Expand Down Expand Up @@ -139,7 +106,7 @@ public function highlight(string $htmlString)
* @param bool $withStylesheet If true will include GeSHi's generated stylesheet.
* @return string Highlighted HTML.
*/
public function highlightText(string $text, string $language, bool $withStylesheet = false)
public function highlightText(string $text, string $language, bool $withStylesheet = false): string
{
$this->_getGeshi();
$this->_geshi->set_source($text);
Expand All @@ -159,7 +126,7 @@ public function highlightText(string $text, string $language, bool $withStyleshe
* @param string $language The language to highlight as.
* @return string Highlighted HTML.
*/
public function highlightAsTable($text, $language)
public function highlightAsTable(string $text, string $language): string
{
$this->_getGeshi();
$this->_geshi->set_source($text);
Expand Down Expand Up @@ -219,7 +186,7 @@ protected function _getGeshi()
* @param array $matches code block groups
* @return string Completed replacement string
*/
protected function _processCodeBlock(array $matches)
protected function _processCodeBlock(array $matches): string
{
[$block, $openTag, $tagName, $lang, $code, $closeTag] = $matches;
unset($matches);
Expand Down Expand Up @@ -260,7 +227,7 @@ protected function _processCodeBlock(array $matches)
* @param string $lang Language
* @return string|null
*/
public function validLang(string $lang)
public function validLang(string $lang): ?string
{
if (in_array($lang, $this->_config['validLanguages'])) {
return $lang;
Expand All @@ -280,7 +247,7 @@ public function validLang(string $lang)
* @param \GeSHi $geshi Geshi instance
* @return void
*/
protected function _configureInstance(GeSHi $geshi)
protected function _configureInstance(GeSHi $geshi): void
{
if (empty($this->_config['features'])) {
if (empty($this->_config['configPath'])) {
Expand Down Expand Up @@ -310,7 +277,7 @@ protected function _configureInstance(GeSHi $geshi)
*
* @return string
*/
protected function _includeStylesheet()
protected function _includeStylesheet(): string
{
$template = <<<HTML
\n<style type="text/css">
Expand Down
34 changes: 17 additions & 17 deletions tests/TestCase/Helper/GeshiHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

class GeshiHelperTest extends TestCase
{
protected $settings = [
protected array $settings = [
'set_header_type' => ['GESHI_HEADER_NONE'],
'enable_line_numbers' => ['GESHI_FANCY_LINE_NUMBERS', 2],
'enable_classes' => [],
'set_tab_width' => [4],
];

protected $view;
protected $configPath;
protected View $view;
protected GeshiHelper $geshi;
protected string $configPath;

public function setUp(): void
{
Expand All @@ -26,7 +27,8 @@ public function setUp(): void

$this->view = $this->getMockBuilder(View::class)->getMock();
$this->geshi = new GeshiHelper($this->view);
$this->configPath = $this->geshi->configPath = dirname(dirname(dirname(__FILE__))) . DS;
$this->configPath = dirname(dirname(dirname(__FILE__))) . DS;
$this->geshi->setConfig('configPath', $this->configPath);
}

public function tearDown(): void
Expand All @@ -44,20 +46,18 @@ public function runVariants($method)
{
// Using a config file, traditional.
$this->geshi = new GeshiHelper($this->view);
$this->geshi->configPath = $this->configPath;
$this->geshi->setConfig('configPath', $this->configPath);
call_user_func([$this, $method]);
unset($this->geshi);

// Pre-configuration during instantiation, such as from controller.
$this->geshi = new GeshiHelper($this->view, $this->settings);
unset($this->geshi->configPath);
$this->geshi = new GeshiHelper($this->view, ['features' => $this->settings]);
call_user_func([$this, $method]);
unset($this->geshi);

// Configuration on the fly, such as from view.
$this->geshi = new GeshiHelper($this->view);
unset($this->geshi->configPath);
$this->geshi->features = $this->settings;
$this->geshi->setConfig('features', $this->settings);
call_user_func([$this, $method]);
unset($this->geshi);
}
Expand All @@ -79,7 +79,7 @@ public function testConfigVariants()
*/
public function testHighlight()
{
$this->geshi->showPlainTextButton = false;
$this->geshi->setConfig('showPlainTextButton', false);

// Simple one code block
$text = '<p>This is some text</p><pre lang="php"><?php echo $foo = "foo"; ?></pre><p>More text</p>';
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testHighlight()
$this->assertHtml($expected, $result);

// More than one valid code block container
$this->geshi->validContainers = ['pre', 'code'];
$this->geshi->setConfig('validContainers', ['pre', 'code'], false);
$text = '<pre lang="php"><?php echo $foo = "foo"; ?></pre><p>Text</p><code lang="php">echo $foo = "foo";</code>';
$result = $this->geshi->highlight($text);
$expected = [
Expand Down Expand Up @@ -202,8 +202,8 @@ public function testHighlight()
$this->assertHtml($expected, $result, true);

// No valid languages no highlights
$this->geshi->validContainers = ['pre'];
$this->geshi->validLanguages = [];
$this->geshi->setConfig('validContainers', ['pre']);
$this->geshi->setConfig('validLanguages', [], false);
$text = '<p>text</p><pre lang="php">echo $foo;</pre><p>text</p>';
$result = $this->geshi->highlight($text);
$expected = [
Expand Down Expand Up @@ -252,8 +252,8 @@ public function testPlainTextButton()
public function testNoTagReplacement()
{
// Simple one code block
$this->geshi->showPlainTextButton = false;
$this->geshi->containerMap = [];
$this->geshi->setConfig('showPlainTextButton', false);
$this->geshi->setConfig('containerMap', [], false);

$text = '<p>This is some text</p><pre lang="php"><?php echo $foo = "foo"; ?></pre><p>More text</p>';
$result = $this->geshi->highlight($text);
Expand Down Expand Up @@ -347,8 +347,8 @@ public function testTemplates()
// The button element and wrappers used for showPlainTextButton
'showplain' => '<a href="#null">Plain Text</a>',
]);
$this->geshi->validContainers = ['pre'];
$this->geshi->validLanguages = [];
$this->geshi->setConfig('validContainers', ['pre']);
$this->geshi->setConfig('validLanguages', [], false);
$text = '<p>text</p><pre lang="php">echo $foo;</pre><p>text</p>';
$result = $this->geshi->highlight($text);
$expected = [
Expand Down

0 comments on commit 054fce8

Please sign in to comment.