Skip to content

Commit

Permalink
Add interface checks for context objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 11, 2014
1 parent ec9f0fc commit 17e3dbf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
22 changes: 14 additions & 8 deletions src/View/Helper/FormHelper.php
Expand Up @@ -22,6 +22,7 @@
use Cake\Utility\Inflector;
use Cake\Utility\Security;
use Cake\View\Form\ArrayContext;
use Cake\View\Form\ContextInterface;
use Cake\View\Form\EntityContext;
use Cake\View\Form\NullContext;
use Cake\View\Helper;
Expand Down Expand Up @@ -204,10 +205,6 @@ protected function _addDefaultContextProviders() {
return new EntityContext($request, $data);
}
});

$this->addContextProvider('_default', function ($request, $data) {
return new NullContext($request, (array)$data);
});
}

/**
Expand Down Expand Up @@ -2908,20 +2905,29 @@ public function context() {
/**
* Find the matching context provider for the data.
*
* If no type can be matched the default provider will be returned.
* If no type can be matched a NullContext will be returned.
*
* @param mixed $data The data to get a context provider for.
* @return mixed Context provider.
* @throws RuntimeException when the context class does not implement the
* ContextInterface.
*/
protected function _buildContext($data) {
foreach ($this->_contextProviders as $key => $check) {
$context = $check($this->request, $data);
if ($context) {
return $context;
break;
}
}
$check = $this->_contextProviders['_default'];
return $check($this->request, $data);
if (!isset($context)) {
$context = new NullContext($this->request, $data);
}
if (!($context instanceof ContextInterface)) {
throw new \RuntimeException(
'Context objects must implement Cake\View\Form\ContextInterface'
);
}
return $context;
}

/**
Expand Down
21 changes: 18 additions & 3 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -553,12 +553,27 @@ public function testAddWidgetInvalid() {
*
* @return void
*/
public function testContextClassMatching() {
public function testAddContextProvider() {
$context = 'My data';
$this->Form->addContextProvider('test', function ($request, $data) use ($context) {
$this->assertInstanceOf('Cake\Network\Request', $request);
$this->assertEquals($context, $data);
return new \StdObject();
$this->assertEquals($context, $data['entity']);
return $this->getMock('Cake\View\Form\ContextInterface');
});
$this->Form->create($context);
}

/**
* Test adding an invalid context class.
*
* @expectedException RuntimeException
* @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
* @return void
*/
public function testAddContextProviderInvalid() {
$context = 'My data';
$this->Form->addContextProvider('test', function ($request, $data) use ($context) {
return new \StdClass();
});
$this->Form->create($context);
}
Expand Down

0 comments on commit 17e3dbf

Please sign in to comment.