-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from ademarco/drupal-7-array-casting
Drupal 7: Implement values array casting in __call() magic method Signed-off-by: Jonathan Hedstrom <[email protected]>
- Loading branch information
Showing
11 changed files
with
193 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Tests\Driver\Drupal7FieldHandlerTest | ||
*/ | ||
|
||
namespace Drupal\Tests\Driver; | ||
|
||
/** | ||
* Class Drupal7FieldHandlerTest | ||
* @package Drupal\Tests\Driver | ||
*/ | ||
class Drupal7FieldHandlerTest extends FieldHandlerAbstractTest { | ||
|
||
/** | ||
* @dataProvider dataProvider | ||
*/ | ||
public function testFieldHandlers($class_name, $entity, $entity_type, $field, $expected_values) | ||
{ | ||
$handler = $this->getMockHandler($class_name, $entity, $entity_type, $field); | ||
|
||
$field_name = $field['field_name']; | ||
$expanded_values = $handler->expand($this->values($entity->$field_name)); | ||
$this->assertArraySubset($expected_values, $expanded_values); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
*/ | ||
public function dataProvider() | ||
{ | ||
return array( | ||
|
||
// Test default text field provided as simple text. | ||
array( | ||
'DefaultHandler', | ||
(object) array('field_text' => 'Text'), | ||
'node', | ||
array('field_name' => 'field_text'), | ||
array('en' => array(array('value' => 'Text'))), | ||
), | ||
|
||
// Test default text field provided as array. | ||
array( | ||
'DefaultHandler', | ||
(object) array('field_text' => array('Text')), | ||
'node', | ||
array('field_name' => 'field_text'), | ||
array('en' => array(array('value' => 'Text'))), | ||
), | ||
|
||
// Test single-value date field provided as simple text. | ||
array( | ||
'DatetimeHandler', | ||
(object) array('field_date' => '2015-01-01 00:00:00'), | ||
'node', | ||
array('field_name' => 'field_date'), | ||
array('en' => array(array('value' => '2015-01-01 00:00:00'))), | ||
), | ||
|
||
// Test single-value date field provided as an array. | ||
array( | ||
'DatetimeHandler', | ||
(object) array('field_date' => array('2015-01-01 00:00:00')), | ||
'node', | ||
array('field_name' => 'field_date'), | ||
array('en' => array(array('value' => '2015-01-01 00:00:00'))), | ||
), | ||
|
||
// Test double-value date field. Can only be provided as an array | ||
// due to array type casting we perform in | ||
// \Drupal\Driver\Fields\Drupal7\AbstractFieldHandler::__call() | ||
array( | ||
'DatetimeHandler', | ||
(object) array('field_date' => array(array('2015-01-01 00:00:00', '2015-01-02 00:00:00'))), | ||
'node', | ||
array('field_name' => 'field_date', 'columns' => array('value' => '', 'value2' => '')), | ||
array('en' => array(array('value' => '2015-01-01 00:00:00', 'value2' => '2015-01-02 00:00:00'))), | ||
), | ||
|
||
); | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Tests\Driver\FieldHandlerAbstractTest | ||
*/ | ||
|
||
namespace Drupal\Tests\Driver; | ||
|
||
use \Mockery as m; | ||
|
||
/** | ||
* Class FieldHandlerAbstractTest | ||
* @package Drupal\Tests\Driver | ||
*/ | ||
abstract class FieldHandlerAbstractTest extends \PHPUnit_Framework_TestCase { | ||
|
||
public function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
/** | ||
* factory method to build and returned a mocked field handler. | ||
* | ||
* @param $handler | ||
* @param $entity | ||
* @param $entity_type | ||
* @param $field | ||
* @return \Mockery\MockInterface | ||
*/ | ||
protected function getMockHandler($handler, $entity, $entity_type, $field) | ||
{ | ||
$mock = m::mock(sprintf('Drupal\Driver\Fields\Drupal7\%s', $handler)); | ||
$mock->makePartial(); | ||
$mock->shouldReceive('getFieldInfo')->andReturn($field); | ||
$mock->shouldReceive('getEntityLanguage')->andReturn('en'); | ||
$mock->__construct($entity, $entity_type, $field); | ||
return $mock; | ||
} | ||
|
||
/** | ||
* Simulate __call() since mocked handlers will not run through magic methods. | ||
* | ||
* @param $values | ||
* @return array | ||
*/ | ||
protected function values($values) | ||
{ | ||
return (array) $values; | ||
} | ||
|
||
} |