1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Add test for invalid stream arguments

This commit is contained in:
Niklas Keller 2017-01-06 23:16:06 +01:00
parent 9ff73ac681
commit 559c64fdb8

View File

@ -11,64 +11,64 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
$results = [];
Loop::execute(function () use (&$results) {
$stream = Amp\stream([new Success(1), new Success(2), new Success(3)]);
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
});
$this->assertSame([1, 2, 3], $results);
}
public function testFailedPromises() {
$exception = new \Exception;
Loop::execute(function () use (&$reason, $exception) {
$stream = Amp\stream([new Failure($exception), new Failure($exception)]);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
public function testMixedPromises() {
$exception = new \Exception;
$results = [];
Loop::execute(function () use (&$results, &$reason, $exception) {
$stream = Amp\stream([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame(\range(1, 2), $results);
$this->assertSame($exception, $reason);
}
public function testPendingPromises() {
$results = [];
Loop::execute(function () use (&$results) {
$stream = Amp\stream([new Pause(30, 1), new Pause(10, 2), new Pause(20, 3), new Success(4)]);
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
});
$this->assertSame(\range(1, 4), $results);
}
public function testTraversable() {
$results = [];
Loop::execute(function () use (&$results) {
@ -77,14 +77,34 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
yield $value;
}
})();
$stream = Amp\stream($generator);
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
});
$this->assertSame(\range(1, 4), $results);
}
/**
* @expectedException \TypeError
* @expectedExceptionMessage Must provide an array or instance of Traversable
* @dataProvider provideInvalidStreamArguments
*/
public function testInvalid($arg) {
Amp\stream($arg);
}
public function provideInvalidStreamArguments() {
return [
[null],
[new \stdClass],
[32],
[false],
[true],
["string"],
];
}
}