Skip to content

Commit

Permalink
Merge pull request #24 from ademarco/drupal-7-array-casting
Browse files Browse the repository at this point in the history
Drupal 7: Implement values array casting in __call() magic method

Signed-off-by: Jonathan Hedstrom <[email protected]>
  • Loading branch information
jhedstrom committed Mar 23, 2015
1 parent 60ebeed commit 38d7c91
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 17 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
},
"require-dev": {
"phpspec/phpspec": "~2.0",
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "~4.0",
"mockery/mockery": "dev-master"
},
"autoload": {
"psr-0": {
"Drupal\\Component": "src/",
"Drupal\\Driver": "src/"
"Drupal\\Driver": "src/",
"Drupal\\Tests\\Driver" : "tests/"
}
}
}
50 changes: 42 additions & 8 deletions src/Drupal/Driver/Fields/Drupal7/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
use Drupal\Driver\Fields\FieldHandlerInterface;

abstract class AbstractHandler implements FieldHandlerInterface {
/**
* @var string
*/
protected $language = LANGUAGE_NONE;

/**
* @var
* @var string
*/
protected $language = NULL;
protected $entity = NULL;
protected $entity_type = NULL;
protected $field_name = NULL;
protected $field_info = array();

/**
Expand All @@ -28,8 +28,42 @@ abstract class AbstractHandler implements FieldHandlerInterface {
* @param $field_name
* @return mixed
*/
public function __construct($entity, $entity_type, $field_name) {
$this->field_info = field_info_field($field_name);
$this->language = field_is_translatable($entity_type, $this->field_info) ? entity_language($entity_type, $entity) : LANGUAGE_NONE;
public function __construct(\stdClass $entity, $entity_type, $field_name) {
$this->entity = $entity;
$this->entity_type = $entity_type;
$this->field_name = $field_name;
$this->field_info = $this->getFieldInfo();
$this->language = $this->getEntityLanguage();
}

/**
* @param $method
* @param $args
* @return mixed
*/
public function __call($method, $args) {
if ($method == 'expand') {
$args['values'] = (array) $args['values'];
}
return call_user_func_array(array($this, $method), $args);
}

/**
* @return bool|mixed|void
*/
public function getFieldInfo() {
return field_info_field($this->field_name);
}

/**
* @return null|string
*/
public function getEntityLanguage() {
if (field_is_translatable($this->entity_type, $this->field_info)) {
return entity_language($this->entity_type, $this->entity);
}
else {
return LANGUAGE_NONE;
}
}
}
4 changes: 2 additions & 2 deletions src/Drupal/Driver/Fields/Drupal7/DatetimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class DatetimeHandler extends AbstractHandler {
public function expand($values) {
$return = array();
if (isset($this->field_info['columns']['value2'])) {
foreach ((array)$values as $value) {
foreach ($values as $value) {
$return[$this->language][] = array(
'value' => $value[0],
'value2' => $value[1],
);
}
}
else {
foreach ((array)$values as $value) {
foreach ($values as $value) {
$return[$this->language][] = array('value' => $value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drupal/Driver/Fields/Drupal7/DefaultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DefaultHandler extends AbstractHandler {
*/
public function expand($values) {
$return = array();
foreach ((array)$values as $value) {
foreach ($values as $value) {
$return[$this->language][] = array('value' => $value);
}
return $return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function expand($values) {
}

$return = array();
foreach ((array)$values as $value) {
foreach ($values as $value) {
$target_id = db_select($entity_info['base table'], 't')
->fields('t', array($entity_info['entity keys']['id']))
->condition('t.' . $entity_info['entity keys']['label'], $value)
Expand Down
2 changes: 1 addition & 1 deletion src/Drupal/Driver/Fields/Drupal7/LinkFieldHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LinkFieldHandler extends AbstractHandler {
*/
public function expand($values) {
$return = array();
foreach ((array)$values as $value) {
foreach ($values as $value) {
$return[$this->language][] = array(
'title' => $value[0],
'url' => $value[1],
Expand Down
2 changes: 1 addition & 1 deletion src/Drupal/Driver/Fields/Drupal7/ListTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ListTextHandler extends AbstractHandler {
public function expand($values) {
$return = array();
$allowed_values = array_flip($this->field_info['settings']['allowed_values']);
foreach ((array)$values as $value) {
foreach ($values as $value) {
$return[$this->language][] = array('value' => $allowed_values[$value]);
}
return $return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TaxonomyTermReferenceHandler extends AbstractHandler {
*/
public function expand($values) {
$return = array();
foreach ((array)$values as $name) {
foreach ($values as $name) {
$terms = taxonomy_get_term_by_name($name);
if (!$terms) {
throw new \Exception(sprintf("No term '%s' exists.", $name));
Expand Down
87 changes: 87 additions & 0 deletions tests/Drupal/Tests/Driver/Drupal7FieldHandlerTest.php
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.
53 changes: 53 additions & 0 deletions tests/Drupal/Tests/Driver/FieldHandlerAbstractTest.php
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;
}

}

0 comments on commit 38d7c91

Please sign in to comment.