Skip to content

Commit

Permalink
ISSUE-337: more test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tatevikg1 committed Dec 3, 2024
1 parent f9ce63b commit 12d014c
Show file tree
Hide file tree
Showing 20 changed files with 353 additions and 554 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0']
php-versions: ['8.1']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -81,4 +81,4 @@ jobs:
continue-on-error: ${{matrix.php-versions == '8.0' }} # [temp-php8]
- name: Running PHP_CodeSniffer
run: vendor/bin/phpcs --standard=config/PhpCodeSniffer/ bin/ src/ tests/ public/;
continue-on-error: ${{matrix.php-versions == '8.0' }} # [temp-php8]
continue-on-error: ${{matrix.php-versions == '8.0' }} # [temp-php8]
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"nette/caching": "^3.0.0",
"nikic/php-parser": "^4.19.1",
"phpmd/phpmd": "^2.6.0",
"symfony/test-pack": "^1.1"
"symfony/test-pack": "^1.1",
"symfony/process": "^6.4"
},
"suggest": {
"phplist/web-frontend": "5.0.x-dev",
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Model/Subscription/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Subscriber implements DomainModel, Identity, CreationDate, ModificationDat
private ?string $extraData;

#[ORM\OneToMany(
mappedBy: "subscriber",
targetEntity: "PhpList\Core\Domain\Model\Subscription\Subscription",
mappedBy: "subscriber",
cascade: ["remove"]
)]
private Collection $subscriptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/
class AdministratorFixture extends Fixture
{
use ModelTestTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/

class AdministratorTokenWithAdministratorFixture extends Fixture
{
use ModelTestTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/
class DetachedAdministratorTokenFixture extends Fixture
{
use ModelTestTrait;
Expand Down
52 changes: 32 additions & 20 deletions tests/Integration/Domain/Repository/Fixtures/SubscriberFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@

use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ObjectManager;
use PhpList\Core\Domain\Model\Subscription\Subscriber;
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/
class SubscriberFixture extends Fixture
{
use ModelTestTrait;
public function load(ObjectManager $manager): void
{
$csvFile = __DIR__ . '/Subscriber.csv';
Expand All @@ -31,26 +26,43 @@ public function load(ObjectManager $manager): void
}

$headers = fgetcsv($handle);
if ($headers === false) {
throw new RuntimeException('Could not read headers from CSV file.');
}

/** @var Connection $connection */
$connection = $manager->getConnection();

$insertQuery = "
INSERT INTO phplist_user_user (
id, entered, modified, email, confirmed, blacklisted, bouncecount,
uniqid, htmlemail, disabled, extradata
) VALUES (
:id, :creation_date, :modification_date, :email, :confirmed, :blacklisted, :bounce_count,
:unique_id, :html_email, :disabled, :extra_data
)
";

$stmt = $connection->prepare($insertQuery);

while (($data = fgetcsv($handle)) !== false) {
$row = array_combine($headers, $data);

$subscriber = new Subscriber();
$this->setSubjectId($subscriber,(int)$row['id']);
$this->setSubjectProperty($subscriber,'creationDate', new DateTime($row['entered']));
$this->setSubjectProperty($subscriber,'modificationDate', new DateTime($row['modified']));
$subscriber->setEmail($row['email']);
$subscriber->setConfirmed((bool) $row['confirmed']);
$subscriber->setBlacklisted((bool) $row['blacklisted']);
$subscriber->setBounceCount((int) $row['bouncecount']);
$subscriber->setUniqueId($row['uniqueid']);
$subscriber->setHtmlEmail((bool) $row['htmlemail']);
$subscriber->setDisabled((bool) $row['disabled']);
$subscriber->setExtraData($row['extradata']);
$manager->persist($subscriber);
$stmt->executeStatement([
'id' => (int) $row['id'],
'creation_date' => (new DateTime($row['entered']))->format('Y-m-d H:i:s'),
'modification_date' => (new DateTime($row['modified']))->format('Y-m-d H:i:s'),
'email' => $row['email'],
'confirmed' => (bool) $row['confirmed'] ? 1 : 0,
'blacklisted' => (bool) $row['blacklisted'] ? 1 : 0,
'bounce_count' => (int) $row['bouncecount'],
'unique_id' => $row['uniqueid'],
'html_email' => (bool) $row['htmlemail'] ? 1 : 0,
'disabled' => (bool) $row['disabled'] ? 1 : 0,
'extra_data' => $row['extradata'],
]);
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/
class SubscriberListFixture extends Fixture
{
use ModelTestTrait;
Expand Down Expand Up @@ -52,10 +49,9 @@ public function load(ObjectManager $manager): void

$manager->persist($admin);
$manager->persist($subscriberList);
$this->setSubjectProperty($subscriberList,'creationDate', new DateTime($row['entered']));
$manager->flush();
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@

use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ObjectManager;
use PhpList\Core\Domain\Model\Messaging\SubscriberList;
use PhpList\Core\Domain\Model\Subscription\Subscriber;
use PhpList\Core\Domain\Model\Subscription\Subscription;
use PhpList\Core\Tests\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

/**
* @author Tatevik Grigoryan <[email protected]>
*/
class SubscriptionFixture extends Fixture
{
use ModelTestTrait;
public function load(ObjectManager $manager): void
{
$csvFile = __DIR__ . '/Subscription.csv';
Expand All @@ -33,27 +26,34 @@ public function load(ObjectManager $manager): void
}

$headers = fgetcsv($handle);
if ($headers === false) {
throw new RuntimeException('Could not read headers from CSV file.');
}

while (($data = fgetcsv($handle)) !== false) {
$row = array_combine($headers, $data);
/** @var Connection $connection */
$connection = $manager->getConnection();

$subscriber = new Subscriber();
$this->setSubjectId($subscriber,(int)$row['userid']);
$manager->persist($subscriber);
$insertSubscriptionQuery = "
INSERT INTO phplist_listuser (
userid, listid, entered, modified
) VALUES (
:subscriber_id, :subscriber_list_id, :creation_date, :modification_date
)
";

$subscriberList = new SubscriberList();
$this->setSubjectId($subscriberList,(int)$row['listid']);
$manager->persist($subscriberList);
$subscriptionStmt = $connection->prepare($insertSubscriptionQuery);

while (($data = fgetcsv($handle)) !== false) {
$row = array_combine($headers, $data);

$subscription = new Subscription();
$this->setSubjectProperty($subscription,'subscriber', $subscriber);
$this->setSubjectProperty($subscription,'subscriberList', $subscriberList);
$this->setSubjectProperty($subscription,'creationDate', new DateTime($row['entered']));
$this->setSubjectProperty($subscription,'modificationDate', new DateTime($row['modified']));
$manager->persist($subscription);
$subscriptionStmt->executeStatement([
'subscriber_id' => (int) $row['userid'],
'subscriber_list_id' => (int) $row['listid'],
'creation_date' => (new DateTime($row['entered']))->format('Y-m-d H:i:s'),
'modification_date' => (new DateTime($row['modified']))->format('Y-m-d H:i:s'),
]);
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* Testcase.
*
* @author Oliver Klee <[email protected]>
* @author Tatevik Grigoryan <[email protected]>
*/
class AdministratorRepositoryTest extends KernelTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* Testcase.
*
* @author Oliver Klee <[email protected]>
* @author Tatevik Grigoryan <[email protected]>
*/
class AdministratorTokenRepositoryTest extends KernelTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function setUp(): void
parent::setUp();
$this->loadSchema();

$this->subject = self::getContainer()->get(SubscriberListRepository::class);
$this->subscriberListRepository = self::getContainer()->get(SubscriberListRepository::class);
$this->administratorRepository = self::getContainer()->get(AdministratorRepository::class);
$this->subscriberRepository = self::getContainer()->get(SubscriberRepository::class);
$this->subscriptionRepository = self::getContainer()->get(SubscriptionRepository::class);
Expand All @@ -59,7 +59,7 @@ public function testFindReadsModelFromDatabase()
$this->loadFixtures([SubscriberListFixture::class]);

$id = 1;
$creationDate = new DateTime('2016-06-22 15:01:17');
$creationDate = new DateTime();
$modificationDate = new DateTime();
$name = 'News';
$description = 'News (and some fun stuff)';
Expand All @@ -68,7 +68,7 @@ public function testFindReadsModelFromDatabase()
$category = 'news';

/** @var SubscriberList $model */
$model = $this->subject->find($id);
$model = $this->subscriberListRepository->find($id);

self::assertSame($id, $model->getId());
self::assertSimilarDates($creationDate, $model->getCreationDate());
Expand All @@ -88,7 +88,7 @@ public function testCreatesOwnerAssociationAsProxy()
$subscriberListId = 1;
$ownerId = 1;
/** @var SubscriberList $model */
$model = $this->subject->find($subscriberListId);
$model = $this->subscriberListRepository->find($subscriberListId);
$owner = $model->getOwner();

self::assertInstanceOf(Administrator::class, $owner);
Expand Down Expand Up @@ -119,19 +119,19 @@ public function testModificationDateOfNewModelIsSetToNowOnPersist()
public function testSavePersistsAndFlushesModel()
{
$model = new SubscriberList();
$this->subject->save($model);
$this->subscriberListRepository->save($model);

self::assertSame($model, $this->subject->find($model->getId()));
self::assertSame($model, $this->subscriberListRepository->find($model->getId()));
}

public function testFindByOwnerFindsSubscriberListWithTheGivenOwner()
{
$this->loadFixtures([SubscriberListFixture::class, AdministratorFixture::class]);

$owner = $this->administratorRepository->find(1);
$ownedList = $this->subject->find(1);
$ownedList = $this->subscriberListRepository->find(1);

$result = $this->subject->findByOwner($owner);
$result = $this->subscriberListRepository->findByOwner($owner);

self::assertContains($ownedList, $result);
}
Expand All @@ -141,32 +141,32 @@ public function testFindByOwnerIgnoresSubscriberListWithOtherOwner()
$this->loadFixtures([SubscriberListFixture::class, AdministratorFixture::class]);

$owner = $this->administratorRepository->find(1);
$foreignList = $this->subject->find(2);
$foreignList = $this->subscriberListRepository->find(2);

$result = $this->subject->findByOwner($owner);
$result = $this->subscriberListRepository->findByOwner($owner);

self::assertNotContains($foreignList, $result);
}

public function testFindByOwnerIgnoresSubscriberListFromOtherOwner()
{
$this->loadFixtures([SubscriberListFixture::class, AdministratorFixture::class]);
$this->loadFixtures([SubscriberListFixture::class]);

$owner = $this->administratorRepository->find(1);
$unownedList = $this->subject->find(3);
$unownedList = $this->subscriberListRepository->find(3);

$result = $this->subject->findByOwner($owner);
$result = $this->subscriberListRepository->findByOwner($owner);

self::assertNotContains($unownedList, $result);
}

public function testFindsAssociatedSubscriptions()
{
$this->loadFixtures([SubscriberListFixture::class, SubscriberFixture::class, SubscriptionFixture::class]);
$this->loadFixtures([SubscriptionFixture::class]);

$id = 2;
/** @var SubscriberList $model */
$model = $this->subject->find($id);
$model = $this->subscriberListRepository->find($id);
$subscriptions = $model->getSubscriptions();

self::assertFalse($subscriptions->isEmpty());
Expand All @@ -183,7 +183,7 @@ public function testFindsAssociatedSubscribers()

$id = 2;
/** @var SubscriberList $model */
$model = $this->subject->find($id);
$model = $this->subscriberListRepository->find($id);
$subscribers = $model->getSubscribers();

$expectedSubscriber = $this->subscriberRepository->find(1);
Expand All @@ -200,12 +200,12 @@ public function testRemoveAlsoRemovesAssociatedSubscriptions()

$id = 2;
/** @var SubscriberList $model */
$model = $this->subject->find($id);
$model = $this->subscriberListRepository->find($id);

$numberOfAssociatedSubscriptions = count($model->getSubscriptions());
self::assertGreaterThan(0, $numberOfAssociatedSubscriptions);

$this->subject->remove($model);
$this->subscriberListRepository->remove($model);

$newNumberOfSubscriptions = count($this->subscriptionRepository->findAll());
$numberOfRemovedSubscriptions = $initialNumberOfSubscriptions - $newNumberOfSubscriptions;
Expand All @@ -217,13 +217,13 @@ public function testRemoveRemovesModel()
$this->loadFixtures([SubscriberListFixture::class]);

/** @var SubscriberList[] $allModels */
$allModels = $this->subject->findAll();
$allModels = $this->subscriberListRepository->findAll();
$numberOfModelsBeforeRemove = count($allModels);
$firstModel = $allModels[0];

$this->subject->remove($firstModel);
$this->subscriberListRepository->remove($firstModel);

$numberOfModelsAfterRemove = count($this->subject->findAll());
$numberOfModelsAfterRemove = count($this->subscriberListRepository->findAll());
self::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
}
}
Loading

0 comments on commit 12d014c

Please sign in to comment.