Skip to content

Commit

Permalink
Merge pull request #5 from transprime-research/feature/allow-older-ph…
Browse files Browse the repository at this point in the history
…p-on-piper

Feature/allow older php on piper
  • Loading branch information
omitobi authored May 3, 2020
2 parents 9da5f54 + e5fef5e commit c6df766
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php

php:
- 7.2
- 7.3
- 7.4

before_script:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Set master image
FROM php:7.4-fpm-alpine
FROM php:7.2-fpm-alpine

# Copy composer.lock and composer.json
COPY composer.json /var/www/html/
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ PHP Pipe function execution with values from initial call like F#

- `composer require transprime-research/piper`

## Requirement
Minimum Requirement
- PHP 7.2 +
- Composer

## Quick Usage

Let us take an array and do the following:
Expand Down Expand Up @@ -60,6 +65,7 @@ $arr = array_intersect($arr, [0 => 'ADE']);

//$arr is ['ADE']
```
> PS: You can still use the old `function() { return v; }`, `fn()` is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php
## Other Usages

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
}
],
"require": {
"php": ">=7.4"
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "~8.0"
"phpunit/phpunit": ">=6.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/Piper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class Piper
{
private array $piped = [];
private $piped = [];

private array $extraParameters = [];
private $extraParameters = [];

public function __invoke(callable $action = null)
{
Expand Down
121 changes: 90 additions & 31 deletions tests/PiperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function testPipeToUp()
$piper = new Piper();

$result = $piper->pipe('name')
->to(fn($name) => strtoupper($name))
->to(function ($name) {
return strtoupper($name);
})
->up();

$this->assertSame('NAME', $result);
Expand All @@ -26,33 +28,53 @@ public function testPipeToUp()
public function testClosureCanBeRunOnPipedResult()
{
piper('name')
->to(fn($name) => strtoupper($name))
->up(fn($result) => $this->assertSame('NAME', $result));
->to(function ($name) {
return strtoupper($name);
})
->up(function ($result) {
return $this->assertSame('NAME', $result);
});
}

public function testManyPipeTo()
{
piper('NAME')
->to(fn($name) => strtolower($name))
->to(fn($name) => ucfirst($name))
->up(fn($result) => $this->assertSame('Name', $result));
->to(function ($name) {
return strtolower($name);
})
->to(function ($name) {
return ucfirst($name);
})
->up(function ($result) {
return $this->assertSame('Name', $result);
});
}

public function testPipeAPipedResult()
{
$result = piper('NAME')
->to(fn($name) => strtolower($name))
->to(fn($name) => ucfirst($name))
->up(fn($result) => \piper($result)->to(fn() => [$result])->up());
->to(function ($name) {
return strtolower($name);
})
->to(function ($name) {
return ucfirst($name);
})
->up(function ($result) {
return \piper($result)->to(function () use ($result) {
return [$result];
})->up();
});

$this->assertSame(['Name'], $result);
}

public function testPiperAcceptsClosure()
{
$result = piper(fn() => 'NAME')
->to(fn($name) => strtolower($name))
->up();
$result = piper(function () {
return 'NAME';
})->to(function ($name) {
return strtolower($name);
})->up();

$this->assertSame('name', $result);
}
Expand All @@ -62,9 +84,13 @@ public function testPipeMethodCannotBeCalledMoreThanOnce()
$this->expectException(PiperException::class);
$this->expectExceptionMessage('on() must be called only once');

piper(fn() => 'NAME')
piper(function () {
return 'NAME';
})
->pipe('1')
->to(fn($name) => strtolower($name))
->to(function ($name) {
return strtolower($name);
})
->up();
}

Expand All @@ -73,49 +99,74 @@ public function testPipeAndToMethodMustBeCalledBeforeUp()
$this->expectException(PiperException::class);
$this->expectExceptionMessage('on() must be called and to() at least once');

piper(fn() => 'NAME')
->up();
piper(function () {
return 'NAME';
})->up();
}

public function testPipeMethodAcceptsCallable()
{
// test global method
$result = piper('NAME', 'strtolower')
->to(fn($name) => ucfirst($name))
->to(function ($name) {
return ucfirst($name);
})
->up();

$this->assertSame('Name', $result);

// test class method
$result2 = piper('NAME', StrManipulator::class . '::strToLower')
->to(fn($name) => ucfirst($name))
->to(function ($name) {
return ucfirst($name);
})
->up();

$this->assertSame('Name', $result2);

// test array class and method
$result2 = piper('NAME', [StrManipulator::class, 'strToLower'])
->to(fn($name) => ucfirst($name))
->up();
->to(function ($name) {
return ucfirst($name);
})->up();

$this->assertSame('Name', $result2);

// test class object
$result2 = piper('NAME', new StrManipulator())
->to(fn($name) => ucfirst($name))
->up();
->to(function ($name) {
return ucfirst($name);
})->up();

$this->assertSame('Name', $result2);
}

public function testFunctionCanBeRetrievedAtTheNextPipe()
{
piper(fn() => fn() => fn() => fn() => 'ade')
->to(fn($fn) => $fn()) //Executes the first function
->to(fn($fn) => $fn()) //Executes the second function
->to(fn($fn) => $fn()) //Executes the third function
->to(fn($name) => ucfirst($name)) //Executes the fourth function
->up(fn($result) => $this->assertSame('Ade', $result)); //manipulates the result
piper(function () {
return function () {
return function () {
return function () {
return 'ade';
};
};
};
})
->to(function ($fn) {
return $fn();
}) //Executes the first function
->to(function ($fn) {
return $fn();
}) //Executes the second function
->to(function ($fn) {
return $fn();
}) //Executes the third function
->to(function ($name) {
return ucfirst($name);
}) //Executes the fourth function
->up(function ($result) {
$this->assertSame('Ade', $result);
}); //manipulates the result
}

public function testAcceptingFunctionAndParametersInToMethod()
Expand All @@ -125,7 +176,11 @@ public function testAcceptingFunctionAndParametersInToMethod()
$this->assertSame(
10,
piper(['ade', 1])
->to(fn($data) => array_filter($data, static fn($val): bool => is_int($val)))
->to(function ($data) {
return array_filter($data, static function ($val): bool {
return is_int($val);
});
})
->to('array_merge', [2, 3, 4])
->to('array_values')
->to('array_sum')
Expand All @@ -137,15 +192,19 @@ public function testAcceptingFunctionAndParametersInToMethod()
piper(['name' => 'ade', 'hobby' => 'coding'])
->to('array_flip')
->to('array_keys')
->to('array_map', fn($val) => strtoupper($val))
->to('array_map', function ($val) {
return strtoupper($val);
})
->to('array_intersect', [0 => 'ADE'])()
);
}

public function testMakePiperInvokableWithoutCallingUpMethod()
{
$result = piper('name')
->to(fn($name) => ucfirst($name))();
->to(function ($name) {
return ucfirst($name);
})();

$this->assertSame('Name', $result);
}
Expand Down

0 comments on commit c6df766

Please sign in to comment.