|
| 1 | +<?php |
| 2 | +declare( strict_types = 1 ); |
| 3 | + |
| 4 | +namespace WooCommerce\PayPalCommerce\StoreSync\Registration; |
| 5 | + |
| 6 | +use Mockery; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use WooCommerce\PayPalCommerce\StoreSync\Setting\AgenticSettingsDataModel; |
| 9 | +use WooCommerce\PayPalCommerce\TestCase; |
| 10 | +use WP_Error; |
| 11 | + |
| 12 | +/** |
| 13 | + * @covers \WooCommerce\PayPalCommerce\StoreSync\Registration\ReconciliationService |
| 14 | + */ |
| 15 | +class ReconciliationServiceTest extends TestCase { |
| 16 | + |
| 17 | + /** |
| 18 | + * @var AgenticSettingsDataModel|Mockery\MockInterface |
| 19 | + */ |
| 20 | + private $settings; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var RegistrationService|Mockery\MockInterface |
| 24 | + */ |
| 25 | + private $registration; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var LoggerInterface|Mockery\MockInterface |
| 29 | + */ |
| 30 | + private $logger; |
| 31 | + |
| 32 | + public function setUp(): void { |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + $this->settings = Mockery::mock( AgenticSettingsDataModel::class ); |
| 36 | + $this->registration = Mockery::mock( RegistrationService::class ); |
| 37 | + |
| 38 | + $this->logger = Mockery::mock( LoggerInterface::class )->shouldIgnoreMissing(); |
| 39 | + } |
| 40 | + |
| 41 | + private function create_testee(): ReconciliationService { |
| 42 | + return new ReconciliationService( $this->settings, $this->registration, $this->logger ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * GIVEN a store that should initialize agentic features but is not yet registered |
| 47 | + * WHEN reconciling the registration state |
| 48 | + * AND the registration call succeeds |
| 49 | + * THEN the store is registered |
| 50 | + * AND the toggle is left active, since registration succeeded |
| 51 | + */ |
| 52 | + public function test_reconcile_registers_store_when_desired_but_not_registered(): void { |
| 53 | + $this->settings->shouldReceive( 'should_initialize_features' )->andReturn( true ); |
| 54 | + $this->registration->shouldReceive( 'is_registered' )->andReturn( false ); |
| 55 | + |
| 56 | + $this->registration->shouldReceive( 'register' ) |
| 57 | + ->once() |
| 58 | + ->andReturn( new RegistrationResult( true, 'Registered', null ) ); |
| 59 | + |
| 60 | + $this->settings->shouldNotReceive( 'set_active' ); |
| 61 | + $this->settings->shouldNotReceive( 'set_last_registration_error' ); |
| 62 | + $this->settings->shouldNotReceive( 'save' ); |
| 63 | + |
| 64 | + $this->create_testee()->reconcile(); |
| 65 | + |
| 66 | + $this->addToAssertionCount( 4 ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * GIVEN a store that should initialize agentic features but is not yet registered |
| 71 | + * WHEN reconciling the registration state |
| 72 | + * AND the registration call fails |
| 73 | + * THEN the toggle is switched off, so a failure is never retried automatically |
| 74 | + * AND the failure reason is logged for troubleshooting |
| 75 | + * AND the settings are saved |
| 76 | + */ |
| 77 | + public function test_reconcile_disables_toggle_when_registration_fails(): void { |
| 78 | + $this->settings->shouldReceive( 'should_initialize_features' )->andReturn( true ); |
| 79 | + $this->registration->shouldReceive( 'is_registered' )->andReturn( false ); |
| 80 | + |
| 81 | + $this->registration->shouldReceive( 'register' ) |
| 82 | + ->once() |
| 83 | + ->andReturn( new WP_Error( 'registration_failed', 'store not found' ) ); |
| 84 | + |
| 85 | + $this->settings->shouldReceive( 'set_active' )->once()->with( false ); |
| 86 | + $this->settings->shouldReceive( 'save' )->once(); |
| 87 | + |
| 88 | + $this->create_testee()->reconcile(); |
| 89 | + |
| 90 | + $this->addToAssertionCount( 3 ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * GIVEN a store that is registered but should no longer have agentic features active |
| 95 | + * WHEN reconciling the registration state |
| 96 | + * THEN the store is deregistered |
| 97 | + */ |
| 98 | + public function test_reconcile_deregisters_store_when_no_longer_desired(): void { |
| 99 | + $this->settings->shouldReceive( 'should_initialize_features' )->andReturn( false ); |
| 100 | + $this->registration->shouldReceive( 'is_registered' )->andReturn( true ); |
| 101 | + |
| 102 | + $this->registration->shouldReceive( 'deregister' )->once(); |
| 103 | + $this->registration->shouldNotReceive( 'register' ); |
| 104 | + |
| 105 | + $this->create_testee()->reconcile(); |
| 106 | + |
| 107 | + $this->addToAssertionCount( 2 ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * GIVEN the desired and actual registration state already match |
| 112 | + * WHEN reconciling the registration state |
| 113 | + * THEN neither registration nor deregistration is attempted |
| 114 | + * |
| 115 | + * @dataProvider matching_state_provider |
| 116 | + */ |
| 117 | + public function test_reconcile_does_nothing_when_state_already_matches( bool $desired, bool $actual ): void { |
| 118 | + $this->settings->shouldReceive( 'should_initialize_features' )->andReturn( $desired ); |
| 119 | + $this->registration->shouldReceive( 'is_registered' )->andReturn( $actual ); |
| 120 | + |
| 121 | + $this->registration->shouldNotReceive( 'register' ); |
| 122 | + $this->registration->shouldNotReceive( 'deregister' ); |
| 123 | + |
| 124 | + $this->create_testee()->reconcile(); |
| 125 | + |
| 126 | + $this->addToAssertionCount( 2 ); |
| 127 | + } |
| 128 | + |
| 129 | + public function matching_state_provider(): array { |
| 130 | + return array( |
| 131 | + 'already registered and still desired' => array( true, true ), |
| 132 | + 'already deregistered and no longer desired' => array( false, false ), |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * GIVEN auto-registration has been disabled via the PPCP_AGENTIC_AUTO_REGISTER constant |
| 138 | + * AND a store that should initialize agentic features but is not yet registered |
| 139 | + * WHEN reconciling the registration state |
| 140 | + * THEN registration is skipped |
| 141 | + * |
| 142 | + * @runInSeparateProcess |
| 143 | + * @preserveGlobalState disabled |
| 144 | + */ |
| 145 | + public function test_reconcile_skips_registration_when_auto_register_is_disabled(): void { |
| 146 | + define( 'PPCP_AGENTIC_AUTO_REGISTER', false ); |
| 147 | + |
| 148 | + $this->settings->shouldReceive( 'should_initialize_features' )->andReturn( true ); |
| 149 | + $this->registration->shouldReceive( 'is_registered' )->andReturn( false ); |
| 150 | + |
| 151 | + $this->registration->shouldNotReceive( 'register' ); |
| 152 | + $this->registration->shouldNotReceive( 'deregister' ); |
| 153 | + |
| 154 | + $this->create_testee()->reconcile(); |
| 155 | + |
| 156 | + $this->addToAssertionCount( 2 ); |
| 157 | + } |
| 158 | +} |
0 commit comments