diff --git a/.editorconfig b/.editorconfig index a4e9086..de2fe7e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,7 +4,7 @@ root = true [*] -indent_style = tab +indent_style = space indent_size = 4 charset = "utf-8" end_of_line = lf diff --git a/.travis.yml b/.travis.yml index 8f5d3ab..806581d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,42 +1,50 @@ language: php php: - - 5.3 - 5.4 - 5.5 - 5.6 env: - global: - - PLUGIN_NAME=InlineCss - - REQUIRE="" - - DB=mysql CAKE_VERSION=2.4 - matrix: - - DB=mysql CAKE_VERSION=2.4 - - DB=mysql CAKE_VERSION=2.5 - - DB=mysql CAKE_VERSION=2.6 + - DB=mysql db_class='Cake\Database\Driver\Mysql' db_dsn='mysql:host=0.0.0.0;dbname=cakephp_test' db_database='cakephp_test' db_username='travis' db_password='' + global: + - DEFAULT=1 matrix: + fast_finish: true + include: - php: 5.4 - env: - - PHPCS=1 + env: PHPCS=1 DEFAULT=0 + + - php: 5.5 + env: COVERALLS=1 DEFAULT=0 + allow_failures: - php: 5.4 - env: - - PHPCS=1 + env: PHPCS=1 DEFAULT=0 before_script: - - git clone https://github.com/FriendsOfCake/travis.git --depth 1 ../travis - - ../travis/before_script.sh - - echo "require APP . 'vendor' . DS . 'autoload.php';" >> ../cakephp/app/Config/bootstrap.php + - composer self-update + - composer install --prefer-source --no-interaction --dev -script: - - ../travis/script.sh + - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi" + + - sh -c "if [ '$PHPCS' = '1' ]; then composer require --dev cakephp/cakephp-codesniffer=2.*; fi" + + - sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi" + - sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi" -after_success: - - ../travis/after_success.sh + - phpenv rehash + - set +H + - cp phpunit.xml.dist phpunit.xml + +script: + - sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --coverage-clover build/logs/clover.xml; fi" + - sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi" + - sh -c "if [ '$DEFAULT' = '1' ]; then phpunit; fi" + - sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP --ignore=tests src/ ; fi" notifications: email: false diff --git a/Test/Case/AllInlineCssTest.php b/Test/Case/AllInlineCssTest.php deleted file mode 100644 index 5d4a9e7..0000000 --- a/Test/Case/AllInlineCssTest.php +++ /dev/null @@ -1,21 +0,0 @@ -addTestDirectoryRecursive($path); - - return $suite; - } -} \ No newline at end of file diff --git a/Test/Case/View/Helper/InlineCssHelperTest.php b/Test/Case/View/Helper/InlineCssHelperTest.php deleted file mode 100644 index 9710790..0000000 --- a/Test/Case/View/Helper/InlineCssHelperTest.php +++ /dev/null @@ -1,37 +0,0 @@ -View = new View($Controller); - $this->InlineCss = new InlineCssHelper($this->View); - } - - public function testAfterLayout() { - $html = $this->View->Blocks->set('content', '

Test

'); - - $this->InlineCss->afterLayout(null); - - $expected = '

Test

'; - $this->assertContains($expected, $this->View->Blocks->get('content')); - } - - public function testIsHtml() { - $html = '

Hello World

'; - $result = $this->InlineCss->isHtml($html); - $this->assertTrue($result); - - $text = 'Hello World'; - $result = $this->InlineCss->isHtml($text); - $this->assertFalse($result); - } - -} diff --git a/View/Helper/InlineCssHelper.php b/View/Helper/InlineCssHelper.php deleted file mode 100644 index 95a39a8..0000000 --- a/View/Helper/InlineCssHelper.php +++ /dev/null @@ -1,48 +0,0 @@ -_View->Blocks->get('content'); - - // We only want to apply inline CSS to HTML emails, so first check if - // the content is HTML before proceeding. - if ($this->isHtml($content) === false) { - return; - } - - if (!isset($this->InlineCss)) { - $this->InlineCss = new CssToInlineStyles(); - } - - // Convert inline style blocks to inline CSS on the HTML content. - $this->InlineCss->setHTML($content); - $this->InlineCss->setUseInlineStylesBlock(true); - $content = $this->InlineCss->convert(); - - $this->_View->Blocks->set('content', $content); - - return; - } - -/** - * Checks if a string contains HTML or not. - * - * @param string $str String content - * @return bool - */ - public function isHtml($str) { - return $str !== strip_tags($str); - } - -} diff --git a/composer.json b/composer.json index 69032a6..8039422 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,18 @@ } ], "require": { + "cakephp/cakephp": "~3.0", "tijsverkoyen/css-to-inline-styles": "^1.5" }, - "extra": { - "installer-name": "InlineCss" + "autoload": { + "psr-4": { + "InlineCss\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Cake\\Test\\": "./vendor/cakephp/cakephp/tests", + "InlineCss\\Test\\": "tests" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..154dae2 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,43 @@ + + + + + + + + + + + ./tests/TestCase + + + + + + + + + + + + + + + + ./vendor/ + ./vendor/ + + ./tests/ + ./tests/ + + + + diff --git a/readme.md b/readme.md index ff06950..00e2453 100644 --- a/readme.md +++ b/readme.md @@ -6,20 +6,20 @@ This plugin provides a CakePHP helper that uses [CssToInlineStyles](https://gith ## Requirements -* CakePHP 2.x +* CakePHP 3.x ## Installation This plugin should be installed using Composer:- ``` -composer require drmonkeyninja/cakephp-inline-css:2.0.* +composer require drmonkeyninja/cakephp-inline-css:3.0.* ``` Then add the following line to your bootstrap.php to load the plugin. ```php -CakePlugin::load('InlineCss.InlineCss'); +Plugin::load('InlineCss'); ``` ## Usage @@ -27,10 +27,10 @@ CakePlugin::load('InlineCss.InlineCss'); To use this plugin you want to load the `InlineCss` helper to use with your email's HTML template:- ```php -$Email = new CakeEmail(); -$Email->template('welcome', 'fancy') +$email = new Email(); +$email->template('welcome', 'fancy') ->emailFormat('html') - ->helpers(array('InlineCss.InlineCss')) + ->helpers(['InlineCss.InlineCss']) ->to('bob@example.com') ->from('app@domain.com') ->send(); @@ -53,4 +53,4 @@ It will be rendered as:- This makes generating HTML emails a lot simpler as you can write your CSS in a more DRY approach. -I recommend you look consider deferring the sending of emails using something like the excellent [Queue plugin](https://github.com/dereuromark/cakephp-queue/tree/2.x) to improve your app's response time for users when using this plugin. +I recommend you look consider deferring the sending of emails using something like the excellent [Queue plugin](https://github.com/dereuromark/cakephp-queue) to improve your app's response time for users when using this plugin. diff --git a/src/View/Helper/InlineCssHelper.php b/src/View/Helper/InlineCssHelper.php new file mode 100644 index 0000000..50307af --- /dev/null +++ b/src/View/Helper/InlineCssHelper.php @@ -0,0 +1,34 @@ +_View->Blocks->get('content'); + + if (!isset($this->InlineCss)) { + $this->InlineCss = new CssToInlineStyles(); + } + + // Convert inline style blocks to inline CSS on the HTML content. + $this->InlineCss->setHTML($content); + $this->InlineCss->setUseInlineStylesBlock(true); + $content = $this->InlineCss->convert(); + + $this->_View->Blocks->set('content', $content); + + return; + } + +} diff --git a/tests/TestCase/View/Helper/InlineCssHelperTest.php b/tests/TestCase/View/Helper/InlineCssHelperTest.php new file mode 100644 index 0000000..cd05d30 --- /dev/null +++ b/tests/TestCase/View/Helper/InlineCssHelperTest.php @@ -0,0 +1,32 @@ +View = new View(new Request()); + $this->InlineCss = new InlineCssHelper($this->View); + } + + public function testAfterLayout() + { + $this->View->Blocks->set('content', '

Test

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

Test

'; + $this->assertContains($expected, $this->View->Blocks->get('content')); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..8452188 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,71 @@ + 'App' +]); +Cake\Core\Configure::write('debug', true); +$cache = [ + 'default' => [ + 'engine' => 'File', + 'path' => CACHE + ], + '_cake_core_' => [ + 'className' => 'File', + 'prefix' => 'crud_myapp_cake_core_', + 'path' => CACHE . 'persistent/', + 'serialize' => true, + 'duration' => '+10 seconds' + ], + '_cake_model_' => [ + 'className' => 'File', + 'prefix' => 'crud_my_app_cake_model_', + 'path' => CACHE . 'models/', + 'serialize' => 'File', + 'duration' => '+10 seconds' + ] +]; +Cake\Cache\Cache::config($cache); +Cake\Core\Plugin::load('SocialShare', ['path' => ROOT . DS]); +// 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', [ + '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, + 'cacheMetadata' => true, +]); diff --git a/tests/config/routes.php b/tests/config/routes.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/config/routes.php @@ -0,0 +1 @@ +