HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers85/equichecker.com/features/bootstrap/FeatureContext.php
<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use PHPUnit_Framework_Assert as PHPUnit;
use Laracasts\Behat\Context\Migrator;
use Laracasts\Behat\Context\DatabaseTransactions;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{

    use Migrator, DatabaseTransactions;

    protected $type;
    protected $email;
    protected $password;
    protected $customerLanguage = 50;


    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
    }


    public function createUser($email, $password)
    {
        \KommaApp\Users\Models\User::create(
            ['role' => 0,
                'username' => 'behat',
                'password' => Hash::make($password),
                'email' => $email,
                'first_name' => 'Behat',
                'last_name' => 'tester']
        );
    }


    public function createCustomer($email, $password, $from = null, $until = null, $active = 1)
    {
        if (!$from) $from = \Carbon\Carbon::now()->subDay();
        if (!$until) $until = \Carbon\Carbon::now()->addDay();

        \KommaApp\Customers\Models\Customer::create(
            [
                'email' => $email,
                'password' => Hash::make($password),
                'active' => $active,
                'first_name' => 'Behat',
                'site_id' => 1,
                'active_from' => $from,
                'active_until' => $until,
                'language_id' => $this->customerLanguage,
                'last_name' => 'tester']
        );
    }


    /**
     * @Given i have an account :type :email :password
     */
    public function iHaveAnAccount($type, $email, $password = null)
    {
        $this->type = $type;
        $this->email = $email;
        $this->password = $password;

        switch ($this->type) {
            case 'user':
                $this->createUser($email, $password);
                break;
            case 'customer':
                $this->createCustomer($email, $password);
                break;
        }
    }

    /**
     * @Given I have an inactive account :type :email :password
     */
    public function iHaveAnInactiveAccount($type, $email, $password = null)
    {
        $this->type = $type;
        $this->email = $email;
        $this->password = $password;

        switch ($this->type) {
            case 'customer':
                $this->createCustomer($this->email, $this->password, \Carbon\Carbon::now()->subDay(), \Carbon\Carbon::now()->addDay(), 0);
                break;
        }

    }


    /**
     * @Given I have an not yet active account :type :email :password
     */
    public function iHaveAnNotYetActiveAccount($type, $email, $password = null)
    {
        $this->type = $type;
        $this->email = $email;
        $this->password = $password;

        switch ($this->type) {
            case 'customer':
                $this->createCustomer($this->email, $this->password, \Carbon\Carbon::now()->addDay(), \Carbon\Carbon::now()->addDays(2), 1);
                break;
        }
    }


    /**
     * @Given I have an expired active account :type :email :password
     */
    public function iHaveAnExpiredActiveAccount($type, $email, $password = null)
    {
        $this->type = $type;
        $this->email = $email;
        $this->password = $password;

        switch ($this->type) {
            case 'customer':
                $this->createCustomer($this->email, $this->password, \Carbon\Carbon::now()->subDays(2), \Carbon\Carbon::now()->subDay(), 1);
                break;
        }
    }

    /**
     * @When i sign in
     */
    public function iSignIn()
    {

        switch ($this->type) {
            case 'user':
                $this->visit('kms/login');
                break;
            case 'customer':
                $this->visit('login');
        }

        $this->fillField('email', $this->email);
        $this->fillField('password', $this->password);
        $this->pressButton('Login');
    }

    /**
     * @Then i should be logged in
     */
    public function iShouldBeLoggedIn()
    {
        switch ($this->type) {
            case 'user':
                $check = Auth::user()->check();
                break;
            case 'customer':
                $check = Auth::customer()->check();
        }

        PHPUnit::assertTrue($check, 'You are not logged in');
    }


    /**
     * @Then i should not be logged in
     */
    public function iShouldNotBeLoggedIn()
    {
        switch ($this->type) {
            case 'user':
                $check = Auth::user()->check();
                break;
            case 'customer':
                $check = Auth::customer()->check();
        }

        PHPUnit::assertFalse($check, 'You are logged in');
    }


    /**
     * @When I ask a password reset email
     */
    public function iAskAPasswordResetEmail()
    {
        switch ($this->type) {
            case 'user':
                $this->visit('kms/password/forgot');
                break;
            case 'customer':
                $this->visit('password/forgot');
        }

        $this->fillField('email', $this->email);
        $this->pressButton('Send email');

    }

    /**
     * @Then a token for the given uses should be generated
     */
    public function aTokenForTheGivenUsesShouldBeGenerated()
    {
        $token = DB::table('password_resets')->where('email', $this->email)->where('type', $this->type)->select('token')->first();

        PHPUnit::assertNotEmpty($token, 'Reset token not generated');

    }

    /**
     * @When i reset my password to :password
     */
    public function iResetMyPasswordTo($password)
    {
        $this->password = $password;
        //Visit password rest page
        $reset = DB::table('password_resets')->where('email', $this->email)->where('type', $this->type)->first();

        switch ($this->type) {
            case 'user':
                $this->visit('kms/password/reset/' . $reset->type . '/' . $reset->token);
                break;
            case 'customer':
                $this->visit('password/reset/' . $reset->type . '/' . $reset->token);
        }

        //Fill in new password
        $this->fillField('password', $this->password);
        $this->fillField('password_confirmation', $this->password);
        //Send
        $this->pressButton('Change password');
    }


    /**
     * @Then I should be able to create a customer
     */
    public function iShouldBeAbleToCreateACustomer()
    {
        $this->visit('kms/default/customers/create');

        $this->fillField('active_checkbox', 1);
        $this->fillField('active_from', \Carbon\Carbon::now()->subDay()->toTimeString());
        $this->fillField('active_until', \Carbon\Carbon::now()->subDay()->toTimeString());
        $this->fillField('customer_number', 'BEHAT01');
        $this->fillField('email', 'behat@komma.pro');
        $this->fillField('first_name', 'behat');
        $this->fillField('name_insertion', 'the');
        $this->fillField('language_id', '104');
        $this->fillField('last_name', 'tester');
        $this->fillField('company', 'Komma');
        $this->fillField('company_vat', '');
        $this->fillField('postal', '6021 PW');
        $this->fillField('city', 'Budel');
        $this->fillField('street', 'Randweg-zuid');
        $this->fillField('house_number', '1');
        $this->fillField('country', '150');
        $this->fillField('telephone', '0484 88 88 88');

        //Send the form
        $this->pressButton('submit-cloaked');

        //Load the customer based om the email
        $customer = \KommaApp\Customers\Models\Customer::where('email', 'behat@komma.pro')->first();

        //Check if it is in the db
        PHPUnit::assertNotEmpty($customer, 'Customer is not saved');

    }


    /**
     * @Then I should be able to edit a customer
     */
    public function iShouldBeAbleToEditACustomer()
    {
        //First create a customer
        $this->iShouldBeAbleToCreateACustomer();

        //Load the customer
        $customer = \KommaApp\Customers\Models\Customer::where('email', 'behat@komma.pro')->first();

        //Visit the edit page
        $this->visit('kms/default/customers/' . $customer->id);

        //Add these fields cause these are js generated
        $this->fillField('active_from', \Carbon\Carbon::now()->subDay()->toTimeString());
        $this->fillField('active_until', \Carbon\Carbon::now()->subDay()->toTimeString());
        $this->fillField('language_id', '104');

        //Change the city
        $this->fillField('city', 'aangepast');
        //Send the form
        $this->pressButton('submit-cloaked');

        //Reload the current model
        $customer = \KommaApp\Customers\Models\Customer::where('email', 'behat@komma.pro')->first();

        //Check if the city equals the changed value
        PHPUnit::assertEquals('aangepast', $customer->city);

    }

    /**
     * @Then an customerPasswordReset token should be generated
     */
    public function anCustomerPasswordResetTokenShouldBeGenerated()
    {
        $token = DB::table('password_resets')->where('email', 'behat@komma.pro')->where('type', 'customer')->select('token')->first();

        PHPUnit::assertNotEmpty($token, 'Reset token not generated');
    }

    /**
     * @Then the language should be set to the customer language
     */
    public function theLanguageShouldBeSetToTheCustomerLanguage()
    {
        //Load the customer
        $customer = \KommaApp\Customers\Models\Customer::where('email', $this->email)->first();

        PHPUnit::assertEquals($customer->language->iso_2, \App::getLocale(), 'Language is not set');

    }


    /**
     * @Then I should be able to search :searchString
     *     */
    public function iShouldBeAbleToSearch($searchString)
    {
        $this->visit('/');
        $this->fillField('search_field', $searchString);

        $this->pressButton('search');
        
        $this->assertPageContainsText('Type');
    }
}