Skip to content

Commit

Permalink
Merge pull request #18 from BinarCode/ci
Browse files Browse the repository at this point in the history
Ci
  • Loading branch information
binaryk authored Jun 4, 2021
2 parents 8d25fa0 + a08e20d commit 1dddd15
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 85 deletions.
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/BinarCode/laravel-restify/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/BinarCode/laravel-restify/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a bug
url: https://github.com/BinarCode/laravel-restify/issues/new
about: Report a reproducable bug
36 changes: 15 additions & 21 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ name: Check & fix styling
on: [push]

jobs:
style:
runs-on: ubuntu-latest
php-cs-fixer:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v1
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Fix style
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs --allow-risky=yes
- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs.dist.php --allow-risky=yes

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Commit changes
uses: stefanzweifel/[email protected]
with:
commit_message: Fix styling
branch: ${{ steps.extract_branch.outputs.branch }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
33 changes: 33 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Psalm

on:
push:
paths:
- '**.php'
- 'psalm.xml.dist'

jobs:
psalm:
name: psalm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}

- name: Run composer install
run: composer install -n --prefer-dist

- name: Run psalm
run: ./vendor/bin/psalm --output-format=github
34 changes: 16 additions & 18 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Tests
name: run-tests

on:
push:
branches: [master]
pull_request:
types: [opened]
branches: [master]

jobs:
test:
Expand All @@ -12,16 +13,14 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.4, 8.0]
laravel: [^7.0, ^8.0]
dependency-version: [prefer-lowest, prefer-stable]
php: [8.0]
laravel: [8.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: ^7.0
testbench: ^5.2
- laravel: ^8.0
testbench: ^6.0
- laravel: 8.*
testbench: ^6.6

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
Expand All @@ -31,19 +30,18 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: vendor
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
run: composer test
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ build
composer.lock
vendor
coverage
.phpunit.result.cache
.idea
.php_cs.cache
html
output
docs/node_modules
docs/.vuepress/dist
.phpunit.result.cache
.php-cs-fixer.cache
.php_cs.cache

35 changes: 35 additions & 0 deletions .php_cs.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setFinder($finder);
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
}
],
"require": {
"php": "^7.4|^8.0"
"php": "^8.0",
"illuminate/contracts": "^8.37"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"laravel/legacy-factories": "^1.0",
"orchestra/testbench": "^5.2|^6.0",
"phpunit/phpunit": "^9.0"
"brianium/paratest": "^6.2",
"nunomaduro/collision": "^5.3",
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.9",
"vimeo/psalm": "^4.4"
},
"autoload": {
"psr-4": {
Expand All @@ -45,9 +48,9 @@
}
},
"scripts": {
"psalm": "./vendor/bin/psalm --no-cache",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
Expand Down
2 changes: 2 additions & 0 deletions config/tenantable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
'table_name' => 'tenants',

'related_tenant_column' => env('TENANTABLE_RELATED_TENANT_COLUMN', 'tenant_id'),

/**
* The base model for tenant.
*/
Expand Down
11 changes: 3 additions & 8 deletions psalm.xml → psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
errorLevel="4"
findUnusedVariablesAndParams="true"
resolveFromConfigFile="true"
useDocblockPropertyTypes="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>

<issueHandlers>
</issueHandlers>

<plugins>
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
</plugins>
</psalm>
15 changes: 10 additions & 5 deletions src/Http/Middleware/EnsureValidTenantSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

namespace BinarCode\Tenantable\Http\Middleware;

use BinarCode\Tenantable\Models\Tenant;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureValidTenantSession
{
public function handle($request, Closure $next)
public function handle(Request $request, Closure $next)
{
if ($isMasterDomain = config('tenantable.master_fqdn')) {
if ($request->getHost() === config('tenantable.master_fqdn')) {
return $next($request);
}

$sessionKey = 'ensure_valid_tenant_session_tenant_id';

/** * @var Tenant $tenant */
$tenant = app(config('tenantable.container_key', 'organization'));

if (! $request->session()->has($sessionKey)) {
$request->session()->put($sessionKey, app(config('tenantable.container_key', 'organization'))->id);
$request->session()->put($sessionKey, $tenant->key());

return $next($request);
}

if ($request->session()->get($sessionKey) !== app(config('tenantable.container_key', 'organization'))->id) {
return abort(Response::HTTP_UNAUTHORIZED);
if ($request->session()->get($sessionKey) !== $tenant->key()) {
abort(Response::HTTP_UNAUTHORIZED);
}

return $next($request);
Expand Down
8 changes: 3 additions & 5 deletions src/Models/BelongsToTenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
*/
trait BelongsToTenant
{
public static $tenantIdColumn = 'tenant_id';

public function tenant()
{
return $this->belongsTo(config('tenantable.model'), BelongsToTenant::$tenantIdColumn);
return $this->belongsTo(config('tenantable.model'), config('tenantable.related_tenant_column'));
}

public static function bootBelongsToTenant()
{
static::addGlobalScope(new TenantScope);

static::creating(function ($model) {
if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) {
if (! $model->getAttribute(config('tenantable.related_tenant_column')) && ! $model->relationLoaded('tenant')) {
if (tenant()->check()) {
$model->setAttribute(BelongsToTenant::$tenantIdColumn, tenant()->key());
$model->setAttribute(config('tenantable.related_tenant_column'), tenant()->key());
$model->setRelation('tenant', tenant());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/TenantScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TenantScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where($model->qualifyColumn(BelongsToTenant::$tenantIdColumn), tenant()->key());
$builder->where($model->qualifyColumn(config('tenantable.related_tenant_column')), tenant()->key());
}

public function extend(Builder $builder)
Expand Down
4 changes: 1 addition & 3 deletions src/Tenantable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace BinarCode\Tenantable;

use BinarCode\Tenantable\Models\BelongsToTenant;

class Tenantable
{
public static function useColumn(string $column): void
{
BelongsToTenant::$tenantIdColumn = $column;
config()->set('tenantable.related_tenant_column', $column);
}
}
2 changes: 1 addition & 1 deletion src/TenantableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/tenantable.php', 'tenantable');

$this->app->bind('tenantable', function ($app) {
$this->app->bind('tenantable', function () {
return new Tenantable();
});
}
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace BinarCode\Tenantable\Tests;

use BinarCode\Tenantable\TenantableServiceProvider;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
use WithLaravelMigrations;

public function setUp(): void
{
parent::setUp();

$this->withFactories(__DIR__.'/database/factories');

$this->setUpDatabase();
}

Expand Down
12 changes: 0 additions & 12 deletions tests/database/factories/TenantFactory.php

This file was deleted.

0 comments on commit 1dddd15

Please sign in to comment.