Skip to content

Commit 1910d5e

Browse files
Merge pull request #17 from laracasts/composite-actions
Composite actions
2 parents 3bfae93 + f982fc2 commit 1910d5e

File tree

9 files changed

+140
-47
lines changed

9 files changed

+140
-47
lines changed

.env.example

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ LOG_STACK=single
1919
LOG_DEPRECATIONS_CHANNEL=null
2020
LOG_LEVEL=debug
2121

22-
DB_CONNECTION=sqlite
23-
# DB_HOST=127.0.0.1
24-
# DB_PORT=3306
25-
# DB_DATABASE=laravel
26-
# DB_USERNAME=root
27-
# DB_PASSWORD=
22+
DB_CONNECTION=mysql
23+
DB_HOST=127.0.0.1
24+
DB_PORT=3306
25+
DB_DATABASE=laravel
26+
DB_USERNAME=root
27+
DB_PASSWORD=
2828

2929
SESSION_DRIVER=database
3030
SESSION_LIFETIME=120
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Setup Tooling
2+
3+
description: Sets up PHP with and Composer with caching.
4+
5+
inputs:
6+
php-version:
7+
description: The version(s) of PHP you want to configure.
8+
required: false
9+
default: '8.3'
10+
php-extensions:
11+
description: The PHP extensions to install.
12+
required: false
13+
default: dom, curl, libxml, mbstring, zip, pcntl, pdo, gd, redis, igbinary, msgpack, lzf, zstd, lz4, memcached, gmp, :php-psr
14+
php-tools:
15+
description: Any tools you want to include with PHP
16+
required: false
17+
default: composer:v2
18+
19+
runs:
20+
using: composite
21+
steps:
22+
- name: Get extension cache hash
23+
id: extension-cache-hash
24+
env:
25+
PHP_EXTENSIONS: ${{ inputs.php-extensions }}
26+
run: echo hash=$(echo $PHP_EXTENSIONS | md5sum) >> $GITHUB_OUTPUT
27+
shell: bash
28+
29+
- name: Setup cache environment
30+
id: extcache
31+
uses: shivammathur/cache-extensions@v1
32+
with:
33+
php-version: ${{ inputs.php-version }}
34+
extensions: ${{ inputs.php-extensions }}
35+
key: php-extensions-${{ steps.extension-cache-hash.outputs.hash }}
36+
37+
- name: Cache extensions
38+
uses: actions/cache@v4
39+
with:
40+
path: ${{ steps.extcache.outputs.dir }}
41+
key: ${{ steps.extcache.outputs.key }}
42+
restore-keys: ${{ steps.extcache.outputs.key }}
43+
44+
- name: Setup PHP and Composer
45+
uses: shivammathur/setup-php@v2
46+
with:
47+
php-version: ${{ inputs.php-version }}
48+
extensions: ${{ inputs.php-extensions }}
49+
tools: ${{ inputs.php-tools }}
50+
51+
- name: Get Composer cache dir
52+
id: composer-cache-dir
53+
run: echo dir=$(composer config cache-files-dir) >> $GITHUB_OUTPUT
54+
shell: bash
55+
56+
- name: Cache dependencies
57+
uses: actions/[email protected]
58+
with:
59+
key: composer-cache-${{ hashFiles('**/composer.lock') }}
60+
path: ${{ steps.composer-cache-dir.outputs.dir }}
61+
restore-keys: composer-cache-

.github/workflows/tests.yml

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
name: Tests
1+
name: Run Tests
22

33
on:
4-
pull_request
4+
pull_request:
5+
branches:
6+
- main
57

68
jobs:
7-
run-tests:
9+
tests:
810
runs-on: ubuntu-latest
9-
env:
10-
DB_NAME: tests
1111

