| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Tests\Module\Dashboard; |
| 6 |
|
| 7 |
use Packetery\Module\Carrier; |
| 8 |
use Packetery\Module\Carrier\CarrierUpdater; |
| 9 |
use Packetery\Module\Dashboard\DashboardHelper; |
| 10 |
use Packetery\Module\Dashboard\DashboardItemBuilder; |
| 11 |
use Packetery\Module\Dashboard\DashboardPage; |
| 12 |
use Packetery\Module\Framework\WpAdapter; |
| 13 |
use Packetery\Module\Options\OptionsProvider; |
| 14 |
use Packetery\Module\Order\PacketSynchronizer; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use PHPUnit\Framework\TestCase; |
| 17 |
use ReflectionClass; |
| 18 |
|
| 19 |
class DashboardItemBuilderTest extends TestCase { |
| 20 |
private WpAdapter|MockObject $wpAdapterMock; |
| 21 |
private OptionsProvider|MockObject $optionsProviderMock; |
| 22 |
private CarrierUpdater|MockObject $carrierUpdaterMock; |
| 23 |
private DashboardItemBuilder|MockObject $dashboardItemBuilder; |
| 24 |
|
| 25 |
protected function setUp(): void { |
| 26 |
$this->wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 27 |
$this->optionsProviderMock = $this->createMock( OptionsProvider::class ); |
| 28 |
$this->carrierUpdaterMock = $this->createMock( CarrierUpdater::class ); |
| 29 |
|
| 30 |
$this->dashboardItemBuilder = new DashboardItemBuilder( |
| 31 |
$this->wpAdapterMock, |
| 32 |
$this->createMock( DashboardHelper::class ), |
| 33 |
$this->optionsProviderMock, |
| 34 |
$this->createMock( Carrier\EntityRepository::class ), |
| 35 |
$this->carrierUpdaterMock, |
| 36 |
$this->createMock( PacketSynchronizer::class ), |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function testGetCarrierUpdateUrlReturnsNullIfApiPasswordIsNull(): void { |
| 41 |
$this->optionsProviderMock->expects( $this->once() ) |
| 42 |
->method( 'get_api_password' ) |
| 43 |
->willReturn( null ); |
| 44 |
|
| 45 |
$this->carrierUpdaterMock->expects( $this->never() ) |
| 46 |
->method( 'getLastUpdate' ); |
| 47 |
|
| 48 |
$result = $this->invokeGetCarrierUpdateUrl(); |
| 49 |
|
| 50 |
$this->assertNull( $result ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testGetCarrierUpdateUrlReturnsCarrierUrlWhenLastUpdateIsNotNull(): void { |
| 54 |
$this->optionsProviderMock->expects( $this->once() ) |
| 55 |
->method( 'get_api_password' ) |
| 56 |
->willReturn( 'validApiPassword' ); |
| 57 |
|
| 58 |
$this->carrierUpdaterMock->expects( $this->once() ) |
| 59 |
->method( 'getLastUpdate' ) |
| 60 |
->willReturn( '2023-10-23' ); |
| 61 |
|
| 62 |
$this->wpAdapterMock->expects( $this->once() ) |
| 63 |
->method( 'adminUrl' ) |
| 64 |
->with( 'admin.php?page=' . Carrier\OptionsPage::SLUG . '&update_carriers=1' ) |
| 65 |
->willReturn( 'https://example.com/admin-options-page-update' ); |
| 66 |
|
| 67 |
$result = $this->invokeGetCarrierUpdateUrl(); |
| 68 |
|
| 69 |
$this->assertEquals( 'https://example.com/admin-options-page-update', $result ); |
| 70 |
} |
| 71 |
|
| 72 |
public function testGetCarrierUpdateUrlReturnsHomeUrlWhenLastUpdateIsNull(): void { |
| 73 |
$this->optionsProviderMock->expects( $this->once() ) |
| 74 |
->method( 'get_api_password' ) |
| 75 |
->willReturn( 'validApiPassword' ); |
| 76 |
|
| 77 |
$this->carrierUpdaterMock->expects( $this->once() ) |
| 78 |
->method( 'getLastUpdate' ) |
| 79 |
->willReturn( null ); |
| 80 |
|
| 81 |
$this->wpAdapterMock->expects( $this->once() ) |
| 82 |
->method( 'adminUrl' ) |
| 83 |
->with( 'admin.php?page=' . DashboardPage::SLUG . '&update_carriers=1' ) |
| 84 |
->willReturn( 'https://example.com/admin-dashboard-page-update' ); |
| 85 |
|
| 86 |
$result = $this->invokeGetCarrierUpdateUrl(); |
| 87 |
|
| 88 |
$this->assertEquals( 'https://example.com/admin-dashboard-page-update', $result ); |
| 89 |
} |
| 90 |
|
| 91 |
private function invokeGetCarrierUpdateUrl(): ?string { |
| 92 |
$reflection = new ReflectionClass( DashboardItemBuilder::class ); |
| 93 |
$method = $reflection->getMethod( 'getCarrierUpdateUrl' ); |
| 94 |
$method->setAccessible( true ); |
| 95 |
|
| 96 |
return $method->invoke( $this->dashboardItemBuilder ); |
| 97 |
} |
| 98 |
} |
| 99 |
|