elektronika_bagisto/tests/_support/FunctionalTester.php

147 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2019-11-23 22:05:38 +00:00
<?php
2022-02-10 09:19:02 +00:00
use Actions\CleanAction;
2020-07-30 12:15:54 +00:00
use Codeception\Actor;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Webkul\Customer\Models\Customer;
use Webkul\User\Models\Admin;
2019-11-23 22:05:38 +00:00
/**
* Inherited methods.
*
2019-11-23 22:05:38 +00:00
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
2020-07-30 12:15:54 +00:00
class FunctionalTester extends Actor
2019-11-23 22:05:38 +00:00
{
2022-02-10 09:19:02 +00:00
use _generated\FunctionalTesterActions, CleanAction;
2019-11-23 22:05:38 +00:00
2019-11-23 22:09:39 +00:00
/**
* Set the logged in user to the admin identity.
*
* @param \Webkul\User\Models\Admin|null $admin
* @return \Webkul\User\Models\Admin
*
* @throws \Exception
2019-11-23 22:09:39 +00:00
*/
public function loginAsAdmin(Admin $admin = null): Admin
2019-11-23 22:05:38 +00:00
{
$I = $this;
if (! $admin) {
$admin = $I->grabRecord(Admin::class, ['email' => 'admin@example.com']);
}
if (! $admin) {
2020-07-30 12:15:54 +00:00
throw new Exception(
'Admin user not found in database. Please ensure Seeders are executed'
);
}
Auth::guard('admin')->login($admin);
2019-11-23 22:05:38 +00:00
$I->seeAuthentication('admin');
return $admin;
2019-11-23 22:05:38 +00:00
}
2019-11-23 22:09:39 +00:00
/**
* Set the logged in user to the customer identity.
*
* @param \Webkul\User\Models\Customer|null $customer
* @return \Webkul\Customer\Models\Customer
2019-11-23 22:09:39 +00:00
*
* @throws \Exception
2019-11-23 22:09:39 +00:00
*/
public function loginAsCustomer(Customer $customer = null): Customer
2019-11-23 22:05:38 +00:00
{
$I = $this;
if (! $customer) {
$customer = $I->have(Customer::class);
}
Auth::guard('customer')->login($customer);
$I->seeAuthentication('customer');
return $customer;
}
/**
* On admin route.
*
* @param string $name
* @param array $params
* @param bool $routeCheck
* @return void
*/
2020-07-30 12:15:54 +00:00
public function amOnAdminRoute(string $name, array $params = [], bool $routeCheck = true)
{
$I = $this;
$I->amOnRoute($name, $params);
2020-07-30 12:15:54 +00:00
if ($routeCheck) {
$I->seeCurrentRouteIs($name);
}
2019-11-23 22:05:38 +00:00
$routes = Route::getRoutes();
$middlewares = $routes->getByName($name)->middleware();
$I->assertContains('admin', $middlewares, 'check that admin middleware is applied');
}
2020-01-17 13:42:47 +00:00
/**
* Set specific `Webkul/Core` configuration keys to a given value.
2020-01-17 13:42:47 +00:00
*
* TODO: Change method as soon as there is a method to set core config data.
*
* @param array $data
2020-01-17 13:42:47 +00:00
* @return void
*/
public function setConfigData($data): void
{
2020-01-17 13:42:47 +00:00
foreach ($data as $key => $value) {
if (DB::table('core_config')->where('code', '=', $key)->exists()) {
2020-03-02 08:08:51 +00:00
DB::table('core_config')
->where('code', '=', $key)
->update(['value' => $value]);
2020-01-17 13:42:47 +00:00
} else {
DB::table('core_config')->insert([
2020-03-02 08:08:51 +00:00
'code' => $key,
'value' => $value,
'channel_code' => null,
'locale_code' => null,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
2020-01-17 13:42:47 +00:00
]);
}
}
}
2020-06-11 12:23:34 +00:00
/**
* Use default theme.
*
* @return void.
*/
2020-06-11 12:23:34 +00:00
public function useDefaultTheme(): void
{
$channel = core()->getCurrentChannel();
if ($channel->theme !== 'default') {
$channel->update(['theme' => 'default']);
}
}
2019-11-23 22:05:38 +00:00
}