Skip to content

Commit

Permalink
Merge pull request #258 from FriendsOfCake/cake-3.7
Browse files Browse the repository at this point in the history
Fix deprecation errors on CakePHP 3.7.
  • Loading branch information
ADmad authored Dec 20, 2018
2 parents 94bd86a + 8e47659 commit d617335
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ before_script:
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest ; fi

- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:"^3.0"; fi
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan:^0.8; fi
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan:^0.9; fi

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi

- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 5 src; fi
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse src; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 3
ignoreErrors:
- "#Offset 'element' does not exist on array\\('params' => mixed\\)#"
25 changes: 25 additions & 0 deletions src/TestSuite/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace BootstrapUI\TestSuite;

/**
* Test case class.
*/
class TestCase extends \Cake\TestSuite\TestCase
{
/**
* Helper method for check deprecation methods
*
* @param callable $callable callable function that will receive asserts
* @return void
*/
public function deprecated($callable)
{
$errorLevel = error_reporting();
error_reporting(E_ALL ^ E_USER_DEPRECATED);
try {
$callable();
} finally {
error_reporting($errorLevel);
}
}
}
25 changes: 25 additions & 0 deletions src/View/Helper/FlashHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BootstrapUI\View\Helper;

use Cake\View\Helper;
use Cake\View\View;

/**
* FlashHelper class to render flash messages.
Expand All @@ -25,6 +26,30 @@ class FlashHelper extends Helper
'element' => 'BootstrapUI.Flash/default'
];

/**
* Request instance.
*
* @var \Cake\Http\ServerRequest;
*/
public $request;

/**
* Constructor
*
* @param \Cake\View\View $View View
* @param array $config Config
*/
public function __construct(View $View, array $config = [])
{
if (method_exists($View, 'getRequest')) {
$this->request = $View->getRequest();
} else {
$this->request = $View->request;
}

parent::__construct($View, $config);
}

/**
* Similar to the core's FlashHelper used to render the message set in FlashComponent::set().
*
Expand Down
12 changes: 12 additions & 0 deletions src/View/Helper/PaginatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

class PaginatorHelper extends \Cake\View\Helper\PaginatorHelper
{
/**
* Request instance.
*
* @var \Cake\Http\ServerRequest;
*/
public $request;

