Add tests

This commit is contained in:
Alex P. 2025-06-02 10:04:23 +03:00
parent 7b07d2aef5
commit 2a033d3f5f
No known key found for this signature in database
GPG key ID: 54487A734A204D71
3 changed files with 199 additions and 5 deletions

View file

@ -132,12 +132,15 @@ class ExperienceContextBuilder {
public function with_current_landing_page(): ExperienceContextBuilder {
$builder = clone $this;
$landing_page = $this->settings->has( 'landing_page' ) ?
(string) $this->settings->get( 'landing_page' )
: ExperienceContext::LANDING_PAGE_NO_PREFERENCE;
if ( empty( $landing_page ) ) {
$landing_page = ExperienceContext::LANDING_PAGE_NO_PREFERENCE;
}
$builder->experience_context = $builder->experience_context
->with_landing_page(
$this->settings->has( 'landing_page' ) ?
(string) $this->settings->get( 'landing_page' )
: ExperienceContext::LANDING_PAGE_NO_PREFERENCE
);
->with_landing_page( $landing_page );
return $builder;
}

View file

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
use WooCommerce\PayPalCommerce\TestCase;
class ExperienceContextTest extends TestCase
{
public function testAllProps()
{
$empty = new ExperienceContext();
$result = $empty
->with_return_url('example.com')
->with_cancel_url('example.com?cancelled')
->with_brand_name('company')
->with_locale('de_DE')
->with_landing_page('NO_PREFERENCE')
->with_shipping_preference('NO_SHIPPING')
->with_user_action('CONTINUE')
->with_payment_method_preference('UNRESTRICTED');
$this->assertEmpty($empty->to_array());
$this->assertEquals([
'return_url' => 'example.com',
'cancel_url' => 'example.com?cancelled',
'brand_name' => 'company',
'locale' => 'de_DE',
'landing_page' => 'NO_PREFERENCE',
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'CONTINUE',
'payment_method_preference' => 'UNRESTRICTED',
], $result->to_array());
}
}

View file

@ -0,0 +1,154 @@
<?php
declare(strict_types=1);
namespace PHPUnit\ApiClient\Factory;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ExperienceContextBuilder;
use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use function Brain\Monkey\Functions\expect;
use Mockery;
class ExperienceContextBuilderTest extends TestCase
{
private $settings;
private $sut;
public function setUp(): void
{
parent::setUp();
$this->settings = Mockery::mock(Settings::class);
$this->sut = new ExperienceContextBuilder($this->settings);
}
public function testOrderReturnUrls()
{
$wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder
->expects('get_checkout_order_received_url')
->andReturn('https://example.com');
expect('add_query_arg')->andReturnUsing(function ($key, $value, $url) {
return "{$url}?{$key}={$value}";
});
$result = $this->sut
->with_order_return_urls($wcOrder)
->build();
self::assertEquals([
'return_url' => 'https://example.com',
'cancel_url' => 'https://example.com?cancelled=true',
], $result->to_array());
}
public function testCurrentLocale()
{
expect('get_user_locale')->andReturn('de-DE-formal');
$result = $this->sut
->with_current_locale()
->build();
self::assertEquals([
'locale' => 'de-DE',
], $result->to_array());
}
/**
* @dataProvider brandNameDataProvider
*/
public function testCurrentBrandName($has, $value, $expected)
{
$this->settings
->expects('has')
->with('brand_name')
->andReturn($has);
if ($has) {
$this->settings
->expects('get')
->with('brand_name')
->andReturn($value);
}
$result = $this->sut
->with_current_brand_name()
->build();
if ($expected === null) {
$this->assertEmpty($result->to_array());
return;
}
self::assertEquals([
'brand_name' => $expected,
], $result->to_array());
}
public function brandNameDataProvider()
{
yield [
false,
'',
null,
];
yield [
true,
'',
null,
];
yield [
true,
'company',
'company',
];
}
/**
* @dataProvider landingPageDataProvider
*/
public function testCurrentLandingPage($has, $value, $expected)
{
$this->settings
->expects('has')
->with('landing_page')
->andReturn($has);
if ($has) {
$this->settings
->expects('get')
->with('landing_page')
->andReturn($value);
}
$result = $this->sut
->with_current_landing_page()
->build();
self::assertEquals([
'landing_page' => $expected,
], $result->to_array());
}
public function landingPageDataProvider()
{
yield [
false,
'',
ExperienceContext::LANDING_PAGE_NO_PREFERENCE,
];
yield [
true,
'',
ExperienceContext::LANDING_PAGE_NO_PREFERENCE,
];
yield [
true,
ExperienceContext::LANDING_PAGE_LOGIN,
ExperienceContext::LANDING_PAGE_LOGIN,
];
}
}