| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Unit\Service\Dolibarr;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Service\Dolibarr\DolibarrUtils;
- use App\Service\Utils\DatesUtils;
- use Doctrine\DBAL\Connection;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- /**
- * Class to expose protected methods for testing.
- */
- class TestableDolibarrUtils extends DolibarrUtils
- {
- public function executeQueryPublic(string $sql): void
- {
- $this->executeQuery($sql, []);
- }
- }
- class DolibarrUtilsTest extends TestCase
- {
- private Connection|MockObject $dolibarrConnection;
- private TestableDolibarrUtils $dolibarrUtils;
- protected function setUp(): void
- {
- $this->dolibarrConnection = $this->createMock(Connection::class);
- $this->dolibarrUtils = new TestableDolibarrUtils($this->dolibarrConnection);
- }
- /**
- * @see DolibarrUtils::getProductId()
- */
- public function testGetProductIdForArtistPremiumTrial(): void
- {
- $result = $this->dolibarrUtils->getProductId(
- SettingsProductEnum::ARTIST_PREMIUM,
- true,
- false
- );
- $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_TRIAL_PRODUCT_ID, $result);
- }
- /**
- * @see DolibarrUtils::getProductId()
- */
- public function testGetProductIdForArtistPremiumCmf(): void
- {
- $result = $this->dolibarrUtils->getProductId(
- SettingsProductEnum::ARTIST_PREMIUM,
- false,
- true
- );
- $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_CMF_PRODUCT_ID, $result);
- }
- /**
- * @see DolibarrUtils::getProductId()
- */
- public function testGetProductIdForArtistPremium(): void
- {
- $result = $this->dolibarrUtils->getProductId(
- SettingsProductEnum::ARTIST_PREMIUM,
- false,
- false
- );
- $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_PRODUCT_ID, $result);
- }
- /**
- * @see DolibarrUtils::getProductId()
- */
- public function testGetProductIdForArtistCmf(): void
- {
- $result = $this->dolibarrUtils->getProductId(
- SettingsProductEnum::ARTIST,
- false,
- true
- );
- $this->assertEquals(DolibarrUtils::ARTIST_STANDARD_CMF_PRODUCT_ID, $result);
- }
- /**
- * @see DolibarrUtils::getProductId()
- */
- public function testGetProductIdWithInvalidContractType(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid contract type');
- $this->dolibarrUtils->getProductId(
- SettingsProductEnum::SCHOOL,
- false,
- false
- );
- }
- /**
- * @see DolibarrUtils::executeQuery()
- */
- public function testExecuteQuery(): void
- {
- $sql = 'DELETE FROM llx_societe_commerciaux WHERE fk_soc = 123';
- $this->dolibarrConnection
- ->expects($this->once())
- ->method('executeQuery')
- ->with($sql, []);
- $this->dolibarrUtils->executeQueryPublic($sql);
- }
- /**
- * @see DolibarrUtils::updateSocietyCommercialsWithApi()
- */
- public function testUpdateSocietyCommercialsWithApi(): void
- {
- $societyId = 123;
- $apiUserId = 8;
- $this->dolibarrConnection
- ->expects($this->exactly(2))
- ->method('executeQuery')
- ->withConsecutive(
- ['DELETE FROM llx_societe_commerciaux WHERE fk_soc = ?', [$societyId]],
- ['INSERT INTO llx_societe_commerciaux (fk_soc, fk_user) VALUES (?, ?)', [$societyId, $apiUserId]]
- );
- $this->dolibarrUtils->updateSocietyCommercialsWithApi($societyId);
- }
- /**
- * @see DolibarrUtils::addActionComm()
- */
- public function testAddActionComm(): void
- {
- $societyId = 123;
- $title = 'Test Title';
- $message = 'Test Message';
- $apiUserId = 8;
- // Mock DatesUtils to return a fixed date
- $tz = new \DateTimeZone('Europe/Paris');
- $mockDate = new \DateTime('2023-01-01 12:00:00', $tz);
- $formattedDate = $mockDate->format('Y-m-d H:i:s');
- // Create a partial mock of DatesUtils to control the 'new' static method
- $datesMock = $this->createMock(DatesUtils::class);
- DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
- $sql = "INSERT INTO llx_actioncomm (fk_soc, ref, code, label, note, datep, datep2, datec, fk_user_author, fk_user_mod, fk_user_action, percent)
- VALUES (?, -1, 'AC_OT_ONLINE_STORE', ?, ?, ?, ?, ?, ?, ?, ?, -1)";
- $this->dolibarrConnection
- ->expects($this->once())
- ->method('executeQuery')
- ->with(
- $sql,
- [
- $societyId,
- $title,
- $message,
- $formattedDate,
- $formattedDate,
- $formattedDate,
- $apiUserId,
- $apiUserId,
- $apiUserId,
- ]
- );
- $this->dolibarrUtils->addActionComm($societyId, $title, $message);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForArtist(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST);
- $this->assertEquals('Opentalent Artist', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForArtistPremiumTrial(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM, true);
- $this->assertEquals('Opentalent Artist Premium (Essai)', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForArtistPremium(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM);
- $this->assertEquals('Opentalent Artist Premium', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForSchool(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL);
- $this->assertEquals('Opentalent School', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForSchoolPremiumTrial(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM, true);
- $this->assertEquals('Opentalent School Premium (Essai)', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForSchoolPremium(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM);
- $this->assertEquals('Opentalent School Premium', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForManager(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER);
- $this->assertEquals('Opentalent Manager', $result);
- }
- /**
- * @see DolibarrUtils::getDolibarrProductName()
- */
- public function testGetDolibarrProductNameForManagerPremium(): void
- {
- $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER_PREMIUM);
- $this->assertEquals('Opentalent Manager Premium', $result);
- }
- }
|