Skip to content

Commit ce3ebf1

Browse files
committed
OXDEV-8947 Improve User model use cases
* added the getId to User interface * in tracker service, we can rely on the User interface instead of listing all related * user integration test splitted - the greeting part was moved to the Greeting namespace * tests improved to wait for form submissions - stability * adjust to handle possible null or empty value in User::getId Signed-off-by: Anton Fedurtsya <anton@fedurtsya.com>
1 parent 122ef78 commit ce3ebf1

File tree

10 files changed

+188
-98
lines changed

10 files changed

+188
-98
lines changed

src/Extension/Model/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use OxidEsales\Eshop\Core\Model\BaseModel;
1313
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUser;
14-
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1514

1615
/**
1716
* @eshopExtension
@@ -28,4 +27,9 @@
2827
class User extends User_parent implements UserInterface
2928
{
3029
use PersonalGreetingUser;
30+
31+
public function getId(): ?string
32+
{
33+
return parent::getId();
34+
}
3135
}

src/Extension/Model/UserInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111

1212
interface UserInterface extends PersonalGreetingUserInterface
1313
{
14+
public function getId(): ?string;
1415
}

src/Greeting/Controller/GreetingController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
1414
use OxidEsales\ExamplesModule\Core\Module as ModuleCore;
1515
use OxidEsales\ExamplesModule\Extension\Model\User as ExamplesModelUser;
16+
use OxidEsales\ExamplesModule\Extension\Model\UserInterface;
1617
use OxidEsales\ExamplesModule\Greeting\Service\GreetingMessageServiceInterface;
1718
use OxidEsales\ExamplesModule\Settings\Service\ModuleSettingsServiceInterface;
1819
use OxidEsales\ExamplesModule\Tracker\Infrastructure\Repository\TrackerRepositoryInterface;
1920

21+
use function PHPUnit\Framework\isInstanceOf;
22+
2023
/**
2124
* @extendable-class
2225
*
@@ -50,11 +53,13 @@ public function render()
5053
{
5154
$template = parent::render();
5255

53-
/** @var ExamplesModelUser $user */
5456
$user = $this->getUser();
5557

56-
/** @phpstan-ignore-next-line */
57-
if (is_a($user, EshopModelUser::class) && $this->moduleSettings->isPersonalGreetingMode()) {
58+
if (
59+
!empty($user->getId())
60+
&& $user instanceof UserInterface
61+
&& $this->moduleSettings->isPersonalGreetingMode()
62+
) {
5863
$greeting = $user->getPersonalGreeting();
5964
$tracker = $this->trackerRepository->getTrackerByUserId($user->getId());
6065
$counter = $tracker->getCount();

src/Tracker/Service/TrackerService.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
namespace OxidEsales\ExamplesModule\Tracker\Service;
1111

12-
use OxidEsales\Eshop\Application\Model\User;
13-
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
12+
use OxidEsales\ExamplesModule\Extension\Model\UserInterface;
1413
use OxidEsales\ExamplesModule\Greeting\Repository\GreetingRepositoryInterface;
1514
use OxidEsales\ExamplesModule\Tracker\Infrastructure\Repository\TrackerRepositoryInterface;
1615

@@ -25,8 +24,12 @@ public function __construct(
2524
) {
2625
}
2726

28-
public function updateTracker(User&PersonalGreetingUserInterface $user): void
27+
public function updateTracker(UserInterface $user): void
2928
{
29+
if (empty($user->getId())) {
30+
return;
31+
}
32+
3033
$savedGreeting = $this->greetingRepository->getSavedUserGreeting($user->getId());
3134

3235
if ($savedGreeting !== $user->getPersonalGreeting()) {

src/Tracker/Service/TrackerServiceInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
namespace OxidEsales\ExamplesModule\Tracker\Service;
1111

12-
use OxidEsales\Eshop\Application\Model\User;
13-
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
12+
use OxidEsales\ExamplesModule\Extension\Model\UserInterface;
1413

1514
interface TrackerServiceInterface
1615
{
17-
public function updateTracker(User&PersonalGreetingUserInterface $user): void;
16+
public function updateTracker(UserInterface $user): void;
1817
}

src/Tracker/Subscriber/BeforeModelUpdate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace OxidEsales\ExamplesModule\Tracker\Subscriber;
1313

14-
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
14+
use OxidEsales\Eshop\Core\Model\BaseModel;
1515
use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\BeforeModelUpdateEvent;
16-
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
16+
use OxidEsales\ExamplesModule\Extension\Model\UserInterface;
1717
use OxidEsales\ExamplesModule\Tracker\Service\TrackerServiceInterface;
1818
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1919

@@ -31,8 +31,8 @@ public function handle(BeforeModelUpdateEvent $event): BeforeModelUpdateEvent
3131
{
3232
$payload = $event->getModel();
3333

34-
if (is_a($payload, PersonalGreetingUserInterface::class)) {
35-
/** @var EshopModelUser&PersonalGreetingUserInterface $payload */
34+
if (is_a($payload, UserInterface::class)) {
35+
/** @var BaseModel&UserInterface $payload */
3636
$this->trackerService->updateTracker($payload);
3737
}
3838

tests/Codeception/Acceptance/UpdateGreetingCest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ public function testSetGreetingMode(AcceptanceTester $I): void
4949

5050
$I->seeElement('#oeem_update_greeting');
5151
$I->click('#oeem_update_greeting');
52+
$I->waitForPageLoad();
5253
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_TITLE'));
5354
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '0');
54-
5555
$I->seeElement('#oeemgreeting_submit');
5656
$I->seeElement('#oeem_greeting_input');
5757
$I->fillField(ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME, 'Hello master of the filled cart');
5858
$I->click('#oeemgreeting_submit');
59+
$I->waitForPageLoad();
5960
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '1');
6061

6162
//See changed greeting text on start page
@@ -74,19 +75,23 @@ public function testTrackGreetingModeChanges(AcceptanceTester $I): void
7475

7576
$I->seeElement('#oeem_update_greeting');
7677
$I->click('#oeem_update_greeting');
78+
$I->waitForPageLoad();
7779
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '0');
7880

7981
$I->seeElement('#oeemgreeting_submit');
8082
$I->fillField(ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME, 'Hello master of the filled cart');
8183
$I->click('#oeemgreeting_submit');
84+
$I->waitForPageLoad();
8285
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '1');
8386

8487
$I->fillField(ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME, 'Hi shopping addict');
8588
$I->click('#oeemgreeting_submit');
89+
$I->waitForPageLoad();
8690
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '2');
8791

8892
//trying to change to same does not update the count
8993
$I->click('#oeemgreeting_submit');
94+
$I->waitForPageLoad();
9095
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '2');
9196

