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 11, 2019
1 parent f2cf7d7 commit 36b9620
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<?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
{
require_once 'accumulate.php';
$files = [
'../src/accumulate.php',
'Str.php',
'StrSplitter.php',
'Is.php',
];

foreach ($files as $file) {
require_once __DIR__ . '/' . $file;
}
}

public function testAccumulateEmpty() : void
Expand Down Expand Up @@ -54,15 +68,15 @@ public function testAccumulateConstants() : 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;
});
})
);
Expand All @@ -77,40 +91,16 @@ public function testAccumulateUsingBuiltInFunction() : 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
{
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSpliter()));
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSplitter()));
}

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;
}
}
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);
}
}

0 comments on commit 36b9620

Please sign in to comment.