From ff113f7b2f08fff223825b62baf7479c34fa9f45 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Mon, 25 Feb 2019 18:54:49 -0300 Subject: [PATCH 01/31] Fix possible false raw content, closes #159 --- src/Swoole/Swoole.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Swoole/Swoole.php b/src/Swoole/Swoole.php index 0235d6f9..ed12d786 100644 --- a/src/Swoole/Swoole.php +++ b/src/Swoole/Swoole.php @@ -222,5 +222,11 @@ function cors(string $origin = '*', string $headers = 'Content-Type', string $me */ function raw(): string { - return Container\get(SWOOLE_HTTP_REQUEST)->rawContent(); + $content = Container\get(SWOOLE_HTTP_REQUEST)->rawContent(); + + if (empty($content)) { + return ''; + } + + return $content; } From e0eb966ad256d75d51284a8971cb078f438ae7d9 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Mon, 25 Feb 2019 18:58:16 -0300 Subject: [PATCH 02/31] Sugar for HTTP Status 204 No Content, closes #160 --- src/Swoole/Swoole.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Swoole/Swoole.php b/src/Swoole/Swoole.php index ed12d786..6a7c38fb 100644 --- a/src/Swoole/Swoole.php +++ b/src/Swoole/Swoole.php @@ -230,3 +230,11 @@ function raw(): string return $content; } + +/** + * Sugar for HTTP 204 No Content. + */ +function no_content() +{ + emit('', 204); +} From d6c910954599daf6f6c83c9823becaf4dc87291f Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 18:27:56 -0300 Subject: [PATCH 03/31] Add fetch function and default fetch mode --- src/Db/Db.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Db/Db.php b/src/Db/Db.php index 17774b99..f63d9007 100644 --- a/src/Db/Db.php +++ b/src/Db/Db.php @@ -22,6 +22,8 @@ function connect(string $dsn, string $username = 'root', string $passwd = '', array $options = []): \PDO { $pdo = new \PDO($dsn, $username, $passwd, $options); + $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); + Container\set(DB_DEFAULT_NAME, $pdo); return $pdo; @@ -107,6 +109,26 @@ function fetch_all(string $statement, int $fetchStyle = \PDO::FETCH_ASSOC, strin return $results; } +/** + * Calls `query()` and `fetch()` on the given `\PDOStatement` in a single function. + * + * @param string $statement The SQL statement. + * @param int $fetchStyle The PDO fetch style to use, defaults to FETCH_ASSOC. + * @param string $pdoName The PDO name on the `Siler\Container` to be used, defaults to the DB_DEFAULT_NAME. + * + * @return array|null Returns the resulting array (maybe empty) or null in case of error. + */ +function fetch(string $statement, int $fetchStyle = \PDO::FETCH_ASSOC, string $pdoName = DB_DEFAULT_NAME): ?array +{ + $result = query($statement, $pdoName)->fetch($fetchStyle); + + if ($result === false) { + return null; + } + + return $result; +} + /** * Gets a MySQL Data Source Name (DSN) composed by the given $opts. * From c868c1d2e6a47e58a3f8e3830f6805e985ac2c3d Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 20:03:08 -0300 Subject: [PATCH 04/31] Add if_then and lazy versions for empty and is_null --- src/Functional/Functional.php | 44 ++++++++++++++++++++++++ tests/Unit/Functional/FunctionalTest.php | 6 ++++ 2 files changed, 50 insertions(+) diff --git a/src/Functional/Functional.php b/src/Functional/Functional.php index a3ac81ee..2305c110 100644 --- a/src/Functional/Functional.php +++ b/src/Functional/Functional.php @@ -480,3 +480,47 @@ function partial(callable $callable, ...$partial): \Closure return call_user_func_array($callable, array_merge($partial, $args)); }; } + +/** + * Calls a function if the predicate is true. + * + * @param callable $predicate + * + * @return \Closure + */ +function if_then(callable $predicate): \Closure +{ + return function (callable $then) use ($predicate) { + if ($predicate()) { + $then(); + } + }; +} + +/** + * A lazy empty evaluation. + * + * @param $var + * + * @return \Closure + */ +function is_empty($var): \Closure +{ + return function () use ($var): bool { + return empty($var); + }; +} + +/** + * A lazy is_null evaluation. + * + * @param $var + * + * @return \Closure + */ +function isnull($var): \Closure +{ + return function () use ($var): bool { + return is_null($var); + }; +} diff --git a/tests/Unit/Functional/FunctionalTest.php b/tests/Unit/Functional/FunctionalTest.php index 99411968..3d15c1ca 100644 --- a/tests/Unit/Functional/FunctionalTest.php +++ b/tests/Unit/Functional/FunctionalTest.php @@ -330,4 +330,10 @@ public function testPartial() $commaExplode = f\partial('explode', ','); $this->assertSame(['foo', 'bar'], $commaExplode('foo,bar')); } + + public function testIfThen() + { + $this->expectOutputString('if_then'); + f\if_then(f\always(true))(f\puts('if_then')); + } } From e93a3db6bc381f6ee08ef2a2f22a67f8ff3edd44 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 20:03:34 -0300 Subject: [PATCH 05/31] Add the Mistake feature --- composer.json | 1 + composer.lock | 25 +++++++++--------- src/Prelude/Mistake.php | 56 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/Prelude/Mistake.php diff --git a/composer.json b/composer.json index 1c43cc80..1f47d412 100644 --- a/composer.json +++ b/composer.json @@ -62,6 +62,7 @@ "src/Jwt/Jwt.php", "src/Mail/SwiftMailer.php", "src/Monolog/Monolog.php", + "src/Prelude/Mistake.php", "src/Prelude/Str.php", "src/Prelude/Tuple.php", "src/Ratchet/Ratchet.php", diff --git a/composer.lock b/composer.lock index 22309b00..acdc1e05 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8be1ead73c9ab0f6168790f279025cf6", + "content-hash": "fd7f88f25b5a5c524fae07814f35b685", "packages": [], "packages-dev": [ { @@ -972,16 +972,16 @@ }, { "name": "phan/phan", - "version": "1.2.4", + "version": "1.2.5", "source": { "type": "git", "url": "https://github.com/phan/phan.git", - "reference": "bf1fc09b21e6bb64dca56d0e99a0f3099c00280a" + "reference": "c987c6621a8b7595320c851bd8553115e49436b1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phan/phan/zipball/bf1fc09b21e6bb64dca56d0e99a0f3099c00280a", - "reference": "bf1fc09b21e6bb64dca56d0e99a0f3099c00280a", + "url": "https://api.github.com/repos/phan/phan/zipball/c987c6621a8b7595320c851bd8553115e49436b1", + "reference": "c987c6621a8b7595320c851bd8553115e49436b1", "shasum": "" }, "require": { @@ -1000,6 +1000,7 @@ }, "suggest": { "ext-ast": "Needed for parsing ASTs (unless --use-fallback-parser is used). 1.0.1+ is recommended, php-ast ^0.1.5|^1.0.0 is needed.", + "ext-igbinary": "Improves performance of polyfill when ext-ast is unavailable", "ext-tokenizer": "Needed for non-AST support and file/line-based suppressions." }, "bin": [ @@ -1034,7 +1035,7 @@ "php", "static" ], - "time": "2019-02-18T21:01:26+00:00" + "time": "2019-02-27T14:10:48+00:00" }, { "name": "phar-io/manifest", @@ -1405,16 +1406,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "7.0.2", + "version": "7.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "cfca9c5f7f2694ca0c7749ffb142927d9f05250f" + "reference": "0317a769a81845c390e19684d9ba25d7f6aa4707" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/cfca9c5f7f2694ca0c7749ffb142927d9f05250f", - "reference": "cfca9c5f7f2694ca0c7749ffb142927d9f05250f", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0317a769a81845c390e19684d9ba25d7f6aa4707", + "reference": "0317a769a81845c390e19684d9ba25d7f6aa4707", "shasum": "" }, "require": { @@ -1464,7 +1465,7 @@ "testing", "xunit" ], - "time": "2019-02-15T13:40:27+00:00" + "time": "2019-02-26T07:38:26+00:00" }, { "name": "phpunit/php-file-iterator", @@ -4238,7 +4239,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.1", + "php": ">=7.2", "ext-json": "*" }, "platform-dev": { diff --git a/src/Prelude/Mistake.php b/src/Prelude/Mistake.php new file mode 100644 index 00000000..fdf2cfe1 --- /dev/null +++ b/src/Prelude/Mistake.php @@ -0,0 +1,56 @@ +id = base64_encode(uniqid()); + } + + /** + * Gets the generated ID for the Mistake. + * + * @return string + */ + public function getId(): string + { + return $this->id; + } + + public function jsonSerialize() + { + return [ + 'id' => $this->id, + 'code' => $this->getCode(), + 'error' => true, + 'message' => $this->getMessage(), + ]; + } +} + +/** + * Creates a Closure for a future Mistake. + * + * @param int $code + * @param string $message + * + * @return \Closure + */ +function abort(int $code, string $message): \Closure +{ + return function () use ($code, $message) { + throw new Mistake($code, $message); + }; +} From 5d3c57d1dc3a621371ed0fa2b83a8bf7c1a101f6 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 20:22:25 -0300 Subject: [PATCH 06/31] Add the Success feature --- composer.json | 1 + src/Prelude/Success.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/Prelude/Success.php diff --git a/composer.json b/composer.json index 1f47d412..5cf34cd6 100644 --- a/composer.json +++ b/composer.json @@ -64,6 +64,7 @@ "src/Monolog/Monolog.php", "src/Prelude/Mistake.php", "src/Prelude/Str.php", + "src/Prelude/Success.php", "src/Prelude/Tuple.php", "src/Ratchet/Ratchet.php", "src/Route/Route.php", diff --git a/src/Prelude/Success.php b/src/Prelude/Success.php new file mode 100644 index 00000000..ad58b4bb --- /dev/null +++ b/src/Prelude/Success.php @@ -0,0 +1,39 @@ +data = $data; + } + + public function jsonSerialize() + { + return array_merge(['error' => false], $this->data); + } +} + +/** + * Lazy create a Success. + * + * @param array $data + * + * @return \Closure + */ +function success(array $data = []): \Closure +{ + return function () use ($data): Success { + return new Success($data); + }; +} From bed3147651fa75e9276219945ad6024cdff0ce46 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 20:28:00 -0300 Subject: [PATCH 07/31] Drop Ratchet, closes #162 --- README.md | 23 ---- composer.json | 1 - src/Ratchet/MessageComponent.php | 80 ------------ src/Ratchet/Ratchet.php | 105 ---------------- tests/Unit/Ratchet/MessageComponentTest.php | 128 -------------------- tests/Unit/Ratchet/RatchetTest.php | 99 --------------- 6 files changed, 436 deletions(-) delete mode 100644 src/Ratchet/MessageComponent.php delete mode 100644 src/Ratchet/Ratchet.php delete mode 100644 tests/Unit/Ratchet/MessageComponentTest.php delete mode 100644 tests/Unit/Ratchet/RatchetTest.php diff --git a/README.md b/README.md index 0b96f024..c60b1328 100644 --- a/README.md +++ b/README.md @@ -139,29 +139,6 @@ Log\alert('alert', ['level' => 'alert']); Log\emergency('emergency', ['level' => 'emergency']); ``` -#### Ratchet - -Real-time web apps using WebSockets. - -```bash -$ composer require cboden/ratchet -``` - -```php -use Siler\Ratchet; - -Ratchet\connected(function ($conn) { - print("New connection\n"); -}); - -Ratchet\inbox(function ($from, $message) { - printf("New message: %s\n", $message); -}); - -print("Listen on 3333\n"); -Ratchet\init(3333); -``` - #### GraphQL [A query language for your API](http://graphql.org/). Thanks to webonyx/graphql-php you can build you Schema from a diff --git a/composer.json b/composer.json index 5cf34cd6..c1c25b15 100644 --- a/composer.json +++ b/composer.json @@ -66,7 +66,6 @@ "src/Prelude/Str.php", "src/Prelude/Success.php", "src/Prelude/Tuple.php", - "src/Ratchet/Ratchet.php", "src/Route/Route.php", "src/Stratigility/Stratigility.php", "src/Swoole/Swoole.php", diff --git a/src/Ratchet/MessageComponent.php b/src/Ratchet/MessageComponent.php deleted file mode 100644 index 952bc277..00000000 --- a/src/Ratchet/MessageComponent.php +++ /dev/null @@ -1,80 +0,0 @@ -attach($conn); - $this->callback(RATCHET_EVENT_OPEN, [$conn]); - } - - - /** - * {@inheritdoc}. - */ - public function onMessage(ConnectionInterface $from, $message) - { - $this->callback(RATCHET_EVENT_MESSAGE, [$from, $message]); - } - - - /** - * {@inheritdoc}. - */ - public function onClose(ConnectionInterface $conn) - { - Container\get(RATCHET_CONNECTIONS)->detach($conn); - $this->callback(RATCHET_EVENT_CLOSE, [$conn]); - } - - - /** - * {@inheritdoc}. - */ - public function onError(ConnectionInterface $conn, \Exception $exception) - { - $this->callback(RATCHET_EVENT_ERROR, [$conn, $exception]); - $conn->close(); - } - - - /** - * Helper function to call event callbacks checking for its existence. - * - * @param string $ratchetEvent The event name used in the Siler\Container - * @param array $params The array of params to be used as arguments - */ - private function callback($ratchetEvent, array $params) - { - $callback = Container\get($ratchetEvent); - - if (is_null($callback)) { - return; - } - - if (!is_callable($callback)) { - return; - } - - call_user_func_array($callback, $params); - } -} diff --git a/src/Ratchet/Ratchet.php b/src/Ratchet/Ratchet.php deleted file mode 100644 index f435ae88..00000000 --- a/src/Ratchet/Ratchet.php +++ /dev/null @@ -1,105 +0,0 @@ -send($message); - } -} diff --git a/tests/Unit/Ratchet/MessageComponentTest.php b/tests/Unit/Ratchet/MessageComponentTest.php deleted file mode 100644 index 10e59ea3..00000000 --- a/tests/Unit/Ratchet/MessageComponentTest.php +++ /dev/null @@ -1,128 +0,0 @@ -conn = $this->createMock(ConnectionInterface::class); - $this->storage = $this->createMock(\SplObjectStorage::class); - - Container\set(Ratchet\RATCHET_CONNECTIONS, $this->storage); - } - - public function testCallbackIsNull() - { - $onOpenCalled = false; - - $messageComponent = new MessageComponent(); - $messageComponent->onOpen($this->conn); - - $this->assertFalse($onOpenCalled); - } - - public function testCallbackIsntCallable() - { - $onOpenCalled = false; - $onOpen = 'not callable'; - - Container\set(Ratchet\RATCHET_EVENT_OPEN, $onOpen); - - $messageComponent = new MessageComponent(); - $messageComponent->onOpen($this->conn); - - $this->assertFalse($onOpenCalled); - } - - public function testOnOpen() - { - $this->storage->expects($this->once()) - ->method('attach') - ->with($this->equalTo($this->conn)); - - $onOpenCalled = false; - - $onOpen = function () use (&$onOpenCalled) { - $onOpenCalled = true; - }; - - Container\set(Ratchet\RATCHET_EVENT_OPEN, $onOpen); - - $messageComponent = new MessageComponent(); - $messageComponent->onOpen($this->conn); - - $this->assertTrue($onOpenCalled); - } - - public function testOnMessage() - { - $message = 'test'; - - $onMessageFrom = null; - $onMessageMessage = null; - - $onMessage = function ($from, $message) use (&$onMessageFrom, &$onMessageMessage) { - $onMessageFrom = $from; - $onMessageMessage = $message; - }; - - Container\set(Ratchet\RATCHET_EVENT_MESSAGE, $onMessage); - - $messageComponent = new MessageComponent(); - $messageComponent->onMessage($this->conn, $message); - - $this->assertSame($this->conn, $onMessageFrom); - $this->assertSame($message, $onMessageMessage); - } - - public function testOnClose() - { - $this->storage->expects($this->once()) - ->method('detach') - ->with($this->equalTo($this->conn)); - - $onCloseCalled = false; - - $onClose = function () use (&$onCloseCalled) { - $onCloseCalled = true; - }; - - Container\set(Ratchet\RATCHET_EVENT_CLOSE, $onClose); - - $messageComponent = new MessageComponent(); - $messageComponent->onClose($this->conn); - - $this->assertTrue($onCloseCalled); - } - - public function testOnError() - { - $expectedException = new \Exception(); - $actualException = null; - - $this->conn->expects($this->once()) - ->method('close'); - - $onError = function ($conn, $e) use (&$actualException) { - $actualException = $e; - }; - - Container\set(Ratchet\RATCHET_EVENT_ERROR, $onError); - - $messageComponent = new MessageComponent(); - $messageComponent->onError($this->conn, $expectedException); - - $this->assertSame($expectedException, $actualException); - } -} diff --git a/tests/Unit/Ratchet/RatchetTest.php b/tests/Unit/Ratchet/RatchetTest.php deleted file mode 100644 index b1db0dad..00000000 --- a/tests/Unit/Ratchet/RatchetTest.php +++ /dev/null @@ -1,99 +0,0 @@ -assertInstanceOf(\SplObjectStorage::class, Container\get(Ratchet\RATCHET_CONNECTIONS)); - $this->assertInstanceOf(IoServer::class, $server); - } - - public function testConnected() - { - $expected = function () { - }; - - Ratchet\connected($expected); - $actual = Container\get(Ratchet\RATCHET_EVENT_OPEN); - - $this->assertSame($expected, $actual); - } - - public function testInbox() - { - $expected = function () { - }; - - Ratchet\inbox($expected); - $actual = Container\get(Ratchet\RATCHET_EVENT_MESSAGE); - - $this->assertSame($expected, $actual); - } - - public function testClosed() - { - $expected = function () { - }; - - Ratchet\closed($expected); - $actual = Container\get(Ratchet\RATCHET_EVENT_CLOSE); - - $this->assertSame($expected, $actual); - } - - public function testError() - { - $expected = function () { - }; - - Ratchet\error($expected); - $actual = Container\get(Ratchet\RATCHET_EVENT_ERROR); - - $this->assertSame($expected, $actual); - } - - public function testBroadcast() - { - $message = 'test'; - - $mock = $this->getMockBuilder(ConnectionInterface::class) - ->setMethods(['send', 'close']) - ->getMock(); - - $mock->expects($this->once()) - ->method('send') - ->with($this->equalTo($message)); - - Container\set(Ratchet\RATCHET_CONNECTIONS, [$mock]); - - Ratchet\broadcast($message); - } - - public function testBroadcastIgnoreSender() - { - $message = 'test'; - - $mock = $this->getMockBuilder(ConnectionInterface::class) - ->setMethods(['send', 'close']) - ->getMock(); - - $mock->expects($this->never()) - ->method('send') - ->with($this->equalTo($message)); - - Container\set(Ratchet\RATCHET_CONNECTIONS, [$mock]); - - Ratchet\broadcast($message, $mock); - } -} From 757df8c6fb141e54787a71312fa66288b41c44a7 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 28 Feb 2019 20:30:09 -0300 Subject: [PATCH 08/31] Drop Jwt, closes #163 --- composer.json | 1 - src/Jwt/Jwt.php | 140 ------------------------------ tests/Integration/JwtAuthTest.php | 50 ----------- tests/Unit/Jwt/JwtTest.php | 99 --------------------- tests/fixtures/.env | 4 - tests/fixtures/jwt_signed.txt | 1 - tests/fixtures/jwt_unsigned.txt | 1 - 7 files changed, 296 deletions(-) delete mode 100644 src/Jwt/Jwt.php delete mode 100644 tests/Integration/JwtAuthTest.php delete mode 100644 tests/Unit/Jwt/JwtTest.php delete mode 100644 tests/fixtures/jwt_signed.txt delete mode 100644 tests/fixtures/jwt_unsigned.txt diff --git a/composer.json b/composer.json index c1c25b15..d81c8eae 100644 --- a/composer.json +++ b/composer.json @@ -59,7 +59,6 @@ "src/Http/Request.php", "src/Http/Response.php", "src/HttpHandlerRunner/HttpHandlerRunner.php", - "src/Jwt/Jwt.php", "src/Mail/SwiftMailer.php", "src/Monolog/Monolog.php", "src/Prelude/Mistake.php", diff --git a/src/Jwt/Jwt.php b/src/Jwt/Jwt.php deleted file mode 100644 index 09b93830..00000000 --- a/src/Jwt/Jwt.php +++ /dev/null @@ -1,140 +0,0 @@ -setIssuer($config['iss']); - } - - if (isset($config['aud'])) { - $builder->setAudience($config['aud']); - } - - if (isset($config['jti'])) { - $builder->setId($config['jti'], true); - } - - if (isset($config['iat'])) { - $builder->setIssuedAt($config['iat']); - } - - if (isset($config['nbf'])) { - $builder->setNotBefore($config['nbf']); - } - - if (isset($config['exp'])) { - $builder->setExpiration($config['exp']); - } - - return function (array $data) use ($builder, $signer, $key) { - foreach ($data as $claim => $value) { - $builder->set($claim, $value); - } - - if (!is_null($signer) && !is_null($key)) { - $builder->sign($signer, $key); - } - - return $builder->getToken(); - }; -} - - -/** - * Returns a validation function based on the given $config and $time. - * - * @param array $config - * @param string $time - * - * @return \Closure - */ -function validator(array $config, string $time) : \Closure -{ - $data = new ValidationData(); - $data->setCurrentTime((int) $time); - - if (isset($config['iss'])) { - $data->setIssuer($config['iss']); - } - - if (isset($config['aud'])) { - $data->setAudience($config['aud']); - } - - if (isset($config['jti'])) { - $data->setId($config['jti']); - } - - return function (Token $token) use ($data) { - return $token->validate($data); - }; -} - - -/** - * Parser helper. - * - * @param string $token - * - * @return Token - */ -function parse(string $token) : Token -{ - return (new Parser())->parse($token); -} - - -/** - * Configuration helper. - * - * @param ?string $iss configures the issuer (iss claim) - * @param ?string $aud configures the audience (aud claim) - * @param ?string $jti configures the id (jti claim), replicating as a header item - * @param ?string $iat configures the time at which the token was issued (iat claim) - * @param ?string $nbf configures the time at which the token can be used (nbf claim) - * @param ?string $exp configures the expiration time of the token (nbf claim) - * - * @return array - */ -function conf( - ?string $iss = null, - ?string $aud = null, - ?string $jti = null, - ?string $iat = null, - ?string $nbf = null, - ?string $exp = null -) : array { - return [ - 'iss' => $iss, - 'aud' => $aud, - 'jti' => $jti, - 'iat' => $iat, - 'nbf' => $nbf, - 'exp' => $exp, - ]; -} diff --git a/tests/Integration/JwtAuthTest.php b/tests/Integration/JwtAuthTest.php deleted file mode 100644 index 7c8781b7..00000000 --- a/tests/Integration/JwtAuthTest.php +++ /dev/null @@ -1,50 +0,0 @@ - 1]); - - self::$cookies['jwt'] = (string) $token; - - $this->assertSame(env('JWT_ISS'), $token->getClaim('iss')); - $this->assertSame(1, $token->getClaim('uid')); - } - - /** - * @depends testSignUp - */ - public function testSignIn() - { - $token = self::$cookies['jwt']; - - $token = Jwt\parse($token); - - $this->assertTrue(Jwt\validator(Jwt\conf(env('JWT_ISS'), env('JWT_AUD')), strval(time()))($token)); - $this->assertTrue($token->verify(new Sha256(), env('APP_KEY'))); - - $this->assertSame(1, $token->getClaim('uid')); - } -} diff --git a/tests/Unit/Jwt/JwtTest.php b/tests/Unit/Jwt/JwtTest.php deleted file mode 100644 index 72f40066..00000000 --- a/tests/Unit/Jwt/JwtTest.php +++ /dev/null @@ -1,99 +0,0 @@ -config = [ - 'iss' => self::ISS, - 'aud' => self::AUD, - 'jti' => self::JTI, - 'iat' => self::IAT, - 'nbf' => self::NBF, - 'exp' => self::EXP, - ]; - - $this->data = ['uid' => 1]; - - $this->signer = new Sha256(); - } - - public function testBuilder() - { - $token = Jwt\builder($this->config)($this->data); - - $this->assertSame(self::JTI, $token->getHeader('jti')); - - $this->assertSame(self::ISS, $token->getClaim('iss')); - $this->assertSame(self::AUD, $token->getClaim('aud')); - $this->assertEquals(self::IAT, $token->getClaim('iat')); - $this->assertEquals(self::NBF, $token->getClaim('nbf')); - $this->assertEquals(self::EXP, $token->getClaim('exp')); - - $this->assertSame(1, $token->getClaim('uid')); - - $this->assertStringEqualsFile(__DIR__ . '/../../fixtures/jwt_unsigned.txt', (string) $token); - } - - public function testValidate() - { - $token = Jwt\builder($this->config)($this->data); - - $this->assertFalse(Jwt\validator($this->config, self::IAT)($token)); - $this->assertTrue(Jwt\validator($this->config, self::NBF)($token)); - $this->assertTrue(Jwt\validator($this->config, self::EXP)($token)); - $this->assertFalse(Jwt\validator($this->config, strval(self::EXP + 1))($token)); - } - - public function testBuilderWithSigner() - { - $token = Jwt\builder($this->config, $this->signer, self::KEY)($this->data); - - $this->assertStringEqualsFile(__DIR__ . '/../../fixtures/jwt_signed.txt', (string) $token); - - $this->assertTrue($token->verify($this->signer, self::KEY)); - $this->assertFalse($token->verify($this->signer, 'wrong key')); - } - - public function testParse() - { - $token = Jwt\parse(file_get_contents(__DIR__ . '/../../fixtures/jwt_signed.txt')); - - $this->assertSame(self::JTI, $token->getHeader('jti')); - - $this->assertSame(self::ISS, $token->getClaim('iss')); - $this->assertSame(self::AUD, $token->getClaim('aud')); - $this->assertEquals(self::IAT, $token->getClaim('iat')); - $this->assertEquals(self::NBF, $token->getClaim('nbf')); - $this->assertEquals(self::EXP, $token->getClaim('exp')); - - $this->assertSame(1, $token->getClaim('uid')); - - $this->assertTrue($token->verify($this->signer, self::KEY)); - } - - public function testConf() - { - $config = Jwt\conf(self::ISS, self::AUD, self::JTI, self::IAT, self::NBF, self::EXP); - $this->assertSame($this->config, $config); - } -} diff --git a/tests/fixtures/.env b/tests/fixtures/.env index 6cc5d05d..4672cd2a 100644 --- a/tests/fixtures/.env +++ b/tests/fixtures/.env @@ -1,6 +1,2 @@ FOO=bar - APP_KEY=test - -JWT_ISS=https://example.com -JWT_AUD=https://example.org diff --git a/tests/fixtures/jwt_signed.txt b/tests/fixtures/jwt_signed.txt deleted file mode 100644 index de61e352..00000000 --- a/tests/fixtures/jwt_signed.txt +++ /dev/null @@ -1 +0,0 @@ -eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6InRlc3QxMjMifQ.eyJpc3MiOiJodHRwczpcL1wvZXhhbXBsZS5jb20iLCJhdWQiOiJodHRwczpcL1wvZXhhbXBsZS5vcmciLCJqdGkiOiJ0ZXN0MTIzIiwiaWF0IjoxMDAwMCwibmJmIjoxMDA2MCwiZXhwIjoxMzYwMCwidWlkIjoxfQ.iIAFbDyfvgLsZtcUQgx1KNhG1RRukyCcl7Nle4tPa-g \ No newline at end of file diff --git a/tests/fixtures/jwt_unsigned.txt b/tests/fixtures/jwt_unsigned.txt deleted file mode 100644 index 48f348c0..00000000 --- a/tests/fixtures/jwt_unsigned.txt +++ /dev/null @@ -1 +0,0 @@ -eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIiwianRpIjoidGVzdDEyMyJ9.eyJpc3MiOiJodHRwczpcL1wvZXhhbXBsZS5jb20iLCJhdWQiOiJodHRwczpcL1wvZXhhbXBsZS5vcmciLCJqdGkiOiJ0ZXN0MTIzIiwiaWF0IjoxMDAwMCwibmJmIjoxMDA2MCwiZXhwIjoxMzYwMCwidWlkIjoxfQ. \ No newline at end of file From bc5f955097c7805699d0d1cc1c15b97c5faecfb0 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Tue, 12 Mar 2019 14:59:33 -0300 Subject: [PATCH 09/31] Dropping PSR-4 since there is so few classes --- composer.json | 8 +- composer.lock | 270 +++++++++++++++++++++++++------ tests/Unit/Dotenv/DotenvTest.php | 2 +- tests/Unit/Http/RequestTest.php | 6 +- 4 files changed, 235 insertions(+), 51 deletions(-) diff --git a/composer.json b/composer.json index d81c8eae..1c2998db 100644 --- a/composer.json +++ b/composer.json @@ -43,9 +43,6 @@ "zendframework/zend-stratigility": "^3.1" }, "autoload": { - "psr-4": { - "Siler\\": "src/" - }, "files": [ "src/Container/Container.php", "src/Db/Db.php", @@ -53,8 +50,12 @@ "src/Diactoros/Diactoros.php", "src/Dotenv/Dotenv.php", "src/Functional/Functional.php", + "src/Functional/Monad/Identity.php", + "src/Functional/Monad/Maybe.php", "src/Functional/Monad/Monad.php", "src/GraphQL/GraphQL.php", + "src/GraphQL/SubscriptionsManager.php", + "src/GraphQL/SubscriptionsServer.php", "src/Http/Http.php", "src/Http/Request.php", "src/Http/Response.php", @@ -66,6 +67,7 @@ "src/Prelude/Success.php", "src/Prelude/Tuple.php", "src/Route/Route.php", + "src/Stratigility/RequestHandlerDecorator.php", "src/Stratigility/Stratigility.php", "src/Swoole/Swoole.php", "src/Twig/Twig.php", diff --git a/composer.lock b/composer.lock index acdc1e05..e56414f6 100644 --- a/composer.lock +++ b/composer.lock @@ -972,16 +972,16 @@ }, { "name": "phan/phan", - "version": "1.2.5", + "version": "1.2.6", "source": { "type": "git", "url": "https://github.com/phan/phan.git", - "reference": "c987c6621a8b7595320c851bd8553115e49436b1" + "reference": "78b8f2bdab9dab2146872bf130a9265a3f1f9a27" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phan/phan/zipball/c987c6621a8b7595320c851bd8553115e49436b1", - "reference": "c987c6621a8b7595320c851bd8553115e49436b1", + "url": "https://api.github.com/repos/phan/phan/zipball/78b8f2bdab9dab2146872bf130a9265a3f1f9a27", + "reference": "78b8f2bdab9dab2146872bf130a9265a3f1f9a27", "shasum": "" }, "require": { @@ -1035,7 +1035,7 @@ "php", "static" ], - "time": "2019-02-27T14:10:48+00:00" + "time": "2019-03-09T19:38:20+00:00" }, { "name": "phar-io/manifest", @@ -3294,25 +3294,28 @@ }, { "name": "swiftmailer/swiftmailer", - "version": "v6.1.3", + "version": "v6.2.0", "source": { "type": "git", "url": "https://github.com/swiftmailer/swiftmailer.git", - "reference": "8ddcb66ac10c392d3beb54829eef8ac1438595f4" + "reference": "6fa3232ff9d3f8237c0fae4b7ff05e1baa4cd707" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/8ddcb66ac10c392d3beb54829eef8ac1438595f4", - "reference": "8ddcb66ac10c392d3beb54829eef8ac1438595f4", + "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/6fa3232ff9d3f8237c0fae4b7ff05e1baa4cd707", + "reference": "6fa3232ff9d3f8237c0fae4b7ff05e1baa4cd707", "shasum": "" }, "require": { "egulias/email-validator": "~2.0", - "php": ">=7.0.0" + "php": ">=7.0.0", + "symfony/polyfill-iconv": "^1.0", + "symfony/polyfill-intl-idn": "^1.10", + "symfony/polyfill-mbstring": "^1.0" }, "require-dev": { "mockery/mockery": "~0.9.1", - "symfony/phpunit-bridge": "~3.3@dev" + "symfony/phpunit-bridge": "^3.4.19|^4.1.8" }, "suggest": { "ext-intl": "Needed to support internationalized email addresses", @@ -3321,7 +3324,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "6.1-dev" + "dev-master": "6.2-dev" } }, "autoload": { @@ -3349,20 +3352,20 @@ "mail", "mailer" ], - "time": "2018-09-11T07:12:52+00:00" + "time": "2019-03-10T07:52:41+00:00" }, { "name": "symfony/console", - "version": "v4.2.3", + "version": "v4.2.4", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4" + "reference": "9dc2299a016497f9ee620be94524e6c0af0280a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4", - "reference": "1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4", + "url": "https://api.github.com/repos/symfony/console/zipball/9dc2299a016497f9ee620be94524e6c0af0280a9", + "reference": "9dc2299a016497f9ee620be94524e6c0af0280a9", "shasum": "" }, "require": { @@ -3421,7 +3424,7 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2019-01-25T14:35:16+00:00" + "time": "2019-02-23T15:17:42+00:00" }, { "name": "symfony/contracts", @@ -3493,16 +3496,16 @@ }, { "name": "symfony/http-foundation", - "version": "v4.2.3", + "version": "v4.2.4", "source": { "type": "git", "url": "https://github.com/symfony/http-foundation.git", - "reference": "8d2318b73e0a1bc75baa699d00ebe2ae8b595a39" + "reference": "850a667d6254ccf6c61d853407b16f21c4579c77" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/8d2318b73e0a1bc75baa699d00ebe2ae8b595a39", - "reference": "8d2318b73e0a1bc75baa699d00ebe2ae8b595a39", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/850a667d6254ccf6c61d853407b16f21c4579c77", + "reference": "850a667d6254ccf6c61d853407b16f21c4579c77", "shasum": "" }, "require": { @@ -3543,7 +3546,7 @@ ], "description": "Symfony HttpFoundation Component", "homepage": "https://symfony.com", - "time": "2019-01-29T09:49:29+00:00" + "time": "2019-02-26T08:03:39+00:00" }, { "name": "symfony/polyfill-ctype", @@ -3603,6 +3606,127 @@ ], "time": "2018-08-06T14:22:27+00:00" }, + { + "name": "symfony/polyfill-iconv", + "version": "v1.10.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-iconv.git", + "reference": "97001cfc283484c9691769f51cdf25259037eba2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-iconv/zipball/97001cfc283484c9691769f51cdf25259037eba2", + "reference": "97001cfc283484c9691769f51cdf25259037eba2", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-iconv": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.9-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Iconv\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Iconv extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "iconv", + "polyfill", + "portable", + "shim" + ], + "time": "2018-09-21T06:26:08+00:00" + }, + { + "name": "symfony/polyfill-intl-idn", + "version": "v1.10.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-idn.git", + "reference": "89de1d44f2c059b266f22c9cc9124ddc4cd0987a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/89de1d44f2c059b266f22c9cc9124ddc4cd0987a", + "reference": "89de1d44f2c059b266f22c9cc9124ddc4cd0987a", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "symfony/polyfill-mbstring": "^1.3", + "symfony/polyfill-php72": "^1.9" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.9-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Intl\\Idn\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + }, + { + "name": "Laurent Bassin", + "email": "laurent@bassin.info" + } + ], + "description": "Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "idn", + "intl", + "polyfill", + "portable", + "shim" + ], + "time": "2018-09-30T16:36:12+00:00" + }, { "name": "symfony/polyfill-mbstring", "version": "v1.10.0", @@ -3662,18 +3786,73 @@ ], "time": "2018-09-21T13:07:52+00:00" }, + { + "name": "symfony/polyfill-php72", + "version": "v1.10.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php72.git", + "reference": "9050816e2ca34a8e916c3a0ae8b9c2fccf68b631" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/9050816e2ca34a8e916c3a0ae8b9c2fccf68b631", + "reference": "9050816e2ca34a8e916c3a0ae8b9c2fccf68b631", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.9-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php72\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2018-09-21T13:07:52+00:00" + }, { "name": "symfony/routing", - "version": "v4.2.3", + "version": "v4.2.4", "source": { "type": "git", "url": "https://github.com/symfony/routing.git", - "reference": "7f8e44fc498972466f0841c3e48dc555f23bdf53" + "reference": "ff03eae644e6b1e26d4a04b2385fe3a1a7f04e42" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/routing/zipball/7f8e44fc498972466f0841c3e48dc555f23bdf53", - "reference": "7f8e44fc498972466f0841c3e48dc555f23bdf53", + "url": "https://api.github.com/repos/symfony/routing/zipball/ff03eae644e6b1e26d4a04b2385fe3a1a7f04e42", + "reference": "ff03eae644e6b1e26d4a04b2385fe3a1a7f04e42", "shasum": "" }, "require": { @@ -3737,7 +3916,7 @@ "uri", "url" ], - "time": "2019-01-29T09:49:29+00:00" + "time": "2019-02-23T15:17:42+00:00" }, { "name": "theseer/tokenizer", @@ -3781,16 +3960,16 @@ }, { "name": "twig/twig", - "version": "v2.6.2", + "version": "v2.7.1", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "7d7342c8a4059fefb9b8d07db0cc14007021f9b7" + "reference": "c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/7d7342c8a4059fefb9b8d07db0cc14007021f9b7", - "reference": "7d7342c8a4059fefb9b8d07db0cc14007021f9b7", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69", + "reference": "c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69", "shasum": "" }, "require": { @@ -3806,7 +3985,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.6-dev" + "dev-master": "2.7-dev" } }, "autoload": { @@ -3844,20 +4023,20 @@ "keywords": [ "templating" ], - "time": "2019-01-14T15:00:48+00:00" + "time": "2019-03-12T15:45:07+00:00" }, { "name": "vlucas/phpdotenv", - "version": "v3.3.2", + "version": "v3.3.3", "source": { "type": "git", "url": "https://github.com/vlucas/phpdotenv.git", - "reference": "1ee9369cfbf26cfcf1f2515d98f15fab54e9647a" + "reference": "dbcc609971dd9b55f48b8008b553d79fd372ddde" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/1ee9369cfbf26cfcf1f2515d98f15fab54e9647a", - "reference": "1ee9369cfbf26cfcf1f2515d98f15fab54e9647a", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/dbcc609971dd9b55f48b8008b553d79fd372ddde", + "reference": "dbcc609971dd9b55f48b8008b553d79fd372ddde", "shasum": "" }, "require": { @@ -3896,7 +4075,7 @@ "env", "environment" ], - "time": "2019-01-30T10:43:17+00:00" + "time": "2019-03-06T09:39:45+00:00" }, { "name": "webmozart/assert", @@ -3951,22 +4130,22 @@ }, { "name": "webonyx/graphql-php", - "version": "v0.13.0", + "version": "v0.13.1", "source": { "type": "git", "url": "https://github.com/webonyx/graphql-php.git", - "reference": "9bb8e7327748fdc828f801001161e6b586ca96e5" + "reference": "0226b08429e5a102ea99cda9ddc4a7185006473f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webonyx/graphql-php/zipball/9bb8e7327748fdc828f801001161e6b586ca96e5", - "reference": "9bb8e7327748fdc828f801001161e6b586ca96e5", + "url": "https://api.github.com/repos/webonyx/graphql-php/zipball/0226b08429e5a102ea99cda9ddc4a7185006473f", + "reference": "0226b08429e5a102ea99cda9ddc4a7185006473f", "shasum": "" }, "require": { "ext-json": "*", "ext-mbstring": "*", - "php": "^7.1" + "php": "^7.1||^8.0" }, "require-dev": { "doctrine/coding-standard": "^5.0", @@ -3974,6 +4153,7 @@ "phpstan/phpstan": "0.10.5", "phpstan/phpstan-phpunit": "0.10.0", "phpstan/phpstan-strict-rules": "0.10.1", + "phpunit/phpcov": "^5.0", "phpunit/phpunit": "^7.2", "psr/http-message": "^1.0", "react/promise": "2.*" @@ -3998,7 +4178,7 @@ "api", "graphql" ], - "time": "2018-11-27T10:23:58+00:00" + "time": "2019-03-09T15:34:29+00:00" }, { "name": "zendframework/zend-diactoros", diff --git a/tests/Unit/Dotenv/DotenvTest.php b/tests/Unit/Dotenv/DotenvTest.php index 4a87e35f..2afa68d4 100644 --- a/tests/Unit/Dotenv/DotenvTest.php +++ b/tests/Unit/Dotenv/DotenvTest.php @@ -13,7 +13,7 @@ public function testEnv() { $entries = \Siler\Dotenv\init(__DIR__ . '/../../fixtures'); - $this->assertCount(4, $entries); + $this->assertCount(2, $entries); $this->assertArrayHasKey('FOO', $entries); $this->assertSame('bar', $entries['FOO']); $this->assertSame($_SERVER, env()); diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php index 2e11bad9..403d5214 100644 --- a/tests/Unit/Http/RequestTest.php +++ b/tests/Unit/Http/RequestTest.php @@ -185,7 +185,9 @@ public function testRecommendedLocale() $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.5'; $this->assertSame('en-US', Request\recommended_locale('it')); - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = ''; - $this->assertSame(\locale_get_default(), Request\recommended_locale()); + if (function_exists('locale_get_default')) { + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = ''; + $this->assertSame(\locale_get_default(), Request\recommended_locale()); + } } } From 2d4595cebe25a4e8acd673d1026ccb582dfc20c2 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Tue, 12 Mar 2019 15:07:56 -0300 Subject: [PATCH 10/31] Restore PSR-4 because of classes that should be lazy loaded on peer deps --- composer.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 1c2998db..d81c8eae 100644 --- a/composer.json +++ b/composer.json @@ -43,6 +43,9 @@ "zendframework/zend-stratigility": "^3.1" }, "autoload": { + "psr-4": { + "Siler\\": "src/" + }, "files": [ "src/Container/Container.php", "src/Db/Db.php", @@ -50,12 +53,8 @@ "src/Diactoros/Diactoros.php", "src/Dotenv/Dotenv.php", "src/Functional/Functional.php", - "src/Functional/Monad/Identity.php", - "src/Functional/Monad/Maybe.php", "src/Functional/Monad/Monad.php", "src/GraphQL/GraphQL.php", - "src/GraphQL/SubscriptionsManager.php", - "src/GraphQL/SubscriptionsServer.php", "src/Http/Http.php", "src/Http/Request.php", "src/Http/Response.php", @@ -67,7 +66,6 @@ "src/Prelude/Success.php", "src/Prelude/Tuple.php", "src/Route/Route.php", - "src/Stratigility/RequestHandlerDecorator.php", "src/Stratigility/Stratigility.php", "src/Swoole/Swoole.php", "src/Twig/Twig.php", From 722ed4859e41ec09125c5207c8fdd233c0308f8a Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Tue, 12 Mar 2019 22:15:14 -0300 Subject: [PATCH 11/31] Using Twig's PSR-4 --- src/Twig/Twig.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Twig/Twig.php b/src/Twig/Twig.php index cd79ca97..c5aacb07 100644 --- a/src/Twig/Twig.php +++ b/src/Twig/Twig.php @@ -19,10 +19,10 @@ * * @return \Twig_Environment */ -function init(string $templatesPath, $templatesCachePath = false, bool $debug = false) : \Twig_Environment +function init(string $templatesPath, $templatesCachePath = false, bool $debug = false) : \Twig\Environment { - $twig = new \Twig_Environment( - new \Twig_Loader_Filesystem($templatesPath), + $twig = new \Twig\Environment( + new \Twig\Loader\Filesystem($templatesPath), [ 'debug' => $debug, 'cache' => $templatesCachePath, From 33a61c2e9a1ae2b1670e099b3ad8178436e5f4df Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Tue, 12 Mar 2019 22:15:59 -0300 Subject: [PATCH 12/31] :white_check_mark: Add functional tests --- tests/Unit/Functional/FunctionalTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Unit/Functional/FunctionalTest.php b/tests/Unit/Functional/FunctionalTest.php index 3d15c1ca..bc133dc8 100644 --- a/tests/Unit/Functional/FunctionalTest.php +++ b/tests/Unit/Functional/FunctionalTest.php @@ -336,4 +336,16 @@ public function testIfThen() $this->expectOutputString('if_then'); f\if_then(f\always(true))(f\puts('if_then')); } + + public function testIsEmpty() + { + $this->assertTrue(f\is_empty([])()); + $this->assertFalse(f\is_empty('[]')()); + } + + public function testIsNull() + { + $this->assertTrue(f\isnull(null)()); + $this->assertFalse(f\isnull([])()); + } } From 55bcab5c4fd470e6971caecbf51a16383dfe92e8 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Wed, 13 Mar 2019 00:22:52 -0300 Subject: [PATCH 13/31] Composer update --- composer.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.lock b/composer.lock index e56414f6..da394c52 100644 --- a/composer.lock +++ b/composer.lock @@ -3960,16 +3960,16 @@ }, { "name": "twig/twig", - "version": "v2.7.1", + "version": "v2.7.2", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69" + "reference": "70c59531da43afe598c66135e39cac39475a2f51" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69", - "reference": "c0dd4dc5f44e541c9c0e0506da76aa4f14b41c69", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/70c59531da43afe598c66135e39cac39475a2f51", + "reference": "70c59531da43afe598c66135e39cac39475a2f51", "shasum": "" }, "require": { @@ -4023,7 +4023,7 @@ "keywords": [ "templating" ], - "time": "2019-03-12T15:45:07+00:00" + "time": "2019-03-12T18:48:26+00:00" }, { "name": "vlucas/phpdotenv", From f0f5143f09bb96c0639df30779a19a7482f9e8fd Mon Sep 17 00:00:00 2001 From: Leo Cavalcante Date: Wed, 13 Mar 2019 01:08:14 -0300 Subject: [PATCH 14/31] Result module in favor of Success/Mistake (#165) --- composer.json | 3 +- src/Prelude/Mistake.php | 56 ------------------------ src/Prelude/Success.php | 39 ----------------- src/Result/Failure.php | 23 ++++++++++ src/Result/Result.php | 71 +++++++++++++++++++++++++++++++ src/Result/Success.php | 23 ++++++++++ tests/Unit/Result/FailureTest.php | 41 ++++++++++++++++++ tests/Unit/Result/ResultTest.php | 26 +++++++++++ tests/Unit/Result/SuccessTest.php | 41 ++++++++++++++++++ 9 files changed, 226 insertions(+), 97 deletions(-) delete mode 100644 src/Prelude/Mistake.php delete mode 100644 src/Prelude/Success.php create mode 100644 src/Result/Failure.php create mode 100644 src/Result/Result.php create mode 100644 src/Result/Success.php create mode 100644 tests/Unit/Result/FailureTest.php create mode 100644 tests/Unit/Result/ResultTest.php create mode 100644 tests/Unit/Result/SuccessTest.php diff --git a/composer.json b/composer.json index d81c8eae..90025a38 100644 --- a/composer.json +++ b/composer.json @@ -61,10 +61,9 @@ "src/HttpHandlerRunner/HttpHandlerRunner.php", "src/Mail/SwiftMailer.php", "src/Monolog/Monolog.php", - "src/Prelude/Mistake.php", "src/Prelude/Str.php", - "src/Prelude/Success.php", "src/Prelude/Tuple.php", + "src/Result/Result.php", "src/Route/Route.php", "src/Stratigility/Stratigility.php", "src/Swoole/Swoole.php", diff --git a/src/Prelude/Mistake.php b/src/Prelude/Mistake.php deleted file mode 100644 index fdf2cfe1..00000000 --- a/src/Prelude/Mistake.php +++ /dev/null @@ -1,56 +0,0 @@ -id = base64_encode(uniqid()); - } - - /** - * Gets the generated ID for the Mistake. - * - * @return string - */ - public function getId(): string - { - return $this->id; - } - - public function jsonSerialize() - { - return [ - 'id' => $this->id, - 'code' => $this->getCode(), - 'error' => true, - 'message' => $this->getMessage(), - ]; - } -} - -/** - * Creates a Closure for a future Mistake. - * - * @param int $code - * @param string $message - * - * @return \Closure - */ -function abort(int $code, string $message): \Closure -{ - return function () use ($code, $message) { - throw new Mistake($code, $message); - }; -} diff --git a/src/Prelude/Success.php b/src/Prelude/Success.php deleted file mode 100644 index ad58b4bb..00000000 --- a/src/Prelude/Success.php +++ /dev/null @@ -1,39 +0,0 @@ -data = $data; - } - - public function jsonSerialize() - { - return array_merge(['error' => false], $this->data); - } -} - -/** - * Lazy create a Success. - * - * @param array $data - * - * @return \Closure - */ -function success(array $data = []): \Closure -{ - return function () use ($data): Success { - return new Success($data); - }; -} diff --git a/src/Result/Failure.php b/src/Result/Failure.php new file mode 100644 index 00000000..50f2f6ff --- /dev/null +++ b/src/Result/Failure.php @@ -0,0 +1,23 @@ +id = is_null($id) ? base64_encode(uniqid()) : $id; + $this->code = $code; + $this->data = $data; + } + + public function getId(): string + { + return $this->id; + } + + public function getCode(): int + { + return $this->code; + } + + public function getData() + { + return $this->data; + } + + abstract public function isSuccess(): bool; + abstract public function isFailure(): bool; + + public function jsonSerialize() + { + $json = [ + 'error' => $this->isFailure() && !$this->isSuccess(), + 'id' => $this->id, + ]; + + if (is_null($this->data)) { + return $json; + } + + if (is_string($this->data)) { + $json['message'] = $this->data; + return $json; + } + + if (is_array($this->data)) { + return array_merge($json, $this->data); + } + + $json['data'] = $this->data; + return $json; + } +} + +function success(int $code, $data = null): Success +{ + return new Success($code, $data); +} + +function failure(int $code, $data = null): Failure +{ + return new Failure($code, $data); +} diff --git a/src/Result/Success.php b/src/Result/Success.php new file mode 100644 index 00000000..8e2f2e83 --- /dev/null +++ b/src/Result/Success.php @@ -0,0 +1,23 @@ +assertNotEmpty($failure->getId()); + $this->assertSame(42, $failure->getCode()); + $this->assertNull($failure->getData()); + } + + public function testIsSuccess() + { + $failure = new Failure(42); + $this->assertTrue($failure->isFailure()); + $this->assertFalse($failure->isSuccess()); + } + + public function testJson() + { + $failure = new Failure(42, null, 'test'); + $this->assertSame('{"error":true,"id":"test"}', json_encode($failure)); + + $failure = new Failure(43, 'foo', 'test'); + $this->assertSame('{"error":true,"id":"test","message":"foo"}', json_encode($failure)); + + $failure = new Failure(43, ['foo' => 'bar'], 'test'); + $this->assertSame('{"error":true,"id":"test","foo":"bar"}', json_encode($failure)); + + $failure = new Failure(43, true, 'test'); + $this->assertSame('{"error":true,"id":"test","data":true}', json_encode($failure)); + } +} diff --git a/tests/Unit/Result/ResultTest.php b/tests/Unit/Result/ResultTest.php new file mode 100644 index 00000000..b76d9e4e --- /dev/null +++ b/tests/Unit/Result/ResultTest.php @@ -0,0 +1,26 @@ +assertInstanceOf(Success::class, $success); + } + + public function testFailure() + { + $failure = failure(0); + $this->assertInstanceOf(Failure::class, $failure); + } +} diff --git a/tests/Unit/Result/SuccessTest.php b/tests/Unit/Result/SuccessTest.php new file mode 100644 index 00000000..9061e0b7 --- /dev/null +++ b/tests/Unit/Result/SuccessTest.php @@ -0,0 +1,41 @@ +assertNotEmpty($success->getId()); + $this->assertSame(42, $success->getCode()); + $this->assertNull($success->getData()); + } + + public function testIsSuccess() + { + $success = new Success(42); + $this->assertTrue($success->isSuccess()); + $this->assertFalse($success->isFailure()); + } + + public function testJson() + { + $success = new Success(42, null, 'test'); + $this->assertSame('{"error":false,"id":"test"}', json_encode($success)); + + $success = new Success(43, 'foo', 'test'); + $this->assertSame('{"error":false,"id":"test","message":"foo"}', json_encode($success)); + + $success = new Success(43, ['foo' => 'bar'], 'test'); + $this->assertSame('{"error":false,"id":"test","foo":"bar"}', json_encode($success)); + + $success = new Success(43, true, 'test'); + $this->assertSame('{"error":false,"id":"test","data":true}', json_encode($success)); + } +} From b4fed597572fbbaf25cd81ceb78657af27ea8c7f Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Wed, 13 Mar 2019 11:08:03 -0300 Subject: [PATCH 15/31] ext-intl may not be installed, closes #166 --- src/Http/Request.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index 1c31823b..7a2f4468 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -299,7 +299,8 @@ function recommended_locale(string $default = '') : string $locale = $default; } - if (empty($locale)) { + if (empty($locale) && function_exists('locale_get_default')) { + /* @phan-suppress-next-line PhanUndeclaredFunction */ $locale = \locale_get_default(); } From 2268da721dc1e65e9ea00dd98289b5951d14f007 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Wed, 13 Mar 2019 11:22:31 -0300 Subject: [PATCH 16/31] Using new Twig interface, suppress deprecated until 3.0, closes #167 --- src/Twig/Twig.php | 3 ++- tests/Unit/Twig/TwigTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Twig/Twig.php b/src/Twig/Twig.php index c5aacb07..c9eb08a9 100644 --- a/src/Twig/Twig.php +++ b/src/Twig/Twig.php @@ -22,7 +22,8 @@ function init(string $templatesPath, $templatesCachePath = false, bool $debug = false) : \Twig\Environment { $twig = new \Twig\Environment( - new \Twig\Loader\Filesystem($templatesPath), + /* @phan-suppress-next-line PhanDeprecatedInterface */ + new \Twig\Loader\FilesystemLoader($templatesPath), [ 'debug' => $debug, 'cache' => $templatesCachePath, diff --git a/tests/Unit/Twig/TwigTest.php b/tests/Unit/Twig/TwigTest.php index b9baa995..256f775b 100644 --- a/tests/Unit/Twig/TwigTest.php +++ b/tests/Unit/Twig/TwigTest.php @@ -22,7 +22,7 @@ public function testRenderWithoutInit() public function testCreateTwigEnv() { $twigEnv = Twig\init(__DIR__ . '/../../fixtures'); - $this->assertInstanceOf(\Twig_Environment::class, $twigEnv); + $this->assertInstanceOf(\Twig\Environment::class, $twigEnv); } public function testRender() From 5619bec9633f620922a4ff32e30e154b26feba26 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Wed, 13 Mar 2019 17:13:45 -0300 Subject: [PATCH 17/31] result: coolest api --- src/Result/Result.php | 6 +++--- tests/Unit/Result/FailureTest.php | 6 +++--- tests/Unit/Result/SuccessTest.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Result/Result.php b/src/Result/Result.php index fde7db51..8ab3efdb 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -17,17 +17,17 @@ public function __construct(int $code, $data = null, string $id = null) $this->data = $data; } - public function getId(): string + public function id(): string { return $this->id; } - public function getCode(): int + public function code(): int { return $this->code; } - public function getData() + public function unwrap() { return $this->data; } diff --git a/tests/Unit/Result/FailureTest.php b/tests/Unit/Result/FailureTest.php index 10284cda..8be92d26 100644 --- a/tests/Unit/Result/FailureTest.php +++ b/tests/Unit/Result/FailureTest.php @@ -12,9 +12,9 @@ class FailureTest extends TestCase public function testConstructor() { $failure = new Failure(42); - $this->assertNotEmpty($failure->getId()); - $this->assertSame(42, $failure->getCode()); - $this->assertNull($failure->getData()); + $this->assertNotEmpty($failure->id()); + $this->assertSame(42, $failure->code()); + $this->assertNull($failure->unwrap()); } public function testIsSuccess() diff --git a/tests/Unit/Result/SuccessTest.php b/tests/Unit/Result/SuccessTest.php index 9061e0b7..507b8a2b 100644 --- a/tests/Unit/Result/SuccessTest.php +++ b/tests/Unit/Result/SuccessTest.php @@ -12,9 +12,9 @@ class SuccessTest extends TestCase public function testConstructor() { $success = new Success(42); - $this->assertNotEmpty($success->getId()); - $this->assertSame(42, $success->getCode()); - $this->assertNull($success->getData()); + $this->assertNotEmpty($success->id()); + $this->assertSame(42, $success->code()); + $this->assertNull($success->unwrap()); } public function testIsSuccess() From 0e0a676ceaaa586aad61f1a184d3b15d53494d92 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 14:20:02 -0300 Subject: [PATCH 18/31] redis: draft --- src/Db/Redis.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Db/Redis.php diff --git a/src/Db/Redis.php b/src/Db/Redis.php new file mode 100644 index 00000000..17e87d21 --- /dev/null +++ b/src/Db/Redis.php @@ -0,0 +1,30 @@ +connect($host, $port); + + Container\set($redisInstance, $redis); + + return $redis; +} + + +function get(string $key, string $redisInstance = DEFAULT_INSTANCE) +{ + $redis = Container\get(DEFAULT_INSTANCE); + return $redis->get($key); +} + +function set(string $key, string $val, string $redisInstance = DEFAULT_INSTANCE) +{ + $redis = Container\get(DEFAULT_INSTANCE); + return $redis->set($key, $val); +} From 2e65237a1dac4083b4e8ad7324cfa9f052fa19c9 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 14:22:14 -0300 Subject: [PATCH 19/31] redis: add has function --- src/Db/Redis.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Db/Redis.php b/src/Db/Redis.php index 17e87d21..5f287183 100644 --- a/src/Db/Redis.php +++ b/src/Db/Redis.php @@ -28,3 +28,9 @@ function set(string $key, string $val, string $redisInstance = DEFAULT_INSTANCE) $redis = Container\get(DEFAULT_INSTANCE); return $redis->set($key, $val); } + +function has(string $key): bool +{ + $redis = Container\get(DEFAULT_INSTANCE); + return $redis->exists($key) > 0; +} From 12ec44ed95c59ed52b47176d16f1b72cba69234a Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 14:32:33 -0300 Subject: [PATCH 20/31] redis: add to autoload files --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 90025a38..4bfdf1fa 100644 --- a/composer.json +++ b/composer.json @@ -50,6 +50,7 @@ "src/Container/Container.php", "src/Db/Db.php", "src/Db/Mongo.php", + "src/Db/Redis.php", "src/Diactoros/Diactoros.php", "src/Dotenv/Dotenv.php", "src/Functional/Functional.php", From 6e010245ecf3928c772cf3bad156918f62522653 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 14:34:28 -0300 Subject: [PATCH 21/31] redis: from the root namespace --- src/Db/Redis.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Db/Redis.php b/src/Db/Redis.php index 5f287183..3cf399d0 100644 --- a/src/Db/Redis.php +++ b/src/Db/Redis.php @@ -6,9 +6,9 @@ const DEFAULT_INSTANCE = 'redis_default_instance'; -function connect(string $host = '127.0.0.1', int $port = 6379, string $redisInstance = DEFAULT_INSTANCE): Redis +function connect(string $host = '127.0.0.1', int $port = 6379, string $redisInstance = DEFAULT_INSTANCE): \Redis { - $redis = new Redis(); + $redis = new \Redis(); $redis->connect($host, $port); Container\set($redisInstance, $redis); From 99ea9259265858f7a04e243df438cea38038dfdd Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 15:07:30 -0300 Subject: [PATCH 22/31] redis: fix instance usage and add docblocks --- src/Db/Redis.php | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Db/Redis.php b/src/Db/Redis.php index 3cf399d0..50447481 100644 --- a/src/Db/Redis.php +++ b/src/Db/Redis.php @@ -4,8 +4,20 @@ use Siler\Container; +/** + * Default Redis instance name on Siler\Container. + */ const DEFAULT_INSTANCE = 'redis_default_instance'; +/** + * Creates an instance and connects to a Redis server. + * + * @param string $host + * @param int $port + * @param string $redisInstance + * + * @return \Redis + */ function connect(string $host = '127.0.0.1', int $port = 6379, string $redisInstance = DEFAULT_INSTANCE): \Redis { $redis = new \Redis(); @@ -16,21 +28,44 @@ function connect(string $host = '127.0.0.1', int $port = 6379, string $redisInst return $redis; } - +/** + * Gets the value from the given $key. + * + * @param string $key + * @param string $redisInstance + * + * @return mixed + */ function get(string $key, string $redisInstance = DEFAULT_INSTANCE) { - $redis = Container\get(DEFAULT_INSTANCE); + $redis = Container\get($redisInstance); return $redis->get($key); } +/** + * Sets a value on the given $key. + * + * @param string $key + * @param string $val + * @param string $redisInstance + * + * @return mixed + */ function set(string $key, string $val, string $redisInstance = DEFAULT_INSTANCE) { - $redis = Container\get(DEFAULT_INSTANCE); + $redis = Container\get($redisInstance); return $redis->set($key, $val); } -function has(string $key): bool +/** + * Checks if the key exists. + * + * @param string $key + * + * @return bool + */ +function has(string $key, string $redisInstance = DEFAULT_INSTANCE): bool { - $redis = Container\get(DEFAULT_INSTANCE); + $redis = Container\get($redisInstance); return $redis->exists($key) > 0; } From ec62afb52e5759eabb21b132600f9c3c57f55119 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 17:43:21 -0300 Subject: [PATCH 23/31] redis: as dev dependency --- .travis.yml | 8 ++++---- composer.json | 3 ++- composer.lock | 5 +++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index b2aec6dd..4b058ee4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,11 @@ language: php php: - - '7.2' - - '7.3' + - "7.2" + - "7.3" install: - - pecl install -f mongodb-1.5.3 ast-1.0.1 + - pecl install -f mongodb ast redis - composer install script: @@ -18,4 +18,4 @@ after_success: branches: only: - - master \ No newline at end of file + - master diff --git a/composer.json b/composer.json index 4bfdf1fa..451af130 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "minimum-stability": "stable", "require-dev": { "ext-mongodb": "^1.5", + "ext-redis": "^4.3", "cboden/ratchet": "^0.4", "cocur/slugify": "^3.2", "gabordemooij/redbean": "^5.2", @@ -90,4 +91,4 @@ "suggest": { "vlucas/phpdotenv": "Storing configuration in the environment is one of the tenets of a twelve-factor app." } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index da394c52..a62c65a9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fd7f88f25b5a5c524fae07814f35b685", + "content-hash": "303c921bb2c1cbabf7b7d597368e1c92", "packages": [], "packages-dev": [ { @@ -4423,6 +4423,7 @@ "ext-json": "*" }, "platform-dev": { - "ext-mongodb": "^1.5" + "ext-mongodb": "^1.5", + "ext-redis": "^4.3" } } From 8e99a03ea28c0a992ce4b37a1a230c0300f3d377 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 18:02:51 -0300 Subject: [PATCH 24/31] ci: set pecl extensions versions --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4b058ee4..4dc70868 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ php: - "7.3" install: - - pecl install -f mongodb ast redis + - pecl install -f mongodb-1.5.3 ast-1.0.1 redis-4.3.0 - composer install script: From f3363bed8dd128006b52e517efa3b6173b0a5911 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 18:29:55 -0300 Subject: [PATCH 25/31] ci: enable igbinary --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4dc70868..b63646d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ php: - "7.3" install: - - pecl install -f mongodb-1.5.3 ast-1.0.1 redis-4.3.0 + - pecl install -f --enable-igbinary mongodb ast redis - composer install script: From 242d62f23849d32351e3f6a50a7a9246892a5614 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 19:24:24 -0300 Subject: [PATCH 26/31] ci: temporary disable redis --- .phan/config.php | 1 + .travis.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.phan/config.php b/.phan/config.php index 60fc8af0..5b41a5e2 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -300,6 +300,7 @@ // to `exclude_analysis_directory_list`. 'exclude_analysis_directory_list' => [ 'src/Swoole/Swoole.php', + 'src/Db/Redis.php', 'vendor/', ], diff --git a/.travis.yml b/.travis.yml index b63646d2..a6657507 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ php: - "7.3" install: - - pecl install -f --enable-igbinary mongodb ast redis + - pecl install mongodb ast - composer install script: From b6fb243db5921aaaaf2f648211af2b9547300741 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 19:29:00 -0300 Subject: [PATCH 27/31] ci: remove redis from dev deps and force pecl --- .travis.yml | 2 +- composer.json | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a6657507..af897885 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ php: - "7.3" install: - - pecl install mongodb ast + - pecl install -f mongodb ast - composer install script: diff --git a/composer.json b/composer.json index 451af130..169bb5d6 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ "minimum-stability": "stable", "require-dev": { "ext-mongodb": "^1.5", - "ext-redis": "^4.3", "cboden/ratchet": "^0.4", "cocur/slugify": "^3.2", "gabordemooij/redbean": "^5.2", From 44b1d2363ee04be30fd115420a753a47bce6f3dd Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 19:40:39 -0300 Subject: [PATCH 28/31] composer update --- composer.lock | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.lock b/composer.lock index a62c65a9..da394c52 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "303c921bb2c1cbabf7b7d597368e1c92", + "content-hash": "fd7f88f25b5a5c524fae07814f35b685", "packages": [], "packages-dev": [ { @@ -4423,7 +4423,6 @@ "ext-json": "*" }, "platform-dev": { - "ext-mongodb": "^1.5", - "ext-redis": "^4.3" + "ext-mongodb": "^1.5" } } From 18aed58b1de2c46dc6b932828ae0bcb4aba34d96 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 19:46:20 -0300 Subject: [PATCH 29/31] :white_check_mark: http: test error on json encode --- tests/Unit/Http/ResponseTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php index 06a7e584..2883a30c 100644 --- a/tests/Unit/Http/ResponseTest.php +++ b/tests/Unit/Http/ResponseTest.php @@ -52,6 +52,14 @@ public function testJson() $this->assertContains('Content-Type: application/json;charset=utf-8', xdebug_get_headers()); } + public function testJsonError() + { + $this->expectException(\UnexpectedValueException::class ); + + Response\json(fopen('php://input', 'r')); + } + + public function testStatusCode() { $this->expectOutputString('{"error":true,"message":"test"}'); From 491754e2eb9a22764ff307fd2a9ee50f906db7be Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Thu, 14 Mar 2019 20:40:49 -0300 Subject: [PATCH 30/31] code style --- tests/Unit/Http/ResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php index 2883a30c..3050740a 100644 --- a/tests/Unit/Http/ResponseTest.php +++ b/tests/Unit/Http/ResponseTest.php @@ -54,7 +54,7 @@ public function testJson() public function testJsonError() { - $this->expectException(\UnexpectedValueException::class ); + $this->expectException(\UnexpectedValueException::class); Response\json(fopen('php://input', 'r')); } From 0f1e056a5a3d2b25ff50f34c5b2e3cb76de283e2 Mon Sep 17 00:00:00 2001 From: leocavalcante Date: Fri, 15 Mar 2019 00:27:45 -0300 Subject: [PATCH 31/31] result: refactoring code as a second parameter --- src/Result/Failure.php | 4 ++-- src/Result/Result.php | 14 +++++++------- src/Result/Success.php | 4 ++-- tests/Unit/Result/FailureTest.php | 16 ++++++++-------- tests/Unit/Result/ResultTest.php | 4 ++-- tests/Unit/Result/SuccessTest.php | 14 +++++++------- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Result/Failure.php b/src/Result/Failure.php index 50f2f6ff..10930581 100644 --- a/src/Result/Failure.php +++ b/src/Result/Failure.php @@ -6,9 +6,9 @@ final class Failure extends Result { - public function __construct(int $code, $data = null, string $id = null) + public function __construct($data = null, int $code = 1, string $id = null) { - parent::__construct($code, $data, $id); + parent::__construct($data, $code, $id); } public function isSuccess(): bool diff --git a/src/Result/Result.php b/src/Result/Result.php index 8ab3efdb..274a68d8 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -7,14 +7,14 @@ abstract class Result implements \JsonSerializable { private $id; - private $code; private $data; + private $code; - public function __construct(int $code, $data = null, string $id = null) + public function __construct($data = null, int $code = 0, string $id = null) { $this->id = is_null($id) ? base64_encode(uniqid()) : $id; - $this->code = $code; $this->data = $data; + $this->code = $code; } public function id(): string @@ -60,12 +60,12 @@ public function jsonSerialize() } } -function success(int $code, $data = null): Success +function success($data = null, int $code = 0, string $id = null): Success { - return new Success($code, $data); + return new Success($data, $code, $id); } -function failure(int $code, $data = null): Failure +function failure($data = null, int $code = 1, string $id = null): Failure { - return new Failure($code, $data); + return new Failure($data, $code, $id); } diff --git a/src/Result/Success.php b/src/Result/Success.php index 8e2f2e83..cdad53ce 100644 --- a/src/Result/Success.php +++ b/src/Result/Success.php @@ -6,9 +6,9 @@ final class Success extends Result { - public function __construct(int $code, $data = null, string $id = null) + public function __construct($data = null, int $code = 0, string $id = null) { - parent::__construct($code, $data, $id); + parent::__construct($data, $code, $id); } public function isSuccess(): bool diff --git a/tests/Unit/Result/FailureTest.php b/tests/Unit/Result/FailureTest.php index 8be92d26..b1be1f41 100644 --- a/tests/Unit/Result/FailureTest.php +++ b/tests/Unit/Result/FailureTest.php @@ -11,31 +11,31 @@ class FailureTest extends TestCase { public function testConstructor() { - $failure = new Failure(42); + $failure = new Failure(); $this->assertNotEmpty($failure->id()); - $this->assertSame(42, $failure->code()); $this->assertNull($failure->unwrap()); + $this->assertSame(1, $failure->code()); } - public function testIsSuccess() + public function testIsFailure() { - $failure = new Failure(42); + $failure = new Failure(); $this->assertTrue($failure->isFailure()); $this->assertFalse($failure->isSuccess()); } public function testJson() { - $failure = new Failure(42, null, 'test'); + $failure = new Failure(null, 1, 'test'); $this->assertSame('{"error":true,"id":"test"}', json_encode($failure)); - $failure = new Failure(43, 'foo', 'test'); + $failure = new Failure('foo', 1, 'test'); $this->assertSame('{"error":true,"id":"test","message":"foo"}', json_encode($failure)); - $failure = new Failure(43, ['foo' => 'bar'], 'test'); + $failure = new Failure(['foo' => 'bar'], 1, 'test'); $this->assertSame('{"error":true,"id":"test","foo":"bar"}', json_encode($failure)); - $failure = new Failure(43, true, 'test'); + $failure = new Failure(true, 1, 'test'); $this->assertSame('{"error":true,"id":"test","data":true}', json_encode($failure)); } } diff --git a/tests/Unit/Result/ResultTest.php b/tests/Unit/Result/ResultTest.php index b76d9e4e..db646113 100644 --- a/tests/Unit/Result/ResultTest.php +++ b/tests/Unit/Result/ResultTest.php @@ -14,13 +14,13 @@ class ResultTest extends TestCase { public function testSuccess() { - $success = success(0); + $success = success(); $this->assertInstanceOf(Success::class, $success); } public function testFailure() { - $failure = failure(0); + $failure = failure(); $this->assertInstanceOf(Failure::class, $failure); } } diff --git a/tests/Unit/Result/SuccessTest.php b/tests/Unit/Result/SuccessTest.php index 507b8a2b..9a541486 100644 --- a/tests/Unit/Result/SuccessTest.php +++ b/tests/Unit/Result/SuccessTest.php @@ -11,31 +11,31 @@ class SuccessTest extends TestCase { public function testConstructor() { - $success = new Success(42); + $success = new Success(); $this->assertNotEmpty($success->id()); - $this->assertSame(42, $success->code()); $this->assertNull($success->unwrap()); + $this->assertSame(0, $success->code()); } public function testIsSuccess() { - $success = new Success(42); + $success = new Success(); $this->assertTrue($success->isSuccess()); $this->assertFalse($success->isFailure()); } public function testJson() { - $success = new Success(42, null, 'test'); + $success = new Success(null, 0, 'test'); $this->assertSame('{"error":false,"id":"test"}', json_encode($success)); - $success = new Success(43, 'foo', 'test'); + $success = new Success('foo', 0, 'test'); $this->assertSame('{"error":false,"id":"test","message":"foo"}', json_encode($success)); - $success = new Success(43, ['foo' => 'bar'], 'test'); + $success = new Success(['foo' => 'bar'], 0, 'test'); $this->assertSame('{"error":false,"id":"test","foo":"bar"}', json_encode($success)); - $success = new Success(43, true, 'test'); + $success = new Success(true, 0, 'test'); $this->assertSame('{"error":false,"id":"test","data":true}', json_encode($success)); } }