sarga/tests/functional/Customer/CustomerCest.php

214 lines
5.6 KiB
PHP
Raw Normal View History

2020-01-10 10:23:31 +00:00
<?php
2020-05-09 10:38:20 +00:00
namespace Tests\Functional\Customer;
2020-06-11 12:23:34 +00:00
use Faker\Factory;
2020-05-09 10:38:20 +00:00
use FunctionalTester;
2021-04-13 13:35:41 +00:00
use Webkul\Customer\Models\Customer;
2021-04-13 11:58:51 +00:00
use Webkul\Customer\Models\CustomerAddress;
2020-01-10 10:23:31 +00:00
class CustomerCest
{
2021-11-29 11:48:34 +00:00
/**
* Faker factory.
*
* @var Faker\Factory
*/
public $faker;
/**
2021-12-04 09:57:39 +00:00
* Address fields.
2021-11-29 11:48:34 +00:00
*
* @var array
*/
2020-01-14 09:49:22 +00:00
public $fields = [];
2020-01-10 10:23:31 +00:00
2021-11-29 11:48:34 +00:00
/**
* Constructor.
*
* @return void
*/
public function __construct()
{
/**
* Instantiate a european faker factory to have the vat provider available.
*/
$this->faker = Factory::create('at_AT');
}
/**
* Before method.
*
* @param FunctionalTester $I
* @return void
*/
2020-06-11 12:23:34 +00:00
public function _before(FunctionalTester $I): void
{
$I->useDefaultTheme();
}
2021-11-29 11:48:34 +00:00
/**
* Test update customer profile.
*
* @param FunctionalTester $I
* @return void
*/
2020-05-08 11:41:13 +00:00
public function updateCustomerProfile(FunctionalTester $I): void
2020-01-10 10:23:31 +00:00
{
2020-01-14 09:49:22 +00:00
$customer = $I->loginAsCustomer();
2021-12-04 09:57:39 +00:00
$customer->first_name = $this->cleanField($customer->first_name);
$customer->last_name = $this->cleanField($customer->last_name);
$customer->save();
2020-01-10 10:23:31 +00:00
$I->amOnPage('/');
2021-04-13 11:58:51 +00:00
$I->click('Profile');
$I->click('Edit');
$I->selectOption('gender', 'Other');
$I->click('Update Profile');
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->dontSeeInSource('The old password does not match.');
$I->seeInSource('Profile updated successfully.');
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->seeRecord(Customer::class, [
'id' => $customer->id,
'gender' => 'Other',
]);
2020-01-10 10:23:31 +00:00
}
2021-11-29 11:48:34 +00:00
/**
* Update customer address.
*
* @param FunctionalTester $I
* @return void
*/
2020-05-08 11:41:13 +00:00
public function updateCustomerAddress(FunctionalTester $I): void
2020-01-10 10:23:31 +00:00
{
$formCssSelector = '#customer-address-form';
2020-01-10 10:23:31 +00:00
$I->loginAsCustomer();
2020-01-10 10:23:31 +00:00
$I->amOnPage('/');
2021-04-13 11:58:51 +00:00
$I->click('Profile');
$I->click('Address');
$I->click('Add Address');
2020-01-10 10:23:31 +00:00
2021-11-29 11:48:34 +00:00
$this->setFields();
2020-01-10 10:23:31 +00:00
2020-01-14 09:49:22 +00:00
foreach ($this->fields as $key => $value) {
2021-11-29 11:48:34 +00:00
/**
* The following fields are rendered via javascript so we ignore them.
*/
if (! in_array($key, [
2020-01-10 10:23:31 +00:00
'country',
'state',
])) {
$selector = 'input[name="' . $key . '"]';
2021-04-13 11:58:51 +00:00
$I->fillField($selector, $value);
2020-01-10 10:23:31 +00:00
}
}
2021-12-04 09:57:39 +00:00
$I->wantTo('Ensure that the `company_name` field is being displayed.');
2021-04-13 11:58:51 +00:00
$I->seeElement('.account-table-content > div:nth-child(2) > input:nth-child(2)');
2020-01-10 10:23:31 +00:00
2021-11-29 11:48:34 +00:00
/**
* We need to use this css selector to hit the correct <form>. There is another one at the
* page header (search).
*/
2021-04-13 11:58:51 +00:00
$I->submitForm($formCssSelector, $this->fields);
$I->seeInSource('The given vat id has a wrong format');
2020-01-10 13:07:58 +00:00
2021-12-04 09:57:39 +00:00
$I->wantTo('Enter a valid vat id.');
2021-11-29 11:48:34 +00:00
$this->fields['vat_id'] = $this->faker->vat(false);
2020-01-10 13:07:58 +00:00
2021-04-13 11:58:51 +00:00
$I->submitForm($formCssSelector, $this->fields);
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->seeInSource('Address have been successfully added.');
2020-01-10 10:23:31 +00:00
2020-01-14 09:49:22 +00:00
$this->assertCustomerAddress($I);
2020-01-10 10:23:31 +00:00
2021-12-04 09:57:39 +00:00
$I->wantTo('Update the created customer address again.');
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->click('Edit');
2020-01-10 10:23:31 +00:00
2020-01-14 09:49:22 +00:00
$oldcompany = $this->fields['company_name'];
2021-12-04 09:57:39 +00:00
$this->fields['company_name'] = $this->cleanField($this->faker->company);
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->submitForm($formCssSelector, $this->fields);
2020-01-10 10:23:31 +00:00
2021-04-13 11:58:51 +00:00
$I->seeInSource('Address updated successfully.');
2020-01-10 10:23:31 +00:00
$I->dontSeeRecord(CustomerAddress::class, [
'company_name' => $oldcompany,
]);
2021-04-13 11:58:51 +00:00
$this->assertCustomerAddress($I);
2020-01-14 09:49:22 +00:00
}
/**
2021-11-29 11:48:34 +00:00
* Assert customer address.
*
* @param FunctionalTester $I
* @return void
2020-01-14 09:49:22 +00:00
*/
private function assertCustomerAddress(FunctionalTester $I): void
{
2021-04-13 11:58:51 +00:00
$I->seeRecord(CustomerAddress::class, [
'company_name' => $this->fields['company_name'],
'first_name' => $this->fields['first_name'],
'last_name' => $this->fields['last_name'],
'vat_id' => $this->fields['vat_id'],
'address1' => $this->fields['address1[]'],
'country' => $this->fields['country'],
'state' => $this->fields['state'],
'city' => $this->fields['city'],
'phone' => $this->fields['phone'],
'postcode' => $this->fields['postcode'],
]);
2020-01-10 10:23:31 +00:00
}
2021-11-29 11:48:34 +00:00
/**
* Set fields.
*
* @return void
*/
private function setFields()
{
2021-12-03 06:13:24 +00:00
$this->fields = $this->cleanAllFields([
'company_name' => $this->faker->company,
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
2021-11-29 11:48:34 +00:00
'vat_id' => 'INVALIDVAT',
2021-12-03 06:13:24 +00:00
'address1[]' => $this->faker->streetAddress,
'country' => $this->faker->countryCode,
'state' => $this->faker->word,
'city' => $this->faker->city,
'postcode' => $this->faker->postcode,
'phone' => $this->faker->phoneNumber,
]);
}
/**
* Clean all fields.
*
* @param array $fields
* @return array
*/
private function cleanAllFields(array $fields)
{
return collect($fields)->map(function ($field, $key) {
return $this->cleanField($field);
})->toArray();
2021-11-29 11:48:34 +00:00
}
/**
* Clean fields.
*
* @param string $field
* @return string
*/
private function cleanField($field)
{
return preg_replace('/[^A-Za-z0-9 ]/', '', $field);
}
2021-04-13 11:58:51 +00:00
}