Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
DominikStyp / AppServiceProvider.php
Last active August 17, 2023 15:20
Laravel 10: How to replace the FlySystem FtpAdapter and supress/catch FTP Warnings/Errors
<?php
namespace App\Providers;
use App\Contracts\BrandNameResolverInterface as BrandNameResolverServiceContract;
use App\Http\Controllers\Auth\PasswordBrokerManager;
use App\Repositories\Interfaces\AccountInterface;
use App\Services\Filesystem\FtpAdapter;
use App\Services\User\AccountSessionService;
use App\View\Components\Modal;
@DominikStyp
DominikStyp / callArtisanWithOptions.php
Created August 7, 2023 15:51
Laravel 10 + PHPUnit: Call artisan command with options
<?php
class CallArtisanWithOptions {
public function test_migrate_documents(): void
{
$this->artisan('app:migrate-ftp-documents-to-local-storage')
->expectsChoice(
'Are you sure you want to migrate all documents from FTP to local storage?',
'yes',
['yes', 'no']
)->expectsChoice(
@DominikStyp
DominikStyp / FilterOnlyAvailable.php
Created July 31, 2023 10:34
Laravel 10: DTO filter only available fields and log error if there are more passed than should be
<?php
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use ReflectionClass;
use ReflectionProperty;
trait FilterOnlyAvailable
{
/**
@DominikStyp
DominikStyp / raw_query_example_tinker.php
Created July 26, 2023 14:51
Laravel 10 raw query example
<?php
// now raw() returns an expression which requires Grammar as argument, but we can provide grammar used for the current connection
DB::select(DB::raw("SELECT DISTINCT(company_size) FROM accounts WHERE company_size <> ''")->getValue(DB::getSchemaGrammar()));
@DominikStyp
DominikStyp / MailableTest.php
Created May 25, 2023 09:14
Laravel: how to render plain text (raw text) view in the blade (PHPUnit)
<?php
namespace App\Mail;
use Illuminate\Mail\Mailable as MailableBase;
class Mailable extends MailableBase {
/**
* this returns the plain text view for the e-mail in tests
@DominikStyp
DominikStyp / BladeViewCorrectSyntaxTest.php
Created May 25, 2023 09:11
Laravel: how to check if view blade syntax is correct and section tags are properly closed (PHPUnit)
<?php
class BladeViewCorrectSyntaxTest {
/**
* @see https://stackoverflow.com/questions/38400305/phpunit-help-needed-about-risky-tests
*/
public function view_test()
{
$viewName = 'some.view';
@DominikStyp
DominikStyp / NumberFacade.php
Created May 11, 2023 17:11
Laravel: mocking facade methods
<?php
namespace App\Facades;
use App\Services\Common\NumberService;
use Illuminate\Support\Facades\Facade;
class NumberFacade extends Facade {
// here we fetch NumberService from the container
@DominikStyp
DominikStyp / migration.php
Created April 27, 2023 09:10
Laravel 10 migration error: SQLSTATE[HY000]: General error: 1005 Can't create table 'sgg.#sql-1_1b' (errno: 150) (Connection: mysql, SQL: alter table field_settings add constraint field_settings_field_key_foreign foreign key (field_key) references editable_fields (key) on delete cascade)
<?php
Schema::create('editable_fields', function (Blueprint $table) {
$table->increments('id');
// instead of
// $table->string('key');
// add an index to the referenced field
$table->string('key')->index();
});
@DominikStyp
DominikStyp / migration.php
Last active April 27, 2023 08:29
Larave l10 dump sql in migration
<?php
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('field_settings', function (Blueprint $table) {
@DominikStyp
DominikStyp / CookieHandleMiddleware.php
Created December 22, 2022 17:17
Laravel Middleware: set and get cookie in the middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\MessageBag;
class CookieHandleMiddleware