Skip to content

Commit

Permalink
Add support for snake_care properties in fixtures. (nelmio#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidde Boomsma authored and theofidry committed May 13, 2016
1 parent 981191f commit b17831b
Show file tree
Hide file tree
Showing 16 changed files with 587 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.x (WIP)

* Added support for snake_case properties (#323)

### 2.1.2 (2015-12-10)

* Bug fix - private properties were populatable directly on the instance, but not private properties of parent classes. Although this is an antipattern, if we're allowing it for the instance we should allow it up the chain.
Expand Down
35 changes: 27 additions & 8 deletions src/Nelmio/Alice/Instances/Populator/Methods/Direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,53 @@ public function __construct(TypeHintChecker $typeHintChecker)
*/
public function canSet(Fixture $fixture, $object, $property, $value)
{
return method_exists($object, $this->setterFor($property));
return method_exists($object, $this->getPropertySetter($object, $property));
}

/**
* {@inheritDoc}
*/
public function set(Fixture $fixture, $object, $property, $value)
{
$setter = $this->setterFor($property);
$setter = $this->getPropertySetter($object, $property);
$value = $this->typeHintChecker->check($object, $setter, $value);

if (!is_callable([$object, $setter])) {
// Protected or private method
$refl = new \ReflectionMethod($object, $setter);
$refl->setAccessible(true);
$refl->invoke($object, $value);
} else {
$object->{$setter}($value);

return;
}

$object->{$setter}($value);
}

/**
* return the name of the setter for a given property
* Returns the name of the setter for a given property.
*
* @param object|string $object
* @param string $property
*
* @param string $property
* @return string
*/
private function setterFor($property)
private function getPropertySetter($object, $property)
{
return "set{$property}";
$normalizedProperty = str_replace('_', '', $property);
$setters = [
"set{$normalizedProperty}" => true,
"set{$property}" => true,
"set_{$property}" => true,
"set_{$normalizedProperty}" => true,
];

foreach ($setters as $setter => $void) {
if (method_exists($object, $setter)) {
return $setter;
}
}

return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
interface MethodInterface
{
/**
* returns true if the method is able to set the property to the value on the object described by the given fixture
* Returns true if the method is able to set the property to the value on the object described by the given fixture.
*
* @param Fixture $fixture
* @param mixed $object
* @param string $property
* @param mixed $value
*
* @return boolean
*/
public function canSet(Fixture $fixture, $object, $property, $value);

/**
* sets the property to the value on the object described by the given fixture
* Sets the property to the value on the object described by the given fixture.
*
* @param Fixture $fixture
* @param mixed $object
Expand Down
17 changes: 17 additions & 0 deletions tests/Nelmio/Alice/Fixtures/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ public function testLoadAssignsDataToNonPublicSetters()
$this->assertEquals('group', $group->getSortName());
}

public function testSnakeCaseProperty()
{
$res = $this->loadData([
self::USER => [
'user0' => [
'familyName' => 'Wonderland',
'display_name' => 'Hatter',
],
],
]);
/** @var User $user */
$user = $res['user0'];

$this->assertEquals('Wonderland', $user->family_name);
$this->assertEquals('Mad Hatter', $user->display_name);
}

public function testLoadAssignsDataToMagicCall()
{
$res = $this->loadData([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class CompositeCamelCaseDummy
{
/** @var string */
public $fullName;

public function setFullName($fullName)
{
$this->fullName = $fullName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class CompositeMixedCaseDummy
{
/** @var string */
public $fullname;

public function setFullname($fullname)
{
$this->fullname = $fullname;
}

public function setfull_name($full_name)
{
// Do not set anything
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class CompositeSnakeCase1Dummy
{
/** @var string */
public $full_name;

public function set_full_name($full_name)
{
$this->full_name = $full_name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class CompositeSnakeCase2Dummy
{
/** @var string */
public $full_name;

public function setfull_name($full_name)
{
$this->full_name = $full_name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class CompositeSnakeCase3Dummy
{
/** @var string */
public $full_name;

public function set_fullname($full_name)
{
$this->full_name = $full_name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class PrivateDummy
{
/** @var string */
public $name;

private function setName($name)
{
$this->name = $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class ProtectedDummy
{
/** @var string */
public $name;

public function setName($name)
{
$this->name = $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class PublicDummy
{
/** @var string */
public $name;

public function setName($name)
{
$this->name = $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class SimpleCamelCaseDummy
{
/** @var string */
public $name;

public function setName($name)
{
$this->name = $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Instances\Populator\Fixtures\Direct;

class SimpleSnakeCaseDummy
{
/** @var string */
public $name;

public function set_name($name)
{
$this->name = $name;
}
}
Loading

0 comments on commit b17831b

Please sign in to comment.