This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotfix/zendframework/zendframework#6869-stdlib-hydrator…
…-should-only-hydrate-public-properties' into develop Close zendframework/zendframework#6869 Forward port zendframework/zendframework#6869
- Loading branch information
Showing
4 changed files
with
239 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Stdlib\Hydrator; | ||
|
||
use Zend\Stdlib\Hydrator\ObjectProperty; | ||
use Zend\Stdlib\Exception\BadMethodCallException; | ||
use ZendTest\Stdlib\TestAsset\ClassWithPublicStaticProperties; | ||
use ZendTest\Stdlib\TestAsset\ObjectProperty as ObjectPropertyTestAsset; | ||
|
||
/** | ||
* Unit tests for {@see \Zend\Stdlib\Hydrator\ObjectProperty} | ||
* | ||
* @covers \Zend\Stdlib\Hydrator\ObjectProperty | ||
* @group Zend_Stdlib | ||
*/ | ||
class ObjectPropertyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ObjectProperty | ||
*/ | ||
private $hydrator; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->hydrator = new ObjectProperty(); | ||
} | ||
|
||
/** | ||
* Verify that we get an exception when trying to extract on a non-object | ||
*/ | ||
public function testHydratorExtractThrowsExceptionOnNonObjectParameter() | ||
{ | ||
$this->setExpectedException('BadMethodCallException'); | ||
$this->hydrator->extract('thisIsNotAnObject'); | ||
} | ||
|
||
/** | ||
* Verify that we get an exception when trying to hydrate a non-object | ||
*/ | ||
public function testHydratorHydrateThrowsExceptionOnNonObjectParameter() | ||
{ | ||
$this->setExpectedException('BadMethodCallException'); | ||
$this->hydrator->hydrate(array('some' => 'data'), 'thisIsNotAnObject'); | ||
} | ||
|
||
/** | ||
* Verifies that the hydrator can extract from property of stdClass objects | ||
*/ | ||
public function testCanExtractFromStdClass() | ||
{ | ||
$object = new \stdClass(); | ||
$object->foo = 'bar'; | ||
|
||
$this->assertSame(array('foo' => 'bar'), $this->hydrator->extract($object)); | ||
} | ||
|
||
/** | ||
* Verifies that the extraction process works on classes that aren't stdClass | ||
*/ | ||
public function testCanExtractFromGenericClass() | ||
{ | ||
$this->assertSame( | ||
array( | ||
'foo' => 'bar', | ||
'bar' => 'foo', | ||
'blubb' => 'baz', | ||
'quo' => 'blubb' | ||
), | ||
$this->hydrator->extract(new ObjectPropertyTestAsset()) | ||
); | ||
} | ||
|
||
/** | ||
* Verify hydration of {@see \stdClass} | ||
*/ | ||
public function testCanHydrateStdClass() | ||
{ | ||
$object = new \stdClass(); | ||
$object->foo = 'bar'; | ||
|
||
$object = $this->hydrator->hydrate(array('foo' => 'baz'), $object); | ||
|
||
$this->assertEquals('baz', $object->foo); | ||
} | ||
|
||
/** | ||
* Verify that new properties are created if the object is stdClass | ||
*/ | ||
public function testCanHydrateAdditionalPropertiesToStdClass() | ||
{ | ||
$object = new \stdClass(); | ||
$object->foo = 'bar'; | ||
|
||
$object = $this->hydrator->hydrate(array('foo' => 'baz', 'bar' => 'baz'), $object); | ||
|
||
$this->assertEquals('baz', $object->foo); | ||
$this->assertObjectHasAttribute('bar', $object); | ||
$this->assertAttributeSame('baz', 'bar', $object); | ||
} | ||
|
||
/** | ||
* Verify that it can hydrate our class public properties | ||
*/ | ||
public function testCanHydrateGenericClassPublicProperties() | ||
{ | ||
$object = $this->hydrator->hydrate( | ||
array( | ||
'foo' => 'foo', | ||
'bar' => 'bar', | ||
'blubb' => 'blubb', | ||
'quo' => 'quo', | ||
'quin' => 'quin' | ||
), | ||
new ObjectPropertyTestAsset() | ||
); | ||
|
||
$this->assertAttributeSame('foo', 'foo', $object); | ||
$this->assertAttributeSame('bar', 'bar', $object); | ||
$this->assertAttributeSame('blubb', 'blubb', $object); | ||
$this->assertAttributeSame('quo', 'quo', $object); | ||
$this->assertAttributeNotSame('quin', 'quin', $object); | ||
} | ||
|
||
/** | ||
* Verify that it can hydrate new properties on generic classes | ||
*/ | ||
public function testCanHydrateGenericClassNonExistingProperties() | ||
{ | ||
$object = $this->hydrator->hydrate(array('newProperty' => 'newPropertyValue'), new ObjectPropertyTestAsset()); | ||
|
||
$this->assertAttributeSame('newPropertyValue', 'newProperty', $object); | ||
} | ||
|
||
/** | ||
* Verify that hydration is skipped for class properties (it is an object hydrator after all) | ||
*/ | ||
public function testSkipsPublicStaticClassPropertiesHydration() | ||
{ | ||
$this->hydrator->hydrate( | ||
array('foo' => '1', 'bar' => '2', 'baz' => '3'), | ||
new ClassWithPublicStaticProperties() | ||
); | ||
|
||
$this->assertSame('foo', ClassWithPublicStaticProperties::$foo); | ||
$this->assertSame('bar', ClassWithPublicStaticProperties::$bar); | ||
$this->assertSame('baz', ClassWithPublicStaticProperties::$baz); | ||
} | ||
|
||
/** | ||
* Verify that extraction is skipped for class properties (it is an object hydrator after all) | ||
*/ | ||
public function testSkipsPublicStaticClassPropertiesExtraction() | ||
{ | ||
$this->assertEmpty($this->hydrator->extract(new ClassWithPublicStaticProperties())); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Stdlib\TestAsset; | ||
|
||
class ClassWithPublicStaticProperties | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public static $foo = 'foo'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public static $bar = 'bar'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public static $baz = 'baz'; | ||
} |
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