Skip to content

Commit

Permalink
Merge pull request #608 from theofidry/bugfix/deprecate-snake-accessors
Browse files Browse the repository at this point in the history
Deprecate usage of non PSR-1 compliant setters
  • Loading branch information
theofidry authored Nov 7, 2016
2 parents 99edefe + 5b50bbc commit b8362c6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/Nelmio/Alice/Instances/Populator/Methods/Direct.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function set(Fixture $fixture, $object, $property, $value)
$setter = $this->getPropertySetter($object, $property);
$value = $this->typeHintChecker->check($object, $setter, $value);

if (false !== strpos($setter, '_')) {
@trigger_error(
'Using a non PSR-2 compliant setter is deprecated since 2.3.0 and will be removed in 3.0.0.',
E_USER_DEPRECATED
);
}

if (!is_callable([$object, $setter])) {
// Protected or private method
$refl = new \ReflectionMethod($object, $setter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProtectedDummy
/** @var string */
public $name;

public function setName($name)
protected function setName($name)
{
$this->name = $name;
}
Expand Down
73 changes: 56 additions & 17 deletions tests/Nelmio/Alice/Instances/Populator/Methods/DirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function setUp()

/**
* @dataProvider provideProperties
* @group legacy
*/
public function testCanSet($property, $model, $expected)
{
Expand Down Expand Up @@ -97,16 +98,36 @@ public function testSetProperty($model, $property, $value, $expectedProperty)
self::assertEquals($value, $model->$expectedProperty);
}

/**
* @dataProvider provideLegacyModelsToSet
*/
public function testLegacySetProperty($model, $property, $value, $expectedProperty)
{
$this->direct->set($this->fixture, $model, $property, $value);

self::assertEquals($value, $model->$expectedProperty);
}

/**
* @group legacy
*/
public function testSetInaccessibleProperty()
public function testSetPropertyViaPrivateSetter()
{
$this->direct->set($this->fixture, $model = new PrivateDummy(), 'name', $value = 'John Doe');

self::assertEquals($value, $model->name);
}

/**
* @group legacy
*/
public function testSetPropertyViaProtectedSetter()
{
$this->direct->set($this->fixture, $model = new ProtectedDummy(), 'name', $value = 'John Doe');

self::assertEquals($value, $model->name);
}

public function provideProperties()
{
return [
Expand Down Expand Up @@ -184,9 +205,6 @@ public function provideProperties()
];
}

/**
* @return array<{string, string, mixed}>
*/
public function provideModelsToSet()
{
$value = 'John Doe';
Expand All @@ -198,12 +216,6 @@ public function provideModelsToSet()
$value,
'name',
],
'simple property with snake_case setter' => [
new SimpleSnakeCaseDummy(),
'name',
$value,
'name',
],

'composite camelCase property with camelCase setter' => [
new CompositeCamelCaseDummy(),
Expand All @@ -218,6 +230,40 @@ public function provideModelsToSet()
'fullName',
],

'composite camelCase property with mixed setter (BC preserved)' => [
new CompositeMixedCaseDummy(),
'fullName',
$value,
'fullname',
],
'composite snake_case property with mixed setter (BC preserved)' => [
new CompositeMixedCaseDummy(),
'full_name',
$value,
'fullname',
],

'public setter' => [
new PublicDummy(),
'name',
$value,
'name',
],
];
}

public function provideLegacyModelsToSet()
{
$value = 'John Doe';

return [
'simple property with snake_case setter' => [
new SimpleSnakeCaseDummy(),
'name',
$value,
'name',
],

'composite snake_case property with snake_case1 setter' => [
new CompositeSnakeCase1Dummy(),
'full_name',
Expand Down Expand Up @@ -264,13 +310,6 @@ public function provideModelsToSet()
$value,
'name',
],

'protected setter' => [
new ProtectedDummy(),
'name',
$value,
'name',
],
];
}
}

0 comments on commit b8362c6

Please sign in to comment.