Skip to content

Commit

Permalink
Merge pull request #17 from laracasts/composite-actions
Browse files Browse the repository at this point in the history
Composite actions
  • Loading branch information
lukeraymonddowning authored Sep 25, 2024
2 parents 3bfae93 + f982fc2 commit 1910d5e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 47 deletions.
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

SESSION_DRIVER=database
SESSION_LIFETIME=120
Expand Down
61 changes: 61 additions & 0 deletions .github/actions/setup-tooling/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Setup Tooling

description: Sets up PHP with and Composer with caching.

inputs:
php-version:
description: The version(s) of PHP you want to configure.
required: false
default: '8.3'
php-extensions:
description: The PHP extensions to install.
required: false
default: dom, curl, libxml, mbstring, zip, pcntl, pdo, gd, redis, igbinary, msgpack, lzf, zstd, lz4, memcached, gmp, :php-psr
php-tools:
description: Any tools you want to include with PHP
required: false
default: composer:v2

runs:
using: composite
steps:
- name: Get extension cache hash
id: extension-cache-hash
env:
PHP_EXTENSIONS: ${{ inputs.php-extensions }}
run: echo hash=$(echo $PHP_EXTENSIONS | md5sum) >> $GITHUB_OUTPUT
shell: bash

- name: Setup cache environment
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ inputs.php-version }}
extensions: ${{ inputs.php-extensions }}
key: php-extensions-${{ steps.extension-cache-hash.outputs.hash }}

- name: Cache extensions
uses: actions/cache@v4
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: Setup PHP and Composer
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
extensions: ${{ inputs.php-extensions }}
tools: ${{ inputs.php-tools }}

- name: Get Composer cache dir
id: composer-cache-dir
run: echo dir=$(composer config cache-files-dir) >> $GITHUB_OUTPUT
shell: bash

- name: Cache dependencies
uses: actions/cache@v4.0.2
with:
key: composer-cache-${{ hashFiles('**/composer.lock') }}
path: ${{ steps.composer-cache-dir.outputs.dir }}
restore-keys: composer-cache-
39 changes: 19 additions & 20 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
name: Tests
name: Run Tests

on:
pull_request
pull_request:
branches:
- main

jobs:
run-tests:
tests:
runs-on: ubuntu-latest
env:
DB_NAME: tests

services:
redis:
image: redis:7.4
ports:
- 6379:6379
mysql:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_DATABASE: laravel
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: ${{ env.DB_NAME }}

steps:
- name: Checkout Code
uses: actions/checkout@v4.1.7

- name: Setup PHP and Composer
uses: shivammathur/setup-php@v2
- name: Setup tooling
uses: ./.github/actions/setup-tooling
with:
php-version: '8.3'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, igbinary, msgpack, lzf, zstd, lz4, memcached, gmp, :php-psr
tools: composer:v2

- name: Install Dependencies
run: composer install -q --no-interaction
- name: Install Project Dependencies
run: composer install -q --no-interaction --no-progress

- name: Copy Environment File
run: cp .env.example .env

- name: Prep Application
run: |
cp .env.example .env
php artisan key:generate
- name: Generate Application Key
run: php artisan key:generate

- name: Run Tests
env:
DB_CONNECTION: mysql
DB_DATABASE: ${{ env.DB_NAME }}
DB_USERNAME: root
run: php artisan test --compact
2 changes: 1 addition & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
Expand All @@ -21,7 +22,6 @@ class CreateNewUser implements CreatesNewUsers
*/
public function create(array $input): User
{
// A new comment!
Validator::make($input, [
'name' => ['required', 'string', 'max:255', 'nullable'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"laravel/jetstream": "^5.1",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.0"
"livewire/livewire": "^3.0",
"ext-redis": "*"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
33 changes: 17 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<!-- <env name="DB_CONNECTION" value="sqlite"/>-->
<!-- <env name="DB_DATABASE" value=":memory:"/>-->
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
10 changes: 9 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php

use App\Models\User;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;

Route::get('/', function () {
return view('welcome');
Redis::incr('landing-page-views');
$seed = Session::remember('users.seed', fn () => rand(0, 100));

return view('welcome', [
'users' => User::inRandomOrder($seed)->paginate(3),
]);
});

Route::middleware([
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/WelcomeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use App\Models\User;
use Illuminate\Support\Facades\Redis;

it('increments the page count for each visit', function () {
Redis::del('landing-page-views');

$this->get('/');
$this->get('/');
$this->get('/');

expect(Redis::get('landing-page-views'))->toEqual(3);
});

it('provides users in random paginated order', function () {
$users = User::factory(4)->create();

$users = collect($this->get('/')->viewData('users')->items())
->merge($this->get('/?page=2')->viewData('users')->items());

expect($users->count())->toBe($users->unique('id')->count());
})->repeat(3);

0 comments on commit 1910d5e

Please sign in to comment.