9297
//See changed greeting text on start page
@@ -113,6 +118,7 @@ public function testAnonymousUserAccessingModuleController(AcceptanceTester $I):
113118
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '0');
114119
$I->fillField(ModuleCore::OEEM_GREETING_TEMPLATE_VARNAME, 'Hi shopping addict');
115120
$I->click('#oeemgreeting_submit');
121+
$I->waitForPageLoad();
116122

117123
// no harm done (do nothing)
118124
$I->see(Translator::translate('OEEXAMPLESMODULE_GREETING_UPDATE_COUNT') . '0');

tests/Integration/Extension/Model/UserTest.php

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,31 @@
1010
namespace OxidEsales\ExamplesModule\Tests\Integration\Extension\Model;
1111

1212
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
13-
use OxidEsales\Eshop\Core\Registry;
14-
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
1513
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
1614
use OxidEsales\ExamplesModule\Extension\Model\User;
15+
use OxidEsales\ExamplesModule\Extension\Model\UserInterface;
16+
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUserInterface;
1717
use PHPUnit\Framework\Attributes\CoversClass;
18+
use PHPUnit\Framework\Attributes\Test;
1819

1920
#[CoversClass(User::class)]
2021
final class UserTest extends IntegrationTestCase
2122
{
22-
public function setUp(): void
23-
{
24-
parent::setUp();
25-
26-
$this->cleanUpUsers();
27-
}
28-
29-
public function tearDown(): void
30-
{
31-
Registry::getSession()->setUser(null);
32-
parent::tearDown();
33-
}
34-
35-
private function cleanUpUsers()
36-
{
37-
$queryBuilder = $this->get(QueryBuilderFactoryInterface::class)->create();
38-
$queryBuilder->delete('oxuser');
39-
$queryBuilder->execute();
40-
}
41-
42-
public function testGetPersonalGreetingNotSet(): void
43-
{
44-
$user = oxNew(EshopModelUser::class);
45-
46-
$this->assertEmpty($user->getPersonalGreeting());
47-
}
48-
49-
public function testGetPersonalGreeting(): void
23+
public function testImplementsInterfaces(): void
5024
{
5125
$user = oxNew(EshopModelUser::class);
52-
$user->setPersonalGreeting('some information about me');
5326

54-
$this->assertSame('some information about me', $user->getPersonalGreeting());
27+
self::assertInstanceOf(UserInterface::class, $user);
28+
self::assertInstanceOf(PersonalGreetingUserInterface::class, $user);
5529
}
5630

57-
public function testNewFieldNotAutomaticallySavedToDatabase(): void
31+
#[Test]
32+
public function getIdWorksAsIntended(): void
5833
{
5934
$user = oxNew(EshopModelUser::class);
60-
$user->setId('_testuser');
61-
$user->save();
62-
$user->setPersonalGreeting('some information about me');
63-
unset($user); //this object was not saved after last assign
35+
$this->assertNull($user->getId());
6436

65-
$user = oxNew(EshopModelUser::class);
66-
$user->load('_testuser');
67-
$this->assertEmpty($user->getPersonalGreeting());
68-
unset($user);
69-
}
70-
71-
public function testNewFieldSavedToDatabase(): void
72-
{
73-
$user = oxNew(EshopModelUser::class);
74-
$user->setId('_newuser');
75-
$user->save();
76-
$user->setPersonalGreeting('some information about me');
77-
$user->assign([
78-
'oxusername' => null,
79-
'oxpassword' => null,
80-
'oxregister' => null,
81-
]);
82-
$user->save();
83-
unset($user);
84-
85-
$user = oxNew(EshopModelUser::class);
86-
$user->load('_newuser');
87-
$this->assertTrue($user->isLoaded());
88-
$this->assertSame('some information about me', $user->getPersonalGreeting());
37+
$user->setId($randomId = uniqid());
38+
$this->assertSame($randomId, $user->getId());
8939
}
9040
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Copyright © . All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\ExamplesModule\Tests\Integration\Greeting\Model;
11+
12+
use OxidEsales\Eshop\Application\Model\User as EshopModelUser;
13+
use OxidEsales\Eshop\Core\Registry;
14+
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
15+
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
16+
use OxidEsales\ExamplesModule\Greeting\Model\PersonalGreetingUser;
17+
use PHPUnit\Framework\Attributes\CoversClass;
18+
19+
#[CoversClass(PersonalGreetingUser::class)]
20+
final class PersonalGreetingUserTest extends IntegrationTestCase
21+
{
22+
public function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->cleanUpUsers();
27+
}
28+
29+
public function tearDown(): void
30+
{
31+
Registry::getSession()->setUser(null);
32+
parent::tearDown();
33+
}
34+
35+
private function cleanUpUsers()
36+
{
37+
$queryBuilder = $this->get(QueryBuilderFactoryInterface::class)->create();
38+
$queryBuilder->delete('oxuser');
39+
$queryBuilder->execute();
40+
}
41+
42+
public function testGetPersonalGreetingNotSet(): void
43+
{
44+
$user = oxNew(EshopModelUser::class);
45+
46+
$this->assertEmpty($user->getPersonalGreeting());
47+
}
48+
49+
public function testGetPersonalGreeting(): void
50+
{
51+
$user = oxNew(EshopModelUser::class);
52+
$user->setPersonalGreeting('some information about me');
53+
54+
$this->assertSame('some information about me', $user->getPersonalGreeting());
55+
}
56+
57+
public function testNewFieldNotAutomaticallySavedToDatabase(): void
58+
{
59+
$user = oxNew(EshopModelUser::class);
60+
$user->setId('_testuser');
61+
$user->save();
62+
$user->setPersonalGreeting('some information about me');
63+
unset($user); //this object was not saved after last assign
64+
65+
$user = oxNew(EshopModelUser::class);
66+
$user->load('_testuser');
67+
$this->assertEmpty($user->getPersonalGreeting());
68+
unset($user);
69+
}
70+
71+
public function testNewFieldSavedToDatabase(): void
72+
{
73+
$user = oxNew(EshopModelUser::class);
74+
$user->setId('_newuser');
75+
$user->save();
76+
$user->setPersonalGreeting('some information about me');
77+
$user->assign([
78+
'oxusername' => null,
79+
'oxpassword' => null,
80+
'oxregister' => null,
81+
]);
82+
$user->save();
83+
unset($user);
84+
85+
$user = oxNew(EshopModelUser::class);
86+
$user->load('_newuser');
87+
$this->assertTrue($user->isLoaded());
88+
$this->assertSame('some information about me', $user->getPersonalGreeting());
89+
}
90+
}

0 commit comments

Comments
 (0)