Skip to content

Upgrade version CakePHP 4.x #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.buildpath
/.project
/.idea
/.settings/
/vendor/
/composer.lock
/nbproject/
/phpunit.xml
/tmp
/.phpunit.result.cache
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
}
],
"require": {
"cakephp/cakephp": "~3.0",
"cakephp/cakephp": "^4.0",
"tijsverkoyen/css-to-inline-styles": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^8"
},
"autoload": {
"psr-4": {
"InlineCss\\": "src"
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ This plugin provides a CakePHP helper that uses [CssToInlineStyles](https://gith

## Requirements

* CakePHP 3.x
* CakePHP 4.x

## Installation

This plugin should be installed using Composer:-

```
composer require drmonkeyninja/cakephp-inline-css:3.0.*
composer require drmonkeyninja/cakephp-inline-css:4.0.*
```

Then add the following line to your bootstrap.php to load the plugin.
Expand Down
10 changes: 6 additions & 4 deletions src/View/Helper/InlineCssHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class InlineCssHelper extends Helper
/**
* After layout logic.
*
* @param \Cake\Event\Event $event Event
* @param string $layoutFile Layout filename
* @param \Cake\Event\Event $event Event
* @param string $layoutFile Layout filename
*
* @throws \TijsVerkoyen\CssToInlineStyles\Exception
*/
public function afterLayout(\Cake\Event\Event $event, $layoutFile)
{
$content = $this->_View->Blocks->get('content');
$content = $this->_View->fetch('content');

if (!isset($this->InlineCss)) {
$this->InlineCss = new CssToInlineStyles();
Expand All @@ -26,7 +28,7 @@ public function afterLayout(\Cake\Event\Event $event, $layoutFile)
$this->InlineCss->setUseInlineStylesBlock(true);
$content = $this->InlineCss->convert();

$this->_View->Blocks->set('content', $content);
$this->_View->assign('content', $content);

return;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/View/Helper/InlineCssHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

namespace InlineCss\TestCase\View\Helper;

use Cake\Network\Request;
use Cake\Http\ServerRequest as Request;
use Cake\TestSuite\TestCase;
use Cake\View\View;
use InlineCss\View\Helper\InlineCssHelper;

class InlineCssHelperTest extends TestCase
{

public $InlineCss = null;

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand All @@ -21,12 +21,12 @@ public function setUp()

public function testAfterLayout()
{
$this->View->Blocks->set('content', '<style type="text/css">a{color:red}</style><p><a href="#">Test</a></p>');
$this->View->assign('content', '<style type="text/css">a{color:red}</style><p><a href="#">Test</a></p>');

$this->InlineCss->afterLayout(new \Cake\Event\Event(''), null);

$expected = '<p><a href="#" style="color: red;">Test</a></p>';
$this->assertContains($expected, $this->View->Blocks->get('content'));
$this->assertStringContainsString($expected, $this->View->fetch('content'));
}

}
55 changes: 28 additions & 27 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
require dirname(__DIR__) . '/vendor/cakephp/cakephp/src/basics.php';
require dirname(__DIR__) . '/vendor/autoload.php';

use Cake\Cache\Cache;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;

require dirname(__DIR__).'/vendor/cakephp/cakephp/src/basics.php';
require dirname(__DIR__).'/vendor/autoload.php';
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
Expand All @@ -13,59 +18,55 @@
}
define('ROOT', dirname(__DIR__));
define('APP_DIR', 'src');
define('APP', rtrim(sys_get_temp_dir(), DS) . DS . APP_DIR . DS);
define('APP', rtrim(sys_get_temp_dir(), DS).DS.APP_DIR.DS);
if (!is_dir(APP)) {
mkdir(APP, 0770, true);
}
define('TMP', ROOT . DS . 'tmp' . DS);
define('TMP', ROOT.DS.'tmp'.DS);
if (!is_dir(TMP)) {
mkdir(TMP, 0770, true);
}
define('CONFIG', dirname(__FILE__) . DS . 'config' . DS);
define('LOGS', TMP . 'logs' . DS);
define('CACHE', TMP . 'cache' . DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT . '/vendor/cakephp/cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('CAKE', CORE_PATH . 'src' . DS);
define('CONFIG', dirname(__FILE__).DS.'config'.DS);
define('LOGS', TMP.'logs'.DS);
define('CACHE', TMP.'cache'.DS);
define('CAKE_CORE_INCLUDE_PATH', ROOT.'/vendor/cakephp/cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH.DS);
define('CAKE', CORE_PATH.'src'.DS);
Cake\Core\Configure::write('App', [
'namespace' => 'App'
'namespace' => 'App',
]);
Cake\Core\Configure::write('debug', true);
$cache = [
'default' => [
'engine' => 'File',
'path' => CACHE
'path' => CACHE,
],
'_cake_core_' => [
'className' => 'File',
'prefix' => 'crud_myapp_cake_core_',
'path' => CACHE . 'persistent/',
'path' => CACHE.'persistent/',
'serialize' => true,
'duration' => '+10 seconds'
'duration' => '+10 seconds',
],
'_cake_model_' => [
'className' => 'File',
'prefix' => 'crud_my_app_cake_model_',
'path' => CACHE . 'models/',
'path' => CACHE.'models/',
'serialize' => 'File',
'duration' => '+10 seconds'
]
'duration' => '+10 seconds',
],
];
Cake\Cache\Cache::config($cache);
Cake\Core\Plugin::load('SocialShare', ['path' => ROOT . DS]);
Cache::setConfig($cache);
// Ensure default test connection is defined
if (!getenv('db_class')) {
putenv('db_class=Cake\Database\Driver\Sqlite');
putenv('db_dsn=sqlite::memory:');
}
Cake\Datasource\ConnectionManager::config('test', [
ConnectionManager::setConfig('test', [
'className' => 'Cake\Database\Connection',
'driver' => getenv('db_class'),
'dsn' => getenv('db_dsn'),
'database' => getenv('db_database'),
'username' => getenv('db_username'),
'password' => getenv('db_password'),
'timezone' => 'UTC',
'quoteIdentifiers' => true,
'driver' => 'Cake\Database\Driver\Sqlite',
'database' => TMP.'test.sqlite',
'encoding' => 'utf8',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
]);