/**
* Constructor. Overridden to merge passed args with URL options.
Expand All @@ -14,6 +20,12 @@ class PaginatorHelper extends \Cake\View\Helper\PaginatorHelper
*/
public function __construct(View $View, array $config = [])
{
if (method_exists($View, 'getRequest')) {
$this->request = $View->getRequest();
} else {
$this->request = $View->request;
}

$this->_defaultConfig['templates'] = [
'nextActive' => '<li class="next"><a rel="next" aria-label="Next" href="{{url}}">' .
'<span aria-hidden="true">{{text}}</span></a></li>',
Expand Down
33 changes: 18 additions & 15 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace BootstrapUI\Test\TestCase\View\Helper;

use BootstrapUI\TestSuite\TestCase;
use BootstrapUI\View\Helper\FlashHelper;
use Cake\Core\Plugin;
use Cake\Http\ServerRequest;
use Cake\Network\Session;
use Cake\TestSuite\TestCase;
use Cake\View\View;

/**
Expand All @@ -24,6 +25,11 @@ class FlashHelperTest extends TestCase
*/
public $Flash;

/**
* @var Session
*/
public $session;

/**
* setUp method
*
Expand All @@ -32,19 +38,16 @@ class FlashHelperTest extends TestCase
public function setUp()
{
parent::setUp();
$this->View = new View();
$session = null;
if (method_exists($this, 'deprecated')) {
$this->deprecated(function () use (&$session) {
$session = new Session();
});
} else {
$session = new Session();
}
$this->View->request = new ServerRequest(['session' => $session]);
$this->Flash = new FlashHelper($this->View);

$session->write([

$this->deprecated(function () {
Plugin::load('BootstrapUI', ['path' => ROOT . DS]);
$this->session = new Session();
});

$this->View = new View(new ServerRequest(['session' => $this->session]));
$this->Flash = new FlashHelper($this->View, []);

$this->session->write([
'Flash' => [
'flash' => [
'key' => 'flash',
Expand Down Expand Up @@ -129,7 +132,7 @@ public function testRender()
*/
public function testRenderForMultipleMessages()
{
$this->View->request->getSession()->write([
$this->session->write([
'Flash' => [
'flash' => [
[
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use BootstrapUI\View\Helper\FormHelper;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Http\ServerRequest as Request;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -36,16 +36,16 @@ public function setUp()
Configure::write('App.base', '');
Configure::write('App.namespace', 'BootstrapUI\Test\TestCase\View\Helper');
Configure::delete('Asset');
$this->View = new View();

$this->Form = new FormHelper($this->View);
$request = new Request('articles/add');
$this->View = new View($request);

$this->Form = new FormHelper($this->View);
$request = $request->withAttribute('here', '/articles/add');
$request = $request->withParam('controller', 'articles');
$request = $request->withParam('action', 'add');
$request = $request->withAttribute('webroot', '');
$request = $request->withAttribute('base', '');
$this->Form->Url->request = $this->Form->request = $request;

$this->article = [
'schema' => [
Expand Down
65 changes: 17 additions & 48 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use BootstrapUI\View\Helper\PaginatorHelper;
use Cake\Core\Configure;
use Cake\Http\ServerRequest as Request;
use Cake\I18n\I18n;
use Cake\Network\Request;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Cake\View\View;
Expand All @@ -25,11 +25,6 @@ class PaginatorHelperTest extends TestCase
*/
public $Paginator;

/**
* @var string
*/
public $locale;

/**
* setUp method
*
Expand All @@ -38,48 +33,10 @@ class PaginatorHelperTest extends TestCase
public function setUp()
{
parent::setUp();
Configure::write('Config.language', 'eng');
$this->View = new View();
$this->Paginator = new PaginatorHelper($this->View);
$this->Paginator->Js = $this->getMockBuilder('Cake\View\Helper\PaginatorHelper')
->setConstructorArgs([$this->View])
->getMock();
$this->Paginator->request = new Request();
$this->Paginator->request = $this->Paginator->request->withAttribute('params', [
'paging' => [
'Article' => [
'page' => 1,
'current' => 9,
'count' => 62,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 7,
'sort' => null,
'direction' => null,
'limit' => null,
]
]
]);

Configure::write('Routing.prefixes', []);
Router::reload();
Router::connect('/:controller/:action/*');
Router::connect('/:plugin/:controller/:action/*');

$this->locale = I18n::getLocale();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
unset($this->View, $this->Paginator);

I18n::setLocale($this->locale);
}

/**
Expand All @@ -89,15 +46,15 @@ public function tearDown()
*/
public function testNumbers()
{
$this->Paginator->request = $this->Paginator->request->withParam('paging', [
$this->setupHelper([
'Client' => [
'page' => 8,
'current' => 3,
'count' => 30,
'prevPage' => false,
'nextPage' => 2,
'pageCount' => 15,
]
],
]);
$result = $this->Paginator->numbers();
$expected = [
Expand Down Expand Up @@ -143,15 +100,15 @@ public function testNumbers()
];
$this->assertHtml($expected, $result);

$this->Paginator->request = $this->Paginator->request->withParam('paging', [
$this->setupHelper([
'Client' => [
'page' => 1,
'current' => 1,
'count' => 2,
'prevPage' => false,
'nextPage' => 2,
'pageCount' => 2,
]
],
]);
$result = $this->Paginator->numbers(['size' => 'lg']);
$expected = [
Expand All @@ -162,4 +119,16 @@ public function testNumbers()
];
$this->assertHtml($expected, $result);
}

protected function setupHelper($options)
{
$request = new Request();
$request = $request->withAttribute('params', [
'pass' => [],
'paging' => $options,
]);

$this->View = new View($request);
$this->Paginator = new PaginatorHelper($this->View);
}
}
4 changes: 2 additions & 2 deletions tests/TestCase/View/UIViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()
parent::setUp();

$this->View = new UIView();
$this->View->layout = 'default';
$this->View->setLayout('default');
}

/**
Expand All @@ -43,6 +43,6 @@ public function tearDown()
public function testInitialize()
{
$this->View->initialize();
$this->assertEquals('BootstrapUI.default', $this->View->layout);
$this->assertEquals('BootstrapUI.default', $this->View->getLayout());
}
}
Loading

0 comments on commit d617335

Please sign in to comment.