2020-04-10 20:32:12 +02:00
< ? php
namespace Tests ;
2022-08-12 22:07:59 +02:00
use App\Group ;
2021-11-19 00:15:20 +01:00
use App\Member\Member ;
2022-02-19 15:18:24 +01:00
use App\Setting\NamiSettings ;
use App\User ;
2020-04-10 20:32:12 +02:00
use Illuminate\Foundation\Testing\TestCase as BaseTestCase ;
2022-02-19 15:18:24 +01:00
use Illuminate\Http\RedirectResponse ;
2024-04-26 23:20:03 +02:00
use Illuminate\Support\Arr ;
2023-02-05 23:35:08 +01:00
use Illuminate\Support\Facades\Http ;
2023-12-16 01:13:49 +01:00
use Illuminate\Testing\AssertableJsonString ;
2021-07-17 15:58:38 +02:00
use Illuminate\Testing\TestResponse ;
2022-10-18 15:04:47 +02:00
use Phake ;
2023-12-16 01:13:49 +01:00
use PHPUnit\Framework\Assert ;
2023-02-23 22:43:13 +01:00
use Tests\Lib\MakesHttpCalls ;
2022-02-12 00:41:52 +01:00
use Tests\Lib\TestsInertia ;
2022-02-19 15:18:24 +01:00
use Zoomyboy\LaravelNami\Authentication\Auth ;
2020-04-10 20:32:12 +02:00
abstract class TestCase extends BaseTestCase
{
use CreatesApplication ;
2022-02-12 00:41:52 +01:00
use TestsInertia ;
2023-02-23 22:43:13 +01:00
use MakesHttpCalls ;
2021-06-13 11:30:31 +02:00
2022-02-19 15:18:24 +01:00
protected User $me ;
public function setUp () : void
2022-02-12 15:33:16 +01:00
{
2022-02-19 15:18:24 +01:00
parent :: setUp ();
Auth :: fake ();
2023-05-16 17:19:56 +02:00
Member :: disableGeolocation ();
2023-12-16 01:13:49 +01:00
$this -> initInertiaTestcase ();
2022-01-03 01:17:24 +01:00
}
2024-06-27 10:52:45 +02:00
public function loginNami ( int $mglnr = 12345 , string $password = 'password' , int | Group $groupId = 55 ) : static
2022-02-19 15:18:24 +01:00
{
Auth :: success ( $mglnr , $password );
2023-04-25 00:28:44 +02:00
$group = is_int ( $groupId )
? Group :: factory () -> create ([ 'nami_id' => $groupId ])
: $groupId ;
2023-04-25 00:42:31 +02:00
$this -> withNamiSettings ( $mglnr , $password , $group -> nami_id );
2022-08-30 23:39:02 +02:00
return $this ;
}
2023-02-26 18:48:34 +01:00
public function withNamiSettings ( int $mglnr = 12345 , string $password = 'password' , int $groupId = 55 ) : self
2022-08-30 23:39:02 +02:00
{
2022-02-19 15:18:24 +01:00
NamiSettings :: fake ([
'mglnr' => $mglnr ,
'password' => $password ,
2023-02-26 18:48:34 +01:00
'default_group_id' => $groupId ,
2022-02-19 15:18:24 +01:00
]);
return $this ;
}
2024-06-27 10:52:45 +02:00
public function login () : static
2022-02-19 15:18:24 +01:00
{
$this -> be ( $user = User :: factory () -> create ());
$this -> me = $user ;
return $this ;
}
2021-11-19 00:15:20 +01:00
public function init () : self
{
Member :: factory () -> defaults () -> create ();
return $this ;
2021-06-18 23:36:06 +02:00
}
2022-03-06 02:56:22 +01:00
2022-11-16 21:23:31 +01:00
/**
* @ param array < string , string > $errors
*/
2022-03-06 02:56:22 +01:00
public function assertErrors ( array $errors , TestResponse $response ) : self
{
2022-02-19 15:18:24 +01:00
$response -> assertSessionHas ( 'errors' );
$this -> assertInstanceOf ( RedirectResponse :: class , $response -> baseResponse );
/** @var RedirectResponse */
$response = $response ;
$sessionErrors = $response -> getSession () -> get ( 'errors' ) -> getBag ( 'default' );
foreach ( $errors as $key => $value ) {
2023-11-30 23:54:16 +01:00
$this -> assertTrue ( $sessionErrors -> has ( $key ), " Cannot find key { $key } in errors ' " . print_r ( $sessionErrors , true ));
$this -> assertEquals ( $value , $sessionErrors -> get ( $key )[ 0 ], " Failed to validate value for session error key { $key } . Actual value: " . print_r ( $sessionErrors , true ));
2022-02-19 15:18:24 +01:00
}
return $this ;
}
2022-09-06 17:51:18 +02:00
/**
* @ param < class - string > $class
*/
public function stubIo ( string $class , callable $mocker ) : self
{
2022-10-18 15:04:47 +02:00
$mock = Phake :: mock ( $class );
2022-09-06 17:51:18 +02:00
$mocker ( $mock );
app () -> instance ( $class , $mock );
return $this ;
}
2023-02-05 23:35:08 +01:00
public function fakeAllHttp () : self
{
Http :: fake ([ '*' => Http :: response ( '' , 200 )]);
return $this ;
}
2023-11-30 23:54:16 +01:00
2023-12-16 01:13:49 +01:00
public function initInertiaTestcase () : void
{
TestResponse :: macro ( 'assertInertiaPath' , function ( $path , $value ) {
/** @var TestResponse */
$response = $this ;
$props = data_get ( $response -> viewData ( 'page' ), 'props' );
Assert :: assertNotNull ( $props );
$json = new AssertableJsonString ( $props );
$json -> assertPath ( $path , $value );
return $this ;
});
2023-12-17 22:33:29 +01:00
2024-01-28 11:42:32 +01:00
TestResponse :: macro ( 'assertInertiaCount' , function ( $path , $count ) {
/** @var TestResponse */
$response = $this ;
$props = data_get ( $response -> viewData ( 'page' ), 'props' );
Assert :: assertNotNull ( $props );
$json = new AssertableJsonString ( $props );
$json -> assertCount ( $count , $path );
return $this ;
});
2023-12-17 22:33:29 +01:00
TestResponse :: macro ( 'assertPdfPageCount' , function ( int $count ) {
/** @var TestResponse */
$response = $this ;
$file = $response -> getFile ();
Assert :: assertTrue ( file_exists ( $file -> getPathname ()));
exec ( 'pdfinfo ' . escapeshellarg ( $file -> getPathname ()) . ' | grep ^Pages | sed "s/Pages:\s*//"' , $output , $returnVar );
Assert :: assertSame ( 0 , $returnVar , 'Failed to get Pages of PDF File ' . $file -> getPathname ());
Assert :: assertCount ( 1 , $output , 'Failed to parse output format of pdfinfo' );
Assert :: assertEquals ( $count , $output [ 0 ]);
return $this ;
});
TestResponse :: macro ( 'assertPdfName' , function ( string $filename ) {
/** @var TestResponse */
$response = $this ;
Assert :: assertEquals ( $filename , $response -> getFile () -> getFilename ());
return $this ;
});
2024-04-26 23:20:03 +02:00
TestResponse :: macro ( 'assertHasJsonPath' , function ( string $path ) {
/** @var TestResponse */
$response = $this ;
Assert :: assertTrue ( Arr :: has ( $response -> json (), $path ), 'Failed that key ' . $path . ' is in Response.' );
return $this ;
});
2023-12-16 01:13:49 +01:00
}
2020-04-10 20:32:12 +02:00
}