From 36ac473f45e4d7064ac4cabd5aade66314aec6ee Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 6 Nov 2024 00:16:40 -0300 Subject: [PATCH 1/3] Hey-3: shold check if ends with question mark? --- app/Http/Controllers/QuestionController.php | 10 +++++++++- tests/Feature/CreateAQuestionTest.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/QuestionController.php b/app/Http/Controllers/QuestionController.php index 221f25e..d3918b4 100644 --- a/app/Http/Controllers/QuestionController.php +++ b/app/Http/Controllers/QuestionController.php @@ -11,7 +11,15 @@ public function store(Request $request): RedirectResponse { $atributes = request()->validate([ - 'question' => ['required', 'min:10'], + 'question' => [ + 'required', + 'min:10', + function (string $atribute, mixed $value, callable $fail) { + if ($value[strlen($value) - 1] !== '?') { + $fail('Are you sure it is a question? It should end with a question mark in the end.'); + } + }, + ], ]); Question::query()->create($atributes); diff --git a/tests/Feature/CreateAQuestionTest.php b/tests/Feature/CreateAQuestionTest.php index da34752..a658992 100644 --- a/tests/Feature/CreateAQuestionTest.php +++ b/tests/Feature/CreateAQuestionTest.php @@ -24,6 +24,19 @@ it('shold check if ends with question mark?', function () { + //Arrange::preparar + $user = User::factory()->create(); + actingAs($user); + + //Act::agir + $request = post(route('questions.store'), [ + 'question' => str_repeat('#', 10), + ]); + + //Assert::verificar + $request->assertSessionHasErrors(['question' => 'Are you sure it is a question? It should end with a question mark in the end.']); + assertDatabaseCount('questions', 0); + }); it('should have at least 10 characters', function () { From 881d89cac1b8f4834b84e07e147d03f2feacb2b0 Mon Sep 17 00:00:00 2001 From: Gabriel Brenno <97069315+gaabrenno@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:41:25 -0300 Subject: [PATCH 2/3] Update laravel.yml --- .github/workflows/laravel.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index f39e985..9ff2e9e 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -25,20 +25,25 @@ jobs: run: | mkdir -p database touch database/database.sqlite + - name: Clean Node Modules and Lock File run: | rm -rf node_modules package-lock.json - name: Installing Package.json run: npm install + - name: Build Assets run: npm run build + - name: Execute tests (Unit and Feature tests) via Pest env: DB_CONNECTION: sqlite DB_DATABASE: database/database.sqlite run: php artisan test --parallel + - name: Execute Larastan run: vendor/bin/phpstan analyse + - name: Execute Pint run: vendor/bin/pint --test From 030d6dc94c0e7fcd4bff7420bdaf3f7df6cee6ce Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 6 Nov 2024 12:41:15 -0300 Subject: [PATCH 3/3] add husky precommit --- .husky/pre-commit | 35 +++++++++++++++++++++++++++++++++++ package-lock.json | 17 +++++++++++++++++ package.json | 34 ++++++++++++++++++---------------- 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..b473e41 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,35 @@ +#!/bin/bash + +# Executa o PHPStan para análise estática do código +echo "Executando PHPStan para análise estática..." +./vendor/bin/phpstan analyse --memory-limit=512M + +# Verifica o status de saída do PHPStan +if [ $? -ne 0 ]; then + echo "hehe Deu ruim cumpadi.... Erros encontrados pelo PHPStan. Corrija-os antes de realizar o commit." + exit 1 +fi + +# Executa o PHPUnit para testes automatizados em parallel +echo "Executando textes em paralelo..." +php artisan test --parallel +if [ $? -ne 0 ]; then + echo "hehe Deu ruim cumpadi.... 1 ou mais testes deram errado. Corrija-os antes de realizar o commit." + exit 1 +fi + +# Executa o Laravel Pint com correção automática +echo "Executando Laravel Pint para corrigir o código..." +./vendor/bin/pint + +# Verifica se houve alterações após a execução do Pint +if [ -n "$(git status --porcelain)" ]; then + echo "O Laravel Pint corrigiu os seguintes arquivos:" + git status --porcelain + + # Adiciona as alterações corrigidas ao commit + git add -u + echo "As alterações corrigidas foram adicionadas ao commit." +else + echo "Laravel Pint executado com sucesso, sem alterações necessárias!" +fi diff --git a/package-lock.json b/package-lock.json index ab67179..a96f4d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "alpinejs": "^3.4.2", "autoprefixer": "^10.4.2", "axios": "^1.7.4", + "husky": "^9.1.6", "laravel-vite-plugin": "^1.0.5", "postcss": "^8.4.31", "tailwindcss": "^3.1.0", @@ -824,6 +825,22 @@ "node": ">= 0.4" } }, + "node_modules/husky": { + "version": "9.1.6", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.6.tgz", + "integrity": "sha512-sqbjZKK7kf44hfdE94EoX8MZNk0n7HeW37O4YrVGCF4wzgQjp+akPAkfUK5LZ6KuR/6sqeAVuXHji+RzQgOn5A==", + "dev": true, + "license": "MIT", + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", diff --git a/package.json b/package.json index 51f7399..e41aea8 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,20 @@ { - "private": true, - "type": "module", - "scripts": { - "dev": "vite", - "build": "vite build" - }, - "devDependencies": { - "@tailwindcss/forms": "^0.5.2", - "alpinejs": "^3.4.2", - "autoprefixer": "^10.4.2", - "axios": "^1.7.4", - "laravel-vite-plugin": "^1.0.5", - "postcss": "^8.4.31", - "tailwindcss": "^3.1.0", - "vite": "^5.0" - } + "private": true, + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "prepare": "husky" + }, + "devDependencies": { + "@tailwindcss/forms": "^0.5.2", + "alpinejs": "^3.4.2", + "autoprefixer": "^10.4.2", + "axios": "^1.7.4", + "husky": "^9.1.6", + "laravel-vite-plugin": "^1.0.5", + "postcss": "^8.4.31", + "tailwindcss": "^3.1.0", + "vite": "^5.0" + } }