Skip to content

Commit

Permalink
PHP 8 support, cookie unit tests (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
luceos committed Jan 26, 2021
1 parent 08f72e7 commit 65b5c20
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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_]

Expand All @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/Http/CookieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct(Config $config)
* @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);

Expand Down Expand Up @@ -108,7 +108,7 @@ public function make($name, $value = null, $maxAge = null)
* @param string $name
* @return \Dflydev\FigCookies\SetCookie
*/
public function expire($name)
public function expire(string $name): SetCookie
{
return $this->make($name)->expire();
}
Expand All @@ -119,7 +119,7 @@ public function expire($name)
* @param string $name
* @return string
*/
public function getName($name)
public function getName(string $name): string
{
return $this->prefix.'_'.$name;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/api/users/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function users_can_see_their_private_information()

// 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());
}

/**
Expand All @@ -61,6 +61,6 @@ public function users_can_not_see_other_users_private_information()

// 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());
}
}
2 changes: 1 addition & 1 deletion tests/integration/extenders/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function custom_relationship_exists_and_can_return_instances_if_added()
$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()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Foundation/ContainerUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ContainerUtilTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
65 changes: 65 additions & 0 deletions tests/unit/Http/CookieFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\unit\Http;

use Carbon\Carbon;
use Flarum\Foundation\Config;
use Flarum\Http\CookieFactory;
use Flarum\Tests\unit\TestCase;

class CookieFactoryTest extends TestCase
{
protected function factory(array $config = null): CookieFactory
{
$config = new Config(array_merge([
'url' => '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());
}
}

0 comments on commit 65b5c20

Please sign in to comment.