1212
services:
13+
redis:
14+
image: redis:7.4
15+
ports:
16+
- 6379:6379
1317
mysql:
1418
image: mysql:8.0
1519
ports:
1620
- 3306:3306
1721
env:
22+
MYSQL_DATABASE: laravel
1823
MYSQL_ALLOW_EMPTY_PASSWORD: yes
19-
MYSQL_DATABASE: ${{ env.DB_NAME }}
2024

2125
steps:
2226
- name: Checkout Code
2327
uses: actions/[email protected]
2428

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

32-
- name: Install Dependencies
33-
run: composer install -q --no-interaction
34+
- name: Install Project Dependencies
35+
run: composer install -q --no-interaction --no-progress
36+
37+
- name: Copy Environment File
38+
run: cp .env.example .env
3439

35-
- name: Prep Application
36-
run: |
37-
cp .env.example .env
38-
php artisan key:generate
40+
- name: Generate Application Key
41+
run: php artisan key:generate
3942

4043
- name: Run Tests
41-
env:
42-
DB_CONNECTION: mysql
43-
DB_DATABASE: ${{ env.DB_NAME }}
44-
DB_USERNAME: root
4544
run: php artisan test --compact

app/Actions/Fortify/CreateNewUser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\User;
77
use Illuminate\Support\Facades\DB;
88
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Support\Facades\Redis;
910
use Illuminate\Support\Facades\Validator;
1011
use Laravel\Fortify\Contracts\CreatesNewUsers;
1112
use Laravel\Jetstream\Jetstream;
@@ -21,7 +22,6 @@ class CreateNewUser implements CreatesNewUsers
2122
*/
2223
public function create(array $input): User
2324
{
24-
// A new comment!
2525
Validator::make($input, [
2626
'name' => ['required', 'string', 'max:255', 'nullable'],
2727
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"laravel/jetstream": "^5.1",
1111
"laravel/sanctum": "^4.0",
1212
"laravel/tinker": "^2.9",
13-
"livewire/livewire": "^3.0"
13+
"livewire/livewire": "^3.0",
14+
"ext-redis": "*"
1415
},
1516
"require-dev": {
1617
"fakerphp/faker": "^1.23",

composer.lock

+17-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
2323
<env name="BCRYPT_ROUNDS" value="4"/>
2424
<env name="CACHE_STORE" value="array"/>
25-
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
26-
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
25+
<!-- <env name="DB_CONNECTION" value="sqlite"/>-->
26+
<!-- <env name="DB_DATABASE" value=":memory:"/>-->
2727
<env name="MAIL_MAILER" value="array"/>
2828
<env name="PULSE_ENABLED" value="false"/>
2929
<env name="QUEUE_CONNECTION" value="sync"/>

routes/web.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<?php
22

3+
use App\Models\User;
34
use Illuminate\Support\Facades\Route;
5+
use Illuminate\Support\Facades\Redis;
6+
use Illuminate\Support\Facades\Session;
47

58
Route::get('/', function () {
6-
return view('welcome');
9+
Redis::incr('landing-page-views');
10+
$seed = Session::remember('users.seed', fn () => rand(0, 100));
11+
12+
return view('welcome', [
13+
'users' => User::inRandomOrder($seed)->paginate(3),
14+
]);
715
});
816

917
Route::middleware([

tests/Feature/WelcomeTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use App\Models\User;
4+
use Illuminate\Support\Facades\Redis;
5+
6+
it('increments the page count for each visit', function () {
7+
Redis::del('landing-page-views');
8+
9+
$this->get('/');
10+
$this->get('/');
11+
$this->get('/');
12+
13+
expect(Redis::get('landing-page-views'))->toEqual(3);
14+
});
15+
16+
it('provides users in random paginated order', function () {
17+
$users = User::factory(4)->create();
18+
19+
$users = collect($this->get('/')->viewData('users')->items())
20+
->merge($this->get('/?page=2')->viewData('users')->items());
21+
22+
expect($users->count())->toBe($users->unique('id')->count());
23+
})->repeat(3);

0 commit comments

Comments
 (0)