Last active
August 31, 2015 21:01
-
-
Save konrness/50e6e33859ccdb7e95e7 to your computer and use it in GitHub Desktop.
Nerdery PHP Challenge Yourselph - 017 An Array Problem
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 | |
/** | |
* Challenge Yourselph - 017 | |
* | |
* An Array Problem | |
* | |
* I have an array of values, keyed by numerical index. What I want is an associative array of those same values, but keyed by corresponding value. | |
* | |
* Test It: vendor/bin/phpunit ArrayCombineKeysTest.php | |
* | |
* @author Konr Ness <konr.ness@gmail.com> | |
*/ | |
/** | |
* Creates an array by using the input array's values for its key/values pairs | |
* | |
* @param array $array | |
* @return array | |
*/ | |
function array_combine_keys(array $array) { | |
return array_combine($array, $array); | |
} |
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 | |
require_once 'array_combine_keys.php'; | |
class ArrayCombineKeysTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testExample() | |
{ | |
$input = array('apples', 'oranges', 'bananas'); | |
$expectedOutput = array('apples' => 'apples', 'oranges' => 'oranges', 'bananas' => 'bananas'); | |
$actualOutput = array_combine_keys($input); | |
$this->assertEquals($expectedOutput, $actualOutput); | |
} | |
public function testDuplicateKeys() | |
{ | |
$input = array('apples', 'oranges', 'bananas', 'apples'); | |
$expectedOutput = array('apples' => 'apples', 'oranges' => 'oranges', 'bananas' => 'bananas'); | |
$actualOutput = array_combine_keys($input); | |
$this->assertEquals($expectedOutput, $actualOutput); | |
} | |
public function testNumericValues() | |
{ | |
$input = array(2, 3, 4); | |
$expectedOutput = array(2 => 2, 3 => 3, 4 => 4); | |
$actualOutput = array_combine_keys($input); | |
$this->assertEquals($expectedOutput, $actualOutput); | |
} | |
public function testEmptyArrayInput() | |
{ | |
$input = array(); | |
$expectedOutput = array(); | |
$actualOutput = array_combine_keys($input); | |
$this->assertEquals($expectedOutput, $actualOutput); | |
} | |
/** | |
* @expectedException PHPUnit_Framework_Error | |
*/ | |
public function testZeroArguments() | |
{ | |
array_combine_keys(); | |
} | |
/** | |
* @expectedException PHPUnit_Framework_Error | |
*/ | |
public function testScalarArgument() | |
{ | |
array_combine_keys(42); | |
} | |
/** | |
* @expectedException PHPUnit_Framework_Error | |
*/ | |
public function testStringArgument() | |
{ | |
array_combine_keys("foo"); | |
} | |
} | |
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
{ | |
"name": "kness/issue15", | |
"authors": [ | |
{ | |
"name": "Konr Ness", | |
"email": "kness@nerdery.com" | |
} | |
], | |
"require-dev": { | |
"phpunit/phpunit" : "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment