--- phase: 07-pre-expansion-fixes plan: 04 subsystem: testing tags: [phpunit, allegro, oauth, unit-tests, bypass-finals] requires: - phase: 01-allegro-token-manager provides: AllegroTokenManager extracted class - phase: 07-pre-expansion-fixes provides: Plans 01-03 bug fixes stabilizing tested classes provides: - 7 unit tests for AllegroTokenManager (token refresh logic) - 5 unit tests for AllegroOrderImportService (import + 401 retry) - PHPUnit infrastructure (composer dependencies, bypass-finals) - 3 use-statement bug fixes discovered by tests affects: [07-05-inpost, future-refactoring, ci-cd] tech-stack: added: [phpunit/phpunit 11.5, dg/bypass-finals 1.9] patterns: [bypass-finals for final class mocking, createMock with DI] key-files: created: - tests/Unit/AllegroTokenManagerTest.php - tests/Unit/AllegroOrderImportServiceTest.php modified: - tests/bootstrap.php - src/Modules/Settings/AllegroTokenManager.php - src/Modules/Settings/AllegroIntegrationRepository.php - src/Modules/Settings/AllegroOrderImportService.php key-decisions: - "dg/bypass-finals zamiast usuwania final z klas produkcyjnych" - "Naprawienie 3 bugów use-statement odkrytych przez testy (nie przez plan)" patterns-established: - "Testy unit: tests/Unit/{ClassName}Test.php z createMock + bypass-finals" - "bootstrap.php: DG\\BypassFinals::enable() dla wszystkich testów" duration: ~15min completed: 2026-03-15T12:00:00Z --- # Phase 7 Plan 04: Unit Tests Summary **12 testów jednostkowych dla AllegroTokenManager (7) i AllegroOrderImportService (5), plus 3 naprawione bugi use-statement odkryte przez testy.** ## Performance | Metric | Value | |--------|-------| | Duration | ~15min | | Completed | 2026-03-15 | | Tasks | 2 completed | | Files created | 2 | | Files modified | 4 | | Tests | 12 | | Assertions | 49 | ## Acceptance Criteria Results | Criterion | Status | Notes | |-----------|--------|-------| | AC-1: AllegroTokenManager — logika refresh pokryta testami | Pass | 7 testów: fresh token, <5min, expired, no config, empty token, re-read, invalid format | | AC-2: AllegroOrderImportService — import happy path pokryty | Pass | 5 testów: happy path, 401 retry, empty ID, non-401 error, re-import | | AC-3: Wszystkie nowe testy przechodzą | Pass | 12 tests, 49 assertions, 0 failures | ## Accomplishments - 7 testów AllegroTokenManager pokrywających: świeży token (brak refresh), wygasający <5min, wygasły, brak konfiguracji OAuth, pusty access token, write-then-re-read, nieprawidłowy format daty - 5 testów AllegroOrderImportService pokrywających: happy path import, 401 retry, pusty ID, propagacja non-401 RuntimeException, re-import (update vs create) - PHPUnit + dg/bypass-finals zainstalowane i skonfigurowane (composer.phar, vendor/, bootstrap.php) - Odkryte i naprawione 3 bugi use-statement w kodzie produkcyjnym ## Files Created/Modified | File | Change | Purpose | |------|--------|---------| | `tests/Unit/AllegroTokenManagerTest.php` | Created | 7 testów logiki OAuth token refresh | | `tests/Unit/AllegroOrderImportServiceTest.php` | Created | 5 testów importu zamówień Allegro | | `tests/bootstrap.php` | Modified | Dodano DG\BypassFinals::enable() | | `src/Modules/Settings/AllegroTokenManager.php` | Modified | Fix: use App\Core\Exceptions\AllegroOAuthException | | `src/Modules/Settings/AllegroIntegrationRepository.php` | Modified | Fix: use App\Core\Exceptions\IntegrationConfigException | | `src/Modules/Settings/AllegroOrderImportService.php` | Modified | Fix: dodano use RuntimeException | | `composer.json` | Modified | Dodano dg/bypass-finals do require-dev | | `composer.lock` | Created | Lock file z zależnościami | | `vendor/` | Created | Zależności Composera | ## Decisions Made | Decision | Rationale | Impact | |----------|-----------|--------| | dg/bypass-finals zamiast usuwania final | Wszystkie klasy produkcyjne są final — usunięcie złamałoby konwencję projektu | Wymaga enable() w bootstrap.php, ale nie zmienia kodu produkcyjnego | | Naprawienie bugów use-statement | Testy ujawniły 3 złamane use-statements które uniemożliwiały prawidłowe działanie catch/throw | AllegroOrderImportService: 401 retry nigdy nie działał bez use RuntimeException | ## Deviations from Plan ### Summary | Type | Count | Impact | |------|-------|--------| | Auto-fixed | 3 | Krytyczne — naprawione bugi use-statement | | Scope additions | 1 | composer install + bypass-finals (konieczne do uruchomienia testów) | | Deferred | 0 | — | **Total impact:** Niezbędne poprawki odkryte przez testy. Bez nich catch(RuntimeException) w AllegroOrderImportService nigdy nie łapał 401. ### Auto-fixed Issues **1. Broken use-statement: AllegroTokenManager** - **Found during:** Task 1 - **Issue:** `use AppCorexceptionsAllegroOAuthException` zamiast `use App\Core\Exceptions\AllegroOAuthException` (brak backslashy) - **Fix:** Poprawiony use-statement - **Files:** src/Modules/Settings/AllegroTokenManager.php - **Verification:** Test `testResolveTokenThrowsWhenNoOAuthConfig` przechodzi **2. Broken use-statement: AllegroIntegrationRepository** - **Found during:** Task 1 - **Issue:** `use AppCorexceptionsIntegrationConfigException` — brak backslashy - **Fix:** Poprawiony use-statement - **Files:** src/Modules/Settings/AllegroIntegrationRepository.php - **Verification:** Klasa ładuje się poprawnie przez autoloader **3. Missing use RuntimeException: AllegroOrderImportService** - **Found during:** Task 2 - **Issue:** catch(RuntimeException) w importSingleOrder() nigdy nie łapał wyjątku — brak `use RuntimeException`, więc PHP szukał `App\Modules\Settings\RuntimeException` - **Fix:** Dodano `use RuntimeException;` - **Files:** src/Modules/Settings/AllegroOrderImportService.php - **Verification:** Test `testImportSingleOrderRetryOn401` przechodzi ## Skill Audit | Oczekiwany | Wywołany | Uwagi | |------------|---------|-------| | sonar-scanner | ○ | Pominięto — brak instalacji w PATH | ## Issues Encountered | Issue | Resolution | |-------|------------| | Brak vendor/ (composer nie był uruchomiony) | Pobrano composer.phar, uruchomiono `composer install --ignore-platform-reqs` | | PHP 8.2 vs wymagane ^8.4 w composer.json | Użyto `--ignore-platform-reqs` — PHPUnit 11.5 działa na 8.2 | | Final classes nie mogą być mockowane | Zainstalowano dg/bypass-finals, dodano enable() w bootstrap | ## Next Phase Readiness **Ready:** - PHPUnit w pełni skonfigurowany — następne testy można dodawać bezproblemowo - Wzorzec testowy ustalony: createMock + bypass-finals - 12 testów jako baseline regression **Concerns:** - PHP 8.2 vs ^8.4 requirement — testy działają ale runtime wymaga 8.4 - sonar-scanner nadal nie uruchamiany (brak w PATH) **Blockers:** - Brak --- *Phase: 07-pre-expansion-fixes, Plan: 04* *Completed: 2026-03-15*