Skip to content

Commit

Permalink
Update Accumulate to use namespaces
Browse files Browse the repository at this point in the history
Split test classes to their own files
  • Loading branch information
David Stockton committed Aug 10, 2019
1 parent f2cf7d7 commit 11b3ae0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
4 changes: 3 additions & 1 deletion exercises/accumulate/example.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

namespace Exercism\Accumulate;

/**
* Given the input array and operation returns an array
* containing the result of applying that operation
* to each element of the input array.
*
* @param array $input
* @param array $input
* @param callable $accumulator
*
* @return array
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Exercism\Accumulate;

function accumulate(array $input, callable $accumulator)
{
// YOUR CODE GOES HERE
Expand Down
11 changes: 11 additions & 0 deletions exercises/accumulate/test/Is.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class Is
{
public function truthy($value): bool
{
return $value ? true : false;
}
}
11 changes: 11 additions & 0 deletions exercises/accumulate/test/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class Str
{
public static function len($string): int
{
return strlen($string);
}
}
11 changes: 11 additions & 0 deletions exercises/accumulate/test/StrSplitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ExercismTest\Accumulate;

class StrSplitter
{
public function __invoke($value)
{
return str_split($value);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?php

class AccumulateTest extends PHPUnit\Framework\TestCase
namespace ExercismTest\Accumulate;

use PHPUnit\Framework\TestCase;
use function Exercism\Accumulate\accumulate;

class AccumulateTest extends TestCase
{
public static function setUpBeforeClass() : void
public static function setUpBeforeClass(): void
{
require_once 'accumulate.php';
$files = [
'/../src/accumulate.php',
'/Str.php',
'/StrSplitter.php',
'/Is.php',
];

foreach ($files as $file) {
require_once $file;
}
}

public function testAccumulateEmpty() : void
public function testAccumulateEmpty(): void
{
$accumulator = function ($value) {
return $value ** 2;
Expand All @@ -16,7 +30,7 @@ public function testAccumulateEmpty() : void
$this->assertEquals([], accumulate([], $accumulator));
}

public function testAccumulateSquares() : void
public function testAccumulateSquares(): void
{
$accumulator = function ($value) {
return $value ** 2;
Expand All @@ -25,7 +39,7 @@ public function testAccumulateSquares() : void
$this->assertEquals([1, 4, 9], accumulate([1, 2, 3], $accumulator));
}

public function testAccumulateUpperCases() : void
public function testAccumulateUpperCases(): void
{
$accumulator = function ($string) {
return mb_strtoupper($string);
Expand All @@ -34,7 +48,7 @@ public function testAccumulateUpperCases() : void
$this->assertEquals(['HELLO', 'WORLD!'], accumulate(['Hello', 'World!'], $accumulator));
}

public function testAccumulateReversedStrings() : void
public function testAccumulateReversedStrings(): void
{
$accumulator = function ($string) {
return strrev($string);
Expand All @@ -43,7 +57,7 @@ public function testAccumulateReversedStrings() : void
$this->assertEquals(['Hello', 'World!'], accumulate(['olleH', '!dlroW'], $accumulator));
}

public function testAccumulateConstants() : void
public function testAccumulateConstants(): void
{
$accumulator = function () {
return 1;
Expand All @@ -52,65 +66,41 @@ public function testAccumulateConstants() : void
$this->assertEquals([1, 1], accumulate(['Hello', 'World!'], $accumulator));
}

public function testAccumulateWithinAccumulate() : void
public function testAccumulateWithinAccumulate(): void
{
$chars = ['a', 'b', 'c'];
$digits = [1, 2, 3];
$chars = ['a', 'b', 'c'];
$digits = [1, 2, 3];
$expected = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']];

$this->assertEquals(
$expected,
accumulate($chars, function ($char) use ($digits) {
return accumulate($digits, function ($digit) use ($char) {
return $char.$digit;
return $char . $digit;
});
})
);
}

// Additional points for making the following tests pass

public function testAccumulateUsingBuiltInFunction() : void
public function testAccumulateUsingBuiltInFunction(): void
{
$this->assertEquals(['Hello', 'World!'], accumulate([" Hello\t", "\t World!\n "], 'trim'));
}

public function testAccumulateUsingStaticMethod() : void
public function testAccumulateUsingStaticMethod(): void
{
$this->assertEquals([5, 6], accumulate(['Hello', 'World!'], 'Str::len'));
$this->assertEquals([5, 6], accumulate(['Hello', 'World!'], 'ExercismTest\Accumulate\Str::len'));
}

public function testAccumulateUsingInvoke() : void
public function testAccumulateUsingInvoke(): void
{
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSpliter()));
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSplitter()));
}

public function testAccumulateUsingObjectAndArrayNotation() : void
public function testAccumulateUsingObjectAndArrayNotation(): void
{
$this->assertEquals([true, false, false], accumulate(['Yes', 0, []], [new Is(), 'truthy']));
}
}

class Str
{
public static function len($string) : int
{
return strlen($string);
}
}

class StrSpliter
{
public function __invoke($value)
{
return str_split($value);
}
}

class Is
{
public function truthy($value) : bool
{
return $value ? true : false;
}
}

0 comments on commit 11b3ae0

Please sign in to comment.