From 8fdddf91a2646e5682f2d16f2c8263a408b9b01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Tue, 26 Jan 2021 23:53:28 +0100 Subject: [PATCH] PHP 8 support, cookie unit tests (#2507) --- framework/core/.github/workflows/test.yml | 8 ++- framework/core/composer.json | 20 +++--- framework/core/src/Http/CookieFactory.php | 6 +- .../integration/api/users/UpdateTest.php | 4 +- .../tests/integration/extenders/ModelTest.php | 2 +- .../unit/Foundation/ContainerUtilTest.php | 2 +- .../tests/unit/Http/CookieFactoryTest.php | 65 +++++++++++++++++++ 7 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 framework/core/tests/unit/Http/CookieFactoryTest.php diff --git a/framework/core/.github/workflows/test.yml b/framework/core/.github/workflows/test.yml index 57ce686b1..6204c2c8f 100644 --- a/framework/core/.github/workflows/test.yml +++ b/framework/core/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - php: [7.2, 7.3, 7.4] + php: [7.2, 7.3, 7.4, '8.0'] service: ['mysql:5.7', mariadb] prefix: ['', flarum_] @@ -33,6 +33,12 @@ jobs: - php: 7.3 service: mariadb prefix: flarum_ + - php: 8.0 + service: 'mysql:5.7' + prefix: flarum_ + - php: 8.0 + service: mariadb + prefix: flarum_ services: mysql: diff --git a/framework/core/composer.json b/framework/core/composer.json index c52546437..de9e79f4d 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -42,9 +42,9 @@ "php": ">=7.2", "axy/sourcemap": "^0.1.4", "components/font-awesome": "^5.14.0", - "dflydev/fig-cookies": "^2.0.1", + "dflydev/fig-cookies": "^3.0.0", "doctrine/dbal": "^2.7", - "franzl/whoops-middleware": "^0.4.0", + "franzl/whoops-middleware": "^2.0.0", "illuminate/bus": "^6.0", "illuminate/cache": "^6.0", "illuminate/config": "^6.0", @@ -61,14 +61,14 @@ "illuminate/validation": "^6.0", "illuminate/view": "^6.0", "intervention/image": "^2.5.0", - "laminas/laminas-diactoros": "^1.8.4", - "laminas/laminas-httphandlerrunner": "^1.0", - "laminas/laminas-stratigility": "^3.0", + "laminas/laminas-diactoros": "^2.4.1", + "laminas/laminas-httphandlerrunner": "^1.2.0", + "laminas/laminas-stratigility": "^3.2.2", "league/flysystem": "^1.0.11", "matthiasmullie/minify": "^1.3", - "middlewares/base-path": "^1.1", - "middlewares/base-path-router": "^0.2.1", - "middlewares/request-handler": "^1.2", + "middlewares/base-path": "^2.0.1", + "middlewares/base-path-router": "^2.0.1", + "middlewares/request-handler": "^2.0.1", "monolog/monolog": "^1.16.0", "nesbot/carbon": "^2.0", "nikic/fast-route": "^0.6", @@ -86,8 +86,8 @@ "wikimedia/less.php": "^3.0" }, "require-dev": { - "mockery/mockery": "^1.0", - "phpunit/phpunit": "^7.0" + "mockery/mockery": "^1.3.3", + "phpunit/phpunit": "^8.0" }, "autoload": { "psr-4": { diff --git a/framework/core/src/Http/CookieFactory.php b/framework/core/src/Http/CookieFactory.php index 5c36cb39b..b0ee9fd60 100644 --- a/framework/core/src/Http/CookieFactory.php +++ b/framework/core/src/Http/CookieFactory.php @@ -77,7 +77,7 @@ class CookieFactory * @param int $maxAge * @return \Dflydev\FigCookies\SetCookie */ - public function make($name, $value = null, $maxAge = null) + public function make(string $name, string $value = null, int $maxAge = null): SetCookie { $cookie = SetCookie::create($this->getName($name), $value); @@ -108,7 +108,7 @@ class CookieFactory * @param string $name * @return \Dflydev\FigCookies\SetCookie */ - public function expire($name) + public function expire(string $name): SetCookie { return $this->make($name)->expire(); } @@ -119,7 +119,7 @@ class CookieFactory * @param string $name * @return string */ - public function getName($name) + public function getName(string $name): string { return $this->prefix.'_'.$name; } diff --git a/framework/core/tests/integration/api/users/UpdateTest.php b/framework/core/tests/integration/api/users/UpdateTest.php index 280554b81..91e65af5c 100644 --- a/framework/core/tests/integration/api/users/UpdateTest.php +++ b/framework/core/tests/integration/api/users/UpdateTest.php @@ -44,7 +44,7 @@ class UpdateTest extends TestCase // Test for successful response and that the email is included in the response $this->assertEquals(200, $response->getStatusCode()); - $this->assertContains('normal@machine.local', (string) $response->getBody()); + $this->assertStringContainsString('normal@machine.local', (string) $response->getBody()); } /** @@ -61,6 +61,6 @@ class UpdateTest extends TestCase // Make sure sensitive information is not made public $this->assertEquals(200, $response->getStatusCode()); - $this->assertNotContains('admin@machine.local', (string) $response->getBody()); + $this->assertStringNotContainsString('admin@machine.local', (string) $response->getBody()); } } diff --git a/framework/core/tests/integration/extenders/ModelTest.php b/framework/core/tests/integration/extenders/ModelTest.php index f40c71d90..c198f9922 100644 --- a/framework/core/tests/integration/extenders/ModelTest.php +++ b/framework/core/tests/integration/extenders/ModelTest.php @@ -174,7 +174,7 @@ class ModelTest extends TestCase $user = User::find(1); $this->assertNotEquals([], $user->customRelation()->get()->toArray()); - $this->assertContains(json_encode(__CLASS__), json_encode($user->customRelation()->get())); + $this->assertStringContainsString(json_encode(__CLASS__), json_encode($user->customRelation()->get())); } /** diff --git a/framework/core/tests/unit/Foundation/ContainerUtilTest.php b/framework/core/tests/unit/Foundation/ContainerUtilTest.php index d90764f3b..96723ecb4 100644 --- a/framework/core/tests/unit/Foundation/ContainerUtilTest.php +++ b/framework/core/tests/unit/Foundation/ContainerUtilTest.php @@ -20,7 +20,7 @@ class ContainerUtilTest extends TestCase /** * @inheritDoc */ - protected function setUp() + protected function setUp(): void { parent::setUp(); diff --git a/framework/core/tests/unit/Http/CookieFactoryTest.php b/framework/core/tests/unit/Http/CookieFactoryTest.php new file mode 100644 index 000000000..163baf884 --- /dev/null +++ b/framework/core/tests/unit/Http/CookieFactoryTest.php @@ -0,0 +1,65 @@ + 'http://flarum.test' + ], $config ?? [])); + + return new CookieFactory($config); + } + + /** @test */ + public function can_create_cookies() + { + $cookie = $this->factory()->make('test', 'australia'); + + $this->assertEquals('flarum_test', $cookie->getName()); + $this->assertEquals('australia', $cookie->getValue()); + $this->assertEquals(0, $cookie->getExpires()); + $this->assertFalse($cookie->getSecure()); + $this->assertEquals('/', $cookie->getPath()); + } + + /** @test */ + public function can_override_cookie_settings_from_config() + { + $cookie = $this->factory([ + 'cookie' => [ + 'name' => 'australia', + 'secure' => true, + 'domain' => 'flarum.com', + 'samesite' => 'none' + ] + ])->make('test', 'australia'); + + $this->assertEquals('australia_test', $cookie->getName()); + $this->assertTrue($cookie->getSecure()); + $this->assertEquals('flarum.com', $cookie->getDomain()); + $this->assertEquals('SameSite=None', $cookie->getSameSite()->asString()); + } + + /** @test */ + public function can_expire_cookies() + { + $cookie = $this->factory()->expire('test'); + + $this->assertLessThan(Carbon::now()->timestamp, $cookie->getExpires()); + } +}