Skip to content
Permalink
Browse files

Add emulation for public properties.

In order to get to PSR7 compliance & immutability the various public
properties will all need to be deprecated. These public properties don't
have accessors and are currently being offered as attributes in the
middleware layers. Being consistent will allow us to remove the
RequestTransformer in the long run.
  • Loading branch information...
markstory committed Aug 31, 2016
1 parent 20e8d3c commit bd4665720e464949ef2d48e7975e55827a37987d
Showing with 121 additions and 3 deletions.
  1. +27 −2 src/Network/Request.php
  2. +94 −1 tests/TestCase/Network/RequestTest.php
@@ -19,6 +19,7 @@
use Cake\Core\Configure;
use Cake\Network\Exception\MethodNotAllowedException;
use Cake\Utility\Hash;
use InvalidArgumentException;
/**
* A class that helps wrap Request information and particulars about a single request.
@@ -1437,7 +1438,12 @@ public function withParam($name, $value)
public function withAttribute($name, $value)
{
$new = clone $this;
$new->attributes[$name] = $value;
$emulated = ['webroot', 'base', 'params'];
if (in_array($name, $emulated, true)) {
$new->{$name} = $value;
} else {
$new->attributes[$name] = $value;
}
return $new;
}
@@ -1447,10 +1453,17 @@ public function withAttribute($name, $value)
* @param string $name The attribute name.
* @param mixed $value The value of the attribute.
* @return static
* @throws InvalidArgumentException
*/
public function withoutAttribute($name)
{
$new = clone $this;
$emulated = ['webroot', 'base', 'params'];
if (in_array($name, $emulated, true)) {
throw new InvalidArgumentException(
"You cannot unset '$name'. It is a required CakePHP attribute."
);
}
unset($new->attributes[$name]);
return $new;
}
@@ -1464,6 +1477,10 @@ public function withoutAttribute($name)
*/
public function getAttribute($name, $default = null)
{
$emulated = ['webroot', 'base', 'params'];
if (in_array($name, $emulated, true)) {
return $this->{$name};
}
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}
@@ -1473,11 +1490,19 @@ public function getAttribute($name, $default = null)
/**
* Get all the attributes in the request.
*
* This will include the params, webroot, and base attributes that CakePHP
* provides.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
$emulated = [
'params' => $this->params,
'webroot' => $this->webroot,
'base' => $this->base
];
return $this->attributes + $emulated;
}
/**
@@ -2659,6 +2659,63 @@ public function testWithAttribute()
$this->assertSame(['complex'], $update->getAttribute('key'));
}
/**
* Test that withAttribute() can modify the deprecated public properties.
*
* @return void
*/
public function testWithAttributesCompatibility()
{
$request = new Request([
'params' => [
'controller' => 'Articles',
'action' => 'index'
],
'base' => '/cakeapp',
'webroot' => '/cakeapp/'
]);
$new = $request->withAttribute('base', '/replace')
->withAttribute('webroot', '/replace/')
->withAttribute('params', ['controller' => 'Tags']);
// Original request should not change.
$this->assertSame('/cakeapp', $request->getAttribute('base'));
$this->assertSame('/cakeapp/', $request->getAttribute('webroot'));
$this->assertSame(
['controller' => 'Articles', 'action' => 'index'],
$request->getAttribute('params')
);
$this->assertSame('/replace', $new->getAttribute('base'));
$this->assertSame('/replace', $new->base);
$this->assertSame('/replace/', $new->getAttribute('webroot'));
$this->assertSame('/replace/', $new->webroot);
$this->assertSame(['controller' => 'Tags'], $new->getAttribute('params'));
$this->assertSame(['controller' => 'Tags'], $new->params);
}
/**
* Test that getAttribute() can read deprecated public properties.
*
* @dataProvider emulatedPropertyProvider
* @return void
*/
public function testGetAttributesCompatibility($prop)
{
$request = new Request([
'params' => [
'controller' => 'Articles',
'action' => 'index'
],
'base' => '/cakeapp',
'webroot' => '/cakeapp/'
]);
$this->assertSame($request->{$prop}, $request->getAttribute($prop));
}
/**
* Test getting all attributes.
*
@@ -2676,7 +2733,16 @@ public function testGetAttributes()
$expected = [
'key' => 'value',
'nully' => null,
'falsey' => false
'falsey' => false,
'params' => [
'plugin' => null,
'controller' => null,
'action' => null,
'_ext' => null,
'pass' => [],
],
'webroot' => '',
'base' => ''
];
$this->assertEquals($expected, $new->getAttributes());
}
@@ -2696,6 +2762,33 @@ public function testWithoutAttribute()
$this->assertNull($update->getAttribute('key'));
}
/**
* Test that withoutAttribute() cannot remove deprecated public properties.
*
* @dataProvider emulatedPropertyProvider
* @expectedException InvalidArgumentException
* @return void
*/
public function testWithoutAttributesDenyEmulatedProperties($prop)
{
$request = new Request([]);
$request->withoutAttribute($prop);
}
/**
* Data provider for emulated property tests.
*
* @return array
*/
public function emulatedPropertyProvider()
{
return [
['params'],
['base'],
['webroot']
];
}
/**
* loadEnvironment method
*

0 comments on commit bd46657

Please sign in to comment.
You can’t perform that action at this time.