Skip to content

Commit

Permalink
Merge pull request #6 from challgren/deprecatedwarning
Browse files Browse the repository at this point in the history
Fixing test and bumping cakephp version to 3.6
  • Loading branch information
rrd108 authored Nov 23, 2018
2 parents 54c5a7b + 18da45c commit 087821b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"homepage": "https://github.com/rrd108/cakephp-datalist",
"require": {
"cakephp/cakephp": "^3.5"
"cakephp/cakephp": "^3.6"
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0"
Expand Down
6 changes: 6 additions & 0 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use Cake\Core\Configure;
if (file_exists(ROOT . DS . 'config' . DS . 'form-templates.php')) {
Configure::load('form-templates');
}
2 changes: 1 addition & 1 deletion src/View/Widget/DatalistJsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DatalistJsWidget extends SelectBoxWidget
{

/** @var \Cake\View\StringTemplate $_templates */
protected $_templates;

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/TestCase/View/Widget/DatalistJsWidgetTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Datalist\Test\TestCase\View\Widget;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use Cake\View\StringTemplate;
use Datalist\View\Widget\DatalistJsWidget;
Expand All @@ -19,10 +20,10 @@ class DatalistJsWidgetTest extends TestCase
public function setUp()
{
parent::setUp();
//read template into $config variable
require __DIR__ . '/../../../../config/form-templates.php';
$config = Configure::read('datalistJs');
$this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
$templates = $config + ['option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>'];
$templates['datalistJs'] = $config;
$templates['option'] = '<option value="{{value}}"{{attrs}}>{{text}}</option>';
$this->templates = new StringTemplate($templates);
}

Expand Down
123 changes: 107 additions & 16 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,116 @@
* has been installed as a dependency of the plugin, or the plugin is itself
* installed as a dependency of an application.
*/
$findRoot = function ($root) {
do {
$lastRoot = $root;
$root = dirname($root);
if (is_dir($root . '/vendor/cakephp/cakephp')) {
return $root;
}
} while ($root !== $lastRoot);

throw new Exception("Cannot find the root of the application, unable to run tests");
};
$root = $findRoot(__FILE__);
unset($findRoot);
use Cake\Datasource\ConnectionManager;

chdir($root);
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('WINDOWS')) {
if (DS === '\\' || substr(PHP_OS, 0, 3) === 'WIN') {
define('WINDOWS', true);
} else {
define('WINDOWS', false);
}
}
define('ROOT', dirname(__DIR__));
define('TMP', ROOT . DS . 'tmp' . DS);
define('LOGS', TMP . 'logs' . DS);
define('CACHE', TMP . 'cache' . DS);
define('APP', ROOT . DS . 'tests' . DS . 'test_app' . DS . 'src' . DS);
define('APP_DIR', 'src');
define('CAKE_CORE_INCLUDE_PATH', ROOT . '/vendor/cakephp/cakephp');
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('CAKE', CORE_PATH . APP_DIR . DS);
define('WWW_ROOT', ROOT . DS . 'webroot' . DS);
define('CONFIG', dirname(__FILE__) . DS . 'config' . DS);

if (file_exists($root . '/config/bootstrap.php')) {
require $root . '/config/bootstrap.php';
require ROOT . '/vendor/autoload.php';
require CORE_PATH . 'config/bootstrap.php';

Cake\Core\Configure::write('App', [
'namespace' => 'App',
'encoding' => 'UTF-8',
'paths' => [
'templates' => [ROOT . DS . 'tests' . DS . 'test_app' . DS . 'src' . DS . 'Template' . DS],
]
]);
Cake\Core\Configure::write('datalistJs', '<input type="text" id="__{{id}}" name="__{{name}}" list="datalist-{{id}}" autocomplete="off"{{inputAttrs}}>'
. '<datalist id="datalist-{{id}}"{{datalistAttrs}}>{{content}}</datalist>'
. '<input type="hidden" name="{{name}}" id="{{id}}">'
. '<script>
if (CakePHP_datalist === undefined) {
var CakePHP_datalist = {};
}
CakePHP_datalist["{{id}}"] = {};
[].forEach.call(
document.querySelectorAll("#datalist-{{id}} option"),
function(element){
CakePHP_datalist["{{id}}"][element.value] = element.getAttribute("data-value");
});
document.getElementById("__{{id}}")
.addEventListener("blur",
function (e) {
document.getElementById("{{id}}").value = CakePHP_datalist["{{id}}"][e.target.value]
? CakePHP_datalist["{{id}}"][e.target.value]
: document.getElementById("__{{id}}").value;
});
</script>');
Cake\Core\Configure::write('debug', true);
$Tmp = new \Cake\Filesystem\Folder(TMP);
$Tmp->create(TMP . 'cache/models', 0770);
$Tmp->create(TMP . 'cache/persistent', 0770);
$Tmp->create(TMP . 'cache/views', 0770);
$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::setConfig($cache);

Cake\Mailer\Email::setConfigTransport('default', [
'className' => 'Debug',
]);
Cake\Mailer\Email::setConfig('default', [
'transport' => 'default',
]);
// Allow local overwrite
// E.g. in your console: export db_dsn="mysql://root:[email protected]/cake_test"
if (!getenv('db_class') && getenv('db_dsn')) {
ConnectionManager::setConfig('test', ['url' => getenv('db_dsn')]);
return;
}
require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
if (!getenv('db_class')) {
putenv('db_class=Cake\Database\Driver\Sqlite');
putenv('db_dsn=sqlite::memory:');
}
// Uses Travis config then (MySQL, Postgres, ...)
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,
'cacheMetadata' => true,
]);

0 comments on commit 087821b

Please sign in to comment.