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 ;
2023-02-05 23:35:08 +01:00
use Illuminate\Support\Facades\Http ;
2021-07-17 15:58:38 +02:00
use Illuminate\Testing\TestResponse ;
2022-10-18 15:04:47 +02:00
use Phake ;
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 ();
2022-01-03 01:17:24 +01:00
}
2023-04-25 00:28:44 +02:00
public function loginNami ( int $mglnr = 12345 , string $password = 'password' , int | Group $groupId = 55 ) : self
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 ;
}
public function login () : self
{
$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 ) {
$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 ));
}
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 ;
}
2020-04-10 20:32:12 +02:00
}