sarga/tests/_support/FunctionalTester.php

130 lines
3.4 KiB
PHP
Raw Normal View History

2019-11-23 22:05:38 +00:00
<?php
use Webkul\User\Models\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Webkul\Customer\Models\Customer;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\RouteCollection;
2019-11-23 22:05:38 +00:00
/**
* Inherited Methods
* @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)
*/
class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;
/**
* Define custom actions here
*/
2019-11-23 22:09:39 +00:00
/**
* Set the logged in user to the admin identity.
*
* @param \Webkul\User\Models\Admin|null $admin
*
* @throws \Exception
* @returns Admin
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) {
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
2019-11-23 22:09:39 +00:00
*
* @throws \Exception
* @returns Customer
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;
}
/**
* @param string $name
*/
public function amOnAdminRoute(string $name, array $params = [])
{
$I = $this;
$I->amOnRoute($name, $params);
2019-11-23 22:05:38 +00:00
$I->seeCurrentRouteIs($name);
/** @var RouteCollection $routes */
$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
*
* // TODO: change method as soon as there is a method to set core config data
*
* @param $data array containing 'code => value' pairs
*
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
]);
}
}
}
2019-11-23 22:05:38 +00:00
}