From bc58df6ebca361bc8cd060736d59954f09f183c7 Mon Sep 17 00:00:00 2001 From: Carsten Windler Date: Tue, 11 Sep 2018 00:07:44 +0200 Subject: [PATCH] Add travis ci, fix symfony tests, add easy coding standard --- .travis.yml | 19 ++++ README.md | 1 - composer.json | 5 +- easy-coding-standard.yml | 4 + src/HttpHelper.php | 12 +-- tests/Unit/Request/RequestToStringTest.php | 6 +- tests/Unit/RequestToCurlTest.php | 66 ------------- tests/Unit/RequestToFileTest.php | 105 --------------------- tests/Unit/RequestToStringTest.php | 71 -------------- 9 files changed, 35 insertions(+), 254 deletions(-) create mode 100644 .travis.yml create mode 100644 easy-coding-standard.yml delete mode 100755 tests/Unit/RequestToCurlTest.php delete mode 100755 tests/Unit/RequestToFileTest.php delete mode 100755 tests/Unit/RequestToStringTest.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..aff186c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ + +language: php + +cache: + directories: + - vendor + - $HOME/.composer/cache + +php: + - 7.0 + - 7.1 + - 7.2 + +install: + - composer install + +script: + - vendor/bin/phpunit -c phpunit.xml --coverage-text + - vendor/bin/ecs check src \ No newline at end of file diff --git a/README.md b/README.md index 41c32ca..71b4992 100755 --- a/README.md +++ b/README.md @@ -74,5 +74,4 @@ For Symfony, the build-in serializers are used. Nice. # Todo -* setup TravisCI * make storage path of `*_to_file()` configurable diff --git a/composer.json b/composer.json index 5356b1e..52e190d 100755 --- a/composer.json +++ b/composer.json @@ -11,14 +11,15 @@ } ], "require": { - "php": "^5.3 | ^7.0", + "php": ">=7.0", "zendframework/zend-diactoros": "^1.3", "symfony/http-foundation": "^2.7 |^3.3" }, "require-dev": { "phpunit/phpunit": "^5 | ^6.3", "mockery/mockery": "0.9.*", - "mikey179/vfsStream": "^1.6.4" + "mikey179/vfsStream": "^1.6.4", + "symplify/easy-coding-standard": "^4.6" }, "autoload": { "files": [ diff --git a/easy-coding-standard.yml b/easy-coding-standard.yml new file mode 100644 index 0000000..0a87059 --- /dev/null +++ b/easy-coding-standard.yml @@ -0,0 +1,4 @@ +# easy-coding-standard.yml +imports: +- { resource: 'vendor/symplify/easy-coding-standard/config/clean-code.yml' } +- { resource: 'vendor/symplify/easy-coding-standard/config/psr2.yml' } \ No newline at end of file diff --git a/src/HttpHelper.php b/src/HttpHelper.php index ce21f8a..a26f394 100755 --- a/src/HttpHelper.php +++ b/src/HttpHelper.php @@ -14,15 +14,13 @@ function request_to_string($request) { $requestString = "unknown\r\n"; - if ( - class_exists('Symfony\Component\HttpFoundation\Request') && + if (class_exists('Symfony\Component\HttpFoundation\Request') && $request instanceof Symfony\Component\HttpFoundation\Request ) { $requestString = $request->__toString(); } - if ( - interface_exists('Psr\Http\Message\RequestInterface') && + if (interface_exists('Psr\Http\Message\RequestInterface') && $request instanceof Psr\Http\Message\RequestInterface ) { $requestString = RequestSerializer::toString($request); @@ -107,15 +105,13 @@ function response_to_string($response) { $responseString = "unknown\r\n"; - if ( - class_exists('Symfony\Component\HttpFoundation\Response') && + if (class_exists('Symfony\Component\HttpFoundation\Response') && $response instanceof Symfony\Component\HttpFoundation\Response ) { $responseString = $response->__toString(); } - if ( - interface_exists('Psr\Http\Message\ResponseInterface') && + if (interface_exists('Psr\Http\Message\ResponseInterface') && $response instanceof Psr\Http\Message\ResponseInterface ) { $responseString = ResponseSerializer::toString($response); diff --git a/tests/Unit/Request/RequestToStringTest.php b/tests/Unit/Request/RequestToStringTest.php index 2d87c42..17b8ab2 100755 --- a/tests/Unit/Request/RequestToStringTest.php +++ b/tests/Unit/Request/RequestToStringTest.php @@ -48,8 +48,12 @@ public function symfony_request_to_string() TestCase::assertEquals( "POST / HTTP/1.1\r\n" . + "Accept: \r\n" . + "Accept-Charset: \r\n" . + "Accept-Language: \r\n" . "Content-Type: application/x-www-form-urlencoded\r\n" . - "Host: www.carstenwindler.de\r\n\r\n", + "Host: www.carstenwindler.de\r\n" . + "User-Agent: \r\n\r\n", request_to_string($request) ); } diff --git a/tests/Unit/RequestToCurlTest.php b/tests/Unit/RequestToCurlTest.php deleted file mode 100755 index ec90c1e..0000000 --- a/tests/Unit/RequestToCurlTest.php +++ /dev/null @@ -1,66 +0,0 @@ -assertEquals( - "curl -X GET http://www.carstenwindler.de -H 'Host: www.carstenwindler.de'", - request_to_curl($request) - ); - } - - /** - * @test - */ - public function to_curl_with_body() - { - $body = new Stream('php://memory', 'w'); - $body->write('body'); - - $request = new Request( - 'http://www.carstenwindler.de', - 'POST', - $body - ); - - $this->assertEquals( - "curl -X POST http://www.carstenwindler.de -H 'Host: www.carstenwindler.de' -d 'body'", - request_to_curl($request) - ); - } - - /** - * @test - */ - public function to_curl_with_header() - { - $body = new Stream('php://memory', 'w'); - - $request = new Request( - 'http://www.carstenwindler.de', - 'POST', - $body, - ['Content-Type' => 'text/xml'] - ); - - $this->assertEquals( - "curl -X POST http://www.carstenwindler.de -H 'Content-Type: text/xml' -H 'Host: www.carstenwindler.de'", - request_to_curl($request) - ); - } -} diff --git a/tests/Unit/RequestToFileTest.php b/tests/Unit/RequestToFileTest.php deleted file mode 100755 index 3bbf0b1..0000000 --- a/tests/Unit/RequestToFileTest.php +++ /dev/null @@ -1,105 +0,0 @@ -vfsRoot = vfsStream::setup('root'); - vfsStream::create([ 'webroot' => [] ], $this->vfsRoot); - } - - /** - * @test - */ - public function to_file_with_default_name() - { - $request = new Request( - 'http://www.carstenwindler.de', - 'POST' - ); - - $_SERVER['DOCUMENT_ROOT'] = vfsStream::url('root/webroot'); - - $filename = request_to_file($request); - - $this->assertTrue(file_exists(vfsStream::url('root/webroot/request.http'))); - $this->assertEquals(vfsStream::url('root/webroot/request.http'), $filename); - - $this->assertEquals( - "POST / HTTP/1.1\r\nHost: www.carstenwindler.de", - file_get_contents($filename) - ); - } - - /** - * @test - */ - public function to_file_with_given_name() - { - $request = new Request( - 'http://www.carstenwindler.de', - 'POST' - ); - - $filename = request_to_file($request, vfsStream::url('root')); - - $this->assertTrue(file_exists(vfsStream::url('root/request.http'))); - $this->assertEquals(vfsStream::url('root/request.http'), $filename); - - $this->assertEquals( - "POST / HTTP/1.1\r\nHost: www.carstenwindler.de", - file_get_contents($filename) - ); - } - - /** - * @test - */ - public function to_file_will_append_if_file_exists() - { - $request = new Request( - 'http://www.carstenwindler.de', - 'POST' - ); - - $filename = request_to_file($request, vfsStream::url('root')); - - $httpRequest = file_get_contents($filename); - $expectedFile = $httpRequest; - - $filename = request_to_file($request, vfsStream::url('root')); - - $expectedFile .= "\n\n### " . date(DATE_RFC822) . "\n\n" . $httpRequest; - - $this->assertTrue(file_exists(vfsStream::url('root/request.http'))); - $this->assertEquals(vfsStream::url('root/request.http'), $filename); - - $this->assertEquals( - $expectedFile, - file_get_contents($filename) - ); - } -} diff --git a/tests/Unit/RequestToStringTest.php b/tests/Unit/RequestToStringTest.php deleted file mode 100755 index 0307047..0000000 --- a/tests/Unit/RequestToStringTest.php +++ /dev/null @@ -1,71 +0,0 @@ -assertEquals( - "POST / HTTP/1.1\r\nHost: www.carstenwindler.de", - request_to_string($request) - ); - } - - /** - * @test - */ - public function symfony_request_to_string() - { - $request = SymfonyRequest::create( - 'http://www.carstenwindler.de', - 'POST', - [], - [], - [], - // symfony adds a lot of stuff by default, which is cool, but what we don't want to test - [ - 'HTTP_ACCEPT' => null, - 'HTTP_ACCEPT_LANGUAGE' => null, - 'HTTP_ACCEPT_CHARSET' => null, - 'HTTP_USER_AGENT' => null - ] - ); - - $this->assertEquals( - "POST / HTTP/1.1\r\n" . - "Content-Type: application/x-www-form-urlencoded\r\n" . - "Host: www.carstenwindler.de\r\n\r\n", - request_to_string($request) - ); - } - - /** - * @test - */ - public function returns_unknown_if_request_is_not_supported() - { - // just some random nonsense - $request = new \stdClass; - $request->host = 'http://www.carstenwindler.de'; - - $this->assertEquals( - "unknown\r\n", - request_to_string($request) - ); - } -}