Skip to content

Commit 636c1b5

Browse files
committed
Fix constraints, add tests & GitHub Actions.
1 parent eb63e6c commit 636c1b5

File tree

9 files changed

+245
-5
lines changed

9 files changed

+245
-5
lines changed

.github/workflows/run_tests.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
php: [7.1, 7.2, 7.3, 7.4, 8.0]
12+
laravel: [5.6.*, 5.7.*, 5.8.*, 6.*]
13+
dependency-version: [prefer-stable, prefer-lowest]
14+
include:
15+
- laravel: 5.6.*
16+
testbench: 3.6.*
17+
php: 7.1.*
18+
- laravel: 5.7.*
19+
testbench: 3.7.*
20+
php: 7.1.*
21+
- laravel: 5.8.*
22+
testbench: 3.8.*
23+
php: 7.1.*
24+
- laravel: 6.*
25+
testbench: 4.*
26+
exclude:
27+
- laravel: 6.*
28+
php: 7.1
29+
30+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Testbench ${{ matrix.testbench }} (${{ matrix.dependency-version }})
31+
32+
steps:
33+
34+
- name: Checkout
35+
uses: actions/checkout@v2
36+
37+
- name: Cache dependencies
38+
uses: actions/cache@v1
39+
with:
40+
path: ~/.composer/cache/files
41+
key: dependencies-php-${{ matrix.php }}-composer-${{ matrix.laravel }}-${{ matrix.testbench }}-${{ matrix.dependency-version }}
42+
43+
- name: Setup PHP
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: ${{ matrix.php }}
47+
extensions: mbstring, zip
48+
coverage: pcov
49+
50+
- name: Install Composer dependencies
51+
run: |
52+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
53+
composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist --no-suggest
54+
55+
- name: PHPUnit Testing
56+
run: vendor/bin/phpunit

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
composer.lock
2-
vendor
2+
.phpunit.result.cache
3+
vendor
4+
.idea

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
"email": "green2go@gmail.com"
1010
}
1111
],
12-
"minimum-stability": "dev",
1312
"prefer-stable": true,
1413
"require": {
15-
"laravel/framework": "^5.6|^6.0",
14+
"php": "^7.1.3",
15+
"illuminate/contracts": "^5.6|^6.0",
16+
"illuminate/http": "^5.6|^6.0",
1617
"s1lentium/iptools": "^1.1"
1718
},
1819
"require-dev": {
19-
"orchestra/testbench": "^3.5",
20+
"orchestra/testbench": "^3.6|^3.7|^3.8|^4.0",
2021
"symfony/thanks": "^1.0"
2122
},
2223
"autoload": {

phpunit.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">./src</directory>
21+
<exclude>
22+
<directory>./vendor</directory>
23+
</exclude>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

src/Repositories/ConfigRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use BoxedCode\Laravel\Auth\Ip\Contracts\Repository;
66
use IPTools\IP;
7-
use IPTools\Network;
87
use IPTools\Range;
98
use Exception;
109

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests\Integration;
4+
5+
use BoxedCode\Laravel\Auth\Challenge\Contracts\Challenge;
6+
use Illuminate\Encryption\Encrypter;
7+
8+
class EnforcingMiddlewareTestCase extends TestCase
9+
{
10+
/**
11+
* Setup the test environment.
12+
*/
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
}
17+
18+
public function testDefaultConfigurationDenied()
19+
{
20+
$response = $this->actingAs($this->testUser)->get('/');
21+
22+
$response->assertForbidden();
23+
}
24+
25+
public function testWhitelistAuthorized()
26+
{
27+
config()->set('ip_auth.addresses.whitelist', ['127.0.0.1']);
28+
29+
$response = $this->actingAs($this->testUser)->get('/');
30+
31+
$response->assertOk();
32+
}
33+
34+
public function testBlacklistOverridesWhitelist()
35+
{
36+
config()->set('ip_auth.addresses.whitelist', ['127.0.0.1']);
37+
config()->set('ip_auth.addresses.blacklist', ['127.0.0.1']);
38+
39+
$response = $this->actingAs($this->testUser)->get('/');
40+
41+
$response->assertForbidden();
42+
}
43+
44+
public function testDefaultActionAllow()
45+
{
46+
config()->set('ip_auth.default_action', 'allow');
47+
48+
$response = $this->actingAs($this->testUser)->get('/');
49+
50+
$response->assertOk();
51+
}
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tests\Integration\Support;
4+
5+
use Illuminate\Foundation\Auth\AuthenticatesUsers;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Routing\Controller as BaseController;
8+
9+
class Controller extends BaseController
10+
{
11+
use AuthenticatesUsers;
12+
13+
public function home(Request $request)
14+
{
15+
return 'Hello '.$request->user()->name.'!';
16+
}
17+
}

tests/Integration/Support/User.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Tests\Integration\Support;
4+
5+
use Illuminate\Foundation\Auth\User as BaseUser;
6+
7+
class User extends BaseUser
8+
{
9+
protected $table = 'users';
10+
11+
protected $fillable = ['name', 'email', 'password'];
12+
}

tests/Integration/TestCase.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Tests\Integration;
4+
5+
use Tests\Integration\Support\User;
6+
7+
abstract class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
public $testUser;
10+
11+
/**
12+
* Get package providers. At a minimum this is the package being tested, but also
13+
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
14+
* In a normal app environment these would be added to the 'providers' array in
15+
* the config/app.php file.
16+
*
17+
* @param \Illuminate\Foundation\Application $app
18+
*
19+
* @return array
20+
*/
21+
protected function getPackageProviders($app)
22+
{
23+
return [
24+
\BoxedCode\Laravel\Auth\Ip\IpAuthServiceProvider::class,
25+
];
26+
}
27+
28+
/**
29+
* Setup the test environment.
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
// Run the migrations.
36+
$this->loadLaravelMigrations(['--database' => 'testing']);
37+
$this->artisan('migrate', ['--database' => 'testing']);
38+
39+
// Create a test user.
40+
$this->testUser = User::create([
41+
'name' => 'Test User',
42+
'email' => 'test@user.com',
43+
'password' => 'password',
44+
]);
45+
46+
\Route::get('/', '\Tests\Integration\Support\Controller@home')->middleware([
47+
'auth',
48+
\BoxedCode\Laravel\Auth\Ip\Middleware\RequireIpAuthorization::class,
49+
]);
50+
51+
\Route::get('/logout', '\Tests\Integration\Support\Controller@logout')->name('logout');
52+
}
53+
54+
/**
55+
* Define environment setup.
56+
*
57+
* @param \Illuminate\Foundation\Application $app
58+
*
59+
* @return void
60+
*/
61+
protected function getEnvironmentSetUp($app)
62+
{
63+
// Setup default database to use sqlite :memory:
64+
$app['config']->set('database.default', 'testing');
65+
$app['config']->set('database.connections.testing', [
66+
'driver' => 'sqlite',
67+
'database' => ':memory:',
68+
'prefix' => '',
69+
]);
70+
71+
// Set default user model.
72+
$app['config']->set('auth.providers.users.model', User::class);
73+
//$app['config']->set()
74+
}
75+
}

0 commit comments

Comments
 (0)