Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Nov 30, 2019
1 parent e72ad55 commit 765797a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 74 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
{
"name": "Johannes M. Schmitt",
"email": "[email protected]"
},
{
"name": "Graham Campbell",
"email": "[email protected]"
}
],
"require": {
"php": "^5.5.9|^7.0"
"php": "^5.5.9 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.7|^5.0"
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 7 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
verbose="true"
>
<testsuites>
<testsuite name="PhpOption Type Test Suite">
<directory>./tests/PhpOption/</directory>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>performance</group>
Expand Down
7 changes: 4 additions & 3 deletions tests/PhpOption/Tests/EnsureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
use PHPUnit\Framework\TestCase;

/**
* Tests for Option::ensure() method.
*
* @covers \PhpOption\Option::ensure
* @covers Option::ensure
*/
class EnsureTest extends \PHPUnit_Framework_TestCase
class EnsureTest extends TestCase
{
protected function ensure($value, $noneValue = null)
{
$option = Option::ensure($value, $noneValue);
$this->assertInstanceOf('PhpOption\Option', $option);
$this->assertInstanceOf(Option::class, $option);

return $option;
}
Expand Down
44 changes: 24 additions & 20 deletions tests/PhpOption/Tests/LazyOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
namespace PhpOption\Tests;

use PhpOption\LazyOption;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
use PHPUnit\Framework\TestCase;

class LazyOptionTest extends \PHPUnit_Framework_TestCase
class LazyOptionTest extends TestCase
{
private $subject;

Expand All @@ -18,13 +22,13 @@ public function setUp()

public function testGetWithArgumentsAndConstructor()
{
$some = \PhpOption\LazyOption::create([$this->subject, 'execute'], ['foo']);
$some = LazyOption::create([$this->subject, 'execute'], ['foo']);

$this->subject
->expects($this->once())
->method('execute')
->with('foo')
->will($this->returnValue(\PhpOption\Some::create('foo')));
->will($this->returnValue(Some::create('foo')));

$this->assertEquals('foo', $some->get());
$this->assertEquals('foo', $some->getOrElse(null));
Expand All @@ -35,13 +39,13 @@ public function testGetWithArgumentsAndConstructor()

public function testGetWithArgumentsAndCreate()
{
$some = new \PhpOption\LazyOption([$this->subject, 'execute'], ['foo']);
$some = new LazyOption([$this->subject, 'execute'], ['foo']);

$this->subject
->expects($this->once())
->method('execute')
->with('foo')
->will($this->returnValue(\PhpOption\Some::create('foo')));
->will($this->returnValue(Some::create('foo')));

$this->assertEquals('foo', $some->get());
$this->assertEquals('foo', $some->getOrElse(null));
Expand All @@ -52,12 +56,12 @@ public function testGetWithArgumentsAndCreate()

public function testGetWithoutArgumentsAndConstructor()
{
$some = new \PhpOption\LazyOption([$this->subject, 'execute']);
$some = new LazyOption([$this->subject, 'execute']);

$this->subject
->expects($this->once())
->method('execute')
->will($this->returnValue(\PhpOption\Some::create('foo')));
->will($this->returnValue(Some::create('foo')));

$this->assertEquals('foo', $some->get());
$this->assertEquals('foo', $some->getOrElse(null));
Expand All @@ -68,12 +72,12 @@ public function testGetWithoutArgumentsAndConstructor()

public function testGetWithoutArgumentsAndCreate()
{
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);
$option = LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
->method('execute')
->will($this->returnValue(\PhpOption\Some::create('foo')));
->will($this->returnValue(Some::create('foo')));

$this->assertTrue($option->isDefined());
$this->assertFalse($option->isEmpty());
Expand All @@ -89,12 +93,12 @@ public function testGetWithoutArgumentsAndCreate()
*/
public function testCallbackReturnsNull()
{
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);
$option = LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
->method('execute')
->will($this->returnValue(\PhpOption\None::create()));
->will($this->returnValue(None::create()));

$this->assertFalse($option->isDefined());
$this->assertTrue($option->isEmpty());
Expand All @@ -112,7 +116,7 @@ public function testCallbackReturnsNull()
*/
public function testExceptionIsThrownIfCallbackReturnsNonOption()
{
$option = \PhpOption\LazyOption::create([$this->subject, 'execute']);
$option = LazyOption::create([$this->subject, 'execute']);

$this->subject
->expects($this->once())
Expand All @@ -128,7 +132,7 @@ public function testExceptionIsThrownIfCallbackReturnsNonOption()
*/
public function testInvalidCallbackAndConstructor()
{
new \PhpOption\LazyOption('invalidCallback');
new LazyOption('invalidCallback');
}

/**
Expand All @@ -137,7 +141,7 @@ public function testInvalidCallbackAndConstructor()
*/
public function testInvalidCallbackAndCreate()
{
\PhpOption\LazyOption::create('invalidCallback');
LazyOption::create('invalidCallback');
}

public function testifDefined()
Expand All @@ -155,7 +159,7 @@ public function testForAll()
{
$called = false;
$self = $this;
$this->assertInstanceOf('PhpOption\Some', LazyOption::fromValue('foo')->forAll(function ($v) use (&$called, $self) {
$this->assertInstanceOf(Some::class, LazyOption::fromValue('foo')->forAll(function ($v) use (&$called, $self) {
$called = true;
$self->assertEquals('foo', $v);
}));
Expand All @@ -164,20 +168,20 @@ public function testForAll()

public function testOrElse()
{
$some = \PhpOption\Some::create('foo');
$lazy = \PhpOption\LazyOption::create(function () use ($some) {
$some = Some::create('foo');
$lazy = LazyOption::create(function () use ($some) {
return $some;
});
$this->assertSame($some, $lazy->orElse(\PhpOption\None::create()));
$this->assertSame($some, $lazy->orElse(\PhpOption\Some::create('bar')));
$this->assertSame($some, $lazy->orElse(None::create()));
$this->assertSame($some, $lazy->orElse(Some::create('bar')));
}

public function testFoldLeftRight()
{
$callback = function () {
};

$option = $this->getMockForAbstractClass('PhpOption\Option');
$option = $this->getMockForAbstractClass(Option::class);
$option->expects($this->once())
->method('foldLeft')
->with(5, $callback)
Expand Down
18 changes: 10 additions & 8 deletions tests/PhpOption/Tests/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace PhpOption\Tests;

use PhpOption\None;
use PhpOption\Some;
use PHPUnit\Framework\TestCase;

class NoneTest extends \PHPUnit_Framework_TestCase
class NoneTest extends TestCase
{
private $none;

Expand All @@ -13,19 +15,19 @@ class NoneTest extends \PHPUnit_Framework_TestCase
*/
public function testGet()
{
$none = \PhpOption\None::create();
$none = None::create();
$none->get();
}

public function testGetOrElse()
{
$none = \PhpOption\None::create();
$none = None::create();
$this->assertEquals('foo', $none->getOrElse('foo'));
}

public function testGetOrCall()
{
$none = \PhpOption\None::create();
$none = None::create();
$this->assertEquals('foo', $none->getOrCall(function () {
return 'foo';
}));
Expand All @@ -42,14 +44,14 @@ public function testGetOrThrow()

public function testIsEmpty()
{
$none = \PhpOption\None::create();
$none = None::create();
$this->assertTrue($none->isEmpty());
}

public function testOrElse()
{
$option = \PhpOption\Some::create('foo');
$this->assertSame($option, \PhpOption\None::create()->orElse($option));
$option = Some::create('foo');
$this->assertSame($option, None::create()->orElse($option));
}

public function testifDefined()
Expand Down Expand Up @@ -106,7 +108,7 @@ public function testReject()

public function testForeach()
{
$none = \PhpOption\None::create();
$none = None::create();

$called = 0;
foreach ($none as $value) {
Expand Down
44 changes: 23 additions & 21 deletions tests/PhpOption/Tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

namespace PhpOption\Tests;

use PhpOption\LazyOption;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
use PHPUnit\Framework\TestCase;

class OptionTest extends \PHPUnit_Framework_TestCase
class OptionTest extends TestCase
{
public function testfromValueWithDefaultNoneValue()
{
$this->assertInstanceOf('PhpOption\None', \PhpOption\Option::fromValue(null));
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue('value'));
$this->assertInstanceOf(None::class, Option::fromValue(null));
$this->assertInstanceOf(Some::class, Option::fromValue('value'));
}

public function testFromValueWithFalseNoneValue()
{
$this->assertInstanceOf('PhpOption\None', \PhpOption\Option::fromValue(false, false));
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue('value', false));
$this->assertInstanceOf('PhpOption\Some', \PhpOption\Option::fromValue(null, false));
$this->assertInstanceOf(None::class, Option::fromValue(false, false));
$this->assertInstanceOf(Some::class, Option::fromValue('value', false));
$this->assertInstanceOf(Some::class, Option::fromValue(null, false));
}

public function testFromArraysValue()
Expand All @@ -41,25 +43,25 @@ public function testFromReturn()
return 'foo';
};

$this->assertTrue(\PhpOption\Option::fromReturn($null)->isEmpty());
$this->assertFalse(\PhpOption\Option::fromReturn($false)->isEmpty());
$this->assertTrue(\PhpOption\Option::fromReturn($false, [], false)->isEmpty());
$this->assertTrue(\PhpOption\Option::fromReturn($some)->isDefined());
$this->assertFalse(\PhpOption\Option::fromReturn($some, [], 'foo')->isDefined());
$this->assertTrue(Option::fromReturn($null)->isEmpty());
$this->assertFalse(Option::fromReturn($false)->isEmpty());
$this->assertTrue(Option::fromReturn($false, [], false)->isEmpty());
$this->assertTrue(Option::fromReturn($some)->isDefined());
$this->assertFalse(Option::fromReturn($some, [], 'foo')->isDefined());
}

public function testOrElse()
{
$a = new \PhpOption\Some('a');
$b = new \PhpOption\Some('b');
$a = new Some('a');
$b = new Some('b');

$this->assertEquals('a', $a->orElse($b)->get());
}

public function testOrElseWithNoneAsFirst()
{
$a = \PhpOption\None::create();
$b = new \PhpOption\Some('b');
$a = None::create();
$b = new Some('b');

$this->assertEquals('b', $a->orElse($b)->get());
}
Expand All @@ -70,22 +72,22 @@ public function testOrElseWithLazyOptions()
throw new \LogicException('Should never be called.');
};

$a = new \PhpOption\Some('a');
$b = new \PhpOption\LazyOption($throws);
$a = new Some('a');
$b = new LazyOption($throws);

$this->assertEquals('a', $a->orElse($b)->get());
}

public function testOrElseWithMultipleAlternatives()
{
$throws = new \PhpOption\LazyOption(function () {
$throws = new LazyOption(function () {
throw new \LogicException('Should never be called.');
});
$returns = new \PhpOption\LazyOption(function () {
return new \PhpOption\Some('foo');
$returns = new LazyOption(function () {
return new Some('foo');
});

$a = \PhpOption\None::create();
$a = None::create();

$this->assertEquals('foo', $a->orElse($returns)->orElse($throws)->get());
}
Expand Down
Loading

0 comments on commit 765797a

Please sign in to comment.