Last active
May 24, 2018 15:54
-
-
Save mgldev/91386cc6e3d65fc239de60dc1bc46114 to your computer and use it in GitHub Desktop.
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
<?php | |
class NameValuePair | |
{ | |
/** @var string */ | |
private $name; | |
/** @var mixed */ | |
private $value; | |
/** | |
* NameValuePair constructor. | |
* | |
* @param string $name | |
* @param mixed $value | |
*/ | |
public function __construct($name, $value) | |
{ | |
$this->name = $name; | |
$this->value = $value; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
} | |
/** | |
* Class ASortObjectTest | |
* | |
* bin/phpunit -c app/phpunit.xml --stderr --filter ASortObjectTest | |
*/ | |
class ASortObjectTest extends PHPUnit_Framework_TestCase | |
{ | |
/** @var NameValuePair[] */ | |
private $nameValuePairs; | |
/** @var array */ | |
const EXPECTED_SORTED_VALUES = [ | |
'a', | |
'b', | |
'c', | |
'd', | |
'e', | |
'f', | |
'g', | |
'h', | |
'i', | |
'j', | |
'k', | |
'l', | |
'm', | |
'n', | |
'o', | |
'p', | |
'q', | |
'r', | |
's' | |
]; | |
public function setUp() | |
{ | |
$this->nameValuePairs = [ | |
new NameValuePair('l', 'l'), | |
new NameValuePair('b', 'b'), | |
new NameValuePair('k', 'k'), | |
new NameValuePair('c', 'c'), | |
new NameValuePair('o', 'o'), | |
new NameValuePair('e', 'e'), | |
new NameValuePair('m', 'm'), | |
new NameValuePair('q', 'q'), | |
new NameValuePair('f', 'f'), | |
new NameValuePair('g', 'g'), | |
new NameValuePair('p', 'p'), | |
new NameValuePair('d', 'd'), | |
new NameValuePair('h', 'h'), | |
new NameValuePair('i', 'i'), | |
new NameValuePair('j', 'j'), | |
new NameValuePair('s', 's'), | |
new NameValuePair('n', 'n'), | |
new NameValuePair('r', 'r'), | |
new NameValuePair('a', 'a'), | |
]; | |
} | |
public function testAsortOnNameValuePairInstances() | |
{ | |
asort($this->nameValuePairs); | |
$sortedValues = array_map(function (NameValuePair $nameValuePair) { | |
return $nameValuePair->getValue(); | |
}, $this->nameValuePairs); | |
$this->assertSame( | |
array_values(self::EXPECTED_SORTED_VALUES), | |
array_values($sortedValues) | |
); | |
} | |
public function testUasortOnNameValuePairInstances() | |
{ | |
uasort($this->nameValuePairs, function (NameValuePair $left, NameValuePair $right) { | |
return strcmp($left->getName(), $right->getName()); | |
}); | |
$sortedValues = array_map(function (NameValuePair $nameValuePair) { | |
return $nameValuePair->getValue(); | |
}, $this->nameValuePairs); | |
$this->assertSame( | |
array_values(self::EXPECTED_SORTED_VALUES), | |
array_values($sortedValues) | |
); | |
} | |
} |
- Can't find any documentation online about how PHP treats objects within in an array when performing an
asort
- Would have thought the default behaviour would be something along the lines of
spl_object_hash
and a sort on the hashes - Appears to sort using the
$name
property of theNameValuePair
instance, not the$value
- Is PHP doing something like
get_object_vars()
to get the object as an array and then sorting on that?get_object_vars
would return an empty array on private properties though - How is PHP getting to the
$name
attribute? Why is it picking the $name attribute? - Is something else at play here?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 2.75 seconds, Memory: 146.00MB
OK (2 tests, 2 assertions)