@@ -69,6 +69,8 @@ public function createApplication(): Application
6969
7070 public static function setUpBeforeClass (): void
7171 {
72+ self ::prepareParallelTestbenchApplication ();
73+ self ::configureParallelDatabase ();
7274 cleanupTestState (self ::applicationBasePath ());
7375 parent ::setUpBeforeClass ();
7476 }
@@ -101,12 +103,13 @@ public function setUp(): void
101103 }
102104
103105 $ loader = new ClassLoader ();
104- $ loader ->addPsr4 ('App \\' , ' vendor/orchestra/testbench-core/laravel /app ' );
106+ $ loader ->addPsr4 ('App \\' , $ this -> getBasePath () . ' /app ' );
105107 $ loader ->register ();
106108
107109 // Enforce the url for testing to be 'http://twill.test' for certain assertions.
108110 // This is different from the one in phpunit.xml because that one is used for laravel dusk.
109- $ _ENV ['APP_URL ' ] = 'http://twill.test ' ;
111+ $ _ENV ['APP_URL ' ] = $ _SERVER ['APP_URL ' ] = 'http://twill.test ' ;
112+ putenv ('APP_URL=http://twill.test ' );
110113 $ _ENV ['MEDIA_LIBRARY_LOCAL_PATH ' ] = "media-library " ;
111114 $ _ENV ["FILE_LIBRARY_LOCAL_PATH " ] = "file-library " ;
112115 $ _ENV ["FILE_LIBRARY_ENDPOINT_TYPE " ] = "local " ;
@@ -149,6 +152,7 @@ public function setUp(): void
149152 */
150153 public function configTwill ($ app ): void
151154 {
155+ $ app ['config ' ]->set ('app.url ' , 'http://twill.test ' );
152156 $ app ['config ' ]->set ('twill.admin_app_url ' , '' );
153157 $ app ['config ' ]->set ('twill.admin_app_path ' , 'twill ' );
154158 $ app ['config ' ]->set ('twill.auth_login_redirect_path ' , '/twill ' );
@@ -556,7 +560,13 @@ public function loadConfig()
556560
557561 protected static function getBasePathStatic (): string
558562 {
559- return __DIR__ . '/../../vendor/orchestra/testbench-core/laravel ' ;
563+ $ basePath = __DIR__ . '/../../vendor/orchestra/testbench-core/laravel ' ;
564+
565+ if ($ token = self ::parallelTestingToken ()) {
566+ return $ basePath . '_ ' . $ token ;
567+ }
568+
569+ return $ basePath ;
560570 }
561571
562572 public static function applicationBasePath (): string
@@ -566,6 +576,65 @@ public static function applicationBasePath(): string
566576
567577 protected function getBasePath (): string
568578 {
569- return __DIR__ . '/../../vendor/orchestra/testbench-core/laravel ' ;
579+ return self ::getBasePathStatic ();
580+ }
581+
582+ protected static function parallelTestingToken (): ?string
583+ {
584+ return $ _SERVER ['TEST_TOKEN ' ] ?? $ _ENV ['TEST_TOKEN ' ] ?? null ;
585+ }
586+
587+ protected static function prepareParallelTestbenchApplication (): void
588+ {
589+ if (! self ::parallelTestingToken ()) {
590+ return ;
591+ }
592+
593+ $ source = __DIR__ . '/../../vendor/orchestra/testbench-core/laravel ' ;
594+ $ target = self ::getBasePathStatic ();
595+
596+ if (! is_dir ($ target )) {
597+ self ::copyDirectory ($ source , $ target );
598+ }
599+ }
600+
601+ protected static function configureParallelDatabase (): void
602+ {
603+ if (! self ::parallelTestingToken () || ($ _ENV ['DB_CONNECTION ' ] ?? null ) !== 'mysql ' ) {
604+ return ;
605+ }
606+
607+ $ database = preg_replace ('/_\d+$/ ' , '' , $ _ENV ['DB_DATABASE ' ]) . '_ ' . self ::parallelTestingToken ();
608+ $ _ENV ['DB_DATABASE ' ] = $ _SERVER ['DB_DATABASE ' ] = $ database ;
609+ putenv ("DB_DATABASE= {$ database }" );
610+
611+ $ host = $ _ENV ['DB_HOST ' ] ?? '127.0.0.1 ' ;
612+ $ port = $ _ENV ['DB_PORT ' ] ?? '3306 ' ;
613+ $ username = $ _ENV ['DB_USERNAME ' ] ?? 'root ' ;
614+ $ password = $ _ENV ['DB_PASSWORD ' ] ?? '' ;
615+
616+ $ pdo = new \PDO ("mysql:host= {$ host };port= {$ port }" , $ username , $ password );
617+ $ pdo ->exec ("DROP DATABASE IF EXISTS ` {$ database }` " );
618+ $ pdo ->exec ("CREATE DATABASE ` {$ database }` " );
619+ }
620+
621+ protected static function copyDirectory (string $ source , string $ target ): void
622+ {
623+ mkdir ($ target , 0777 , true );
624+
625+ foreach (scandir ($ source ) as $ item ) {
626+ if ($ item === '. ' || $ item === '.. ' ) {
627+ continue ;
628+ }
629+
630+ $ sourcePath = $ source . DIRECTORY_SEPARATOR . $ item ;
631+ $ targetPath = $ target . DIRECTORY_SEPARATOR . $ item ;
632+
633+ if (is_dir ($ sourcePath ) && ! is_link ($ sourcePath )) {
634+ self ::copyDirectory ($ sourcePath , $ targetPath );
635+ } else {
636+ copy ($ sourcePath , $ targetPath );
637+ }
638+ }
570639 }
571640}
0 commit comments