Skip to content

Commit

Permalink
ISSUE-337: test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tatevikg1 committed Dec 5, 2024
1 parent df46140 commit 38c3ad8
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
/vendor/
/docs/phpdocumentor
.vagrant
.phpdoc/
.phpdoc/
.phpunit.result.cache
7 changes: 1 addition & 6 deletions src/Domain/Model/Subscription/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ class Subscriber implements DomainModel, Identity, CreationDate, ModificationDat

#[ORM\ManyToMany(
targetEntity: "PhpList\Core\Domain\Model\Messaging\SubscriberList",
inversedBy: "subscribers"
)]
#[ORM\JoinTable(
name: "phplist_listuser",
joinColumns: [new ORM\JoinColumn(name: "userid")],
inverseJoinColumns: [new ORM\JoinColumn(name: "listid")]
mappedBy: "subscribers"
)]
private Collection $subscribedLists;

Expand Down
3 changes: 1 addition & 2 deletions src/TestingSupport/Traits/DatabaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ protected function loadFixtures(array $fixtures): void
}

$fixtureInstance->load($this->entityManager);
$this->entityManager->flush();
}

$this->entityManager->flush();
}

protected function loadSchema(): void
Expand Down
5 changes: 2 additions & 3 deletions src/TestingSupport/Traits/ModelTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait ModelTestTrait
/**
* Sets the (private) ID of $this->repository.
*
* @param DomainModel $model
* @param int $id
*
* @return void
Expand All @@ -32,9 +33,7 @@ private function setSubjectId(DomainModel $model,int $id): void
* @param string $propertyName
* @param mixed $value
*
* @return void
* @internal
*
* @return void*
*/
private function setSubjectProperty(DomainModel $model, string $propertyName, mixed $value): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ public function load(ObjectManager $manager): void
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,5 @@ public function load(ObjectManager $manager): void
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ public function load(ObjectManager $manager): void
}

fclose($handle);
$manager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function load(ObjectManager $manager): void

$manager->persist($admin);
$manager->persist($subscriberList);
$manager->flush();
}

fclose($handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

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\TestingSupport\Traits\ModelTestTrait;
use RuntimeException;

class SubscriptionFixture extends Fixture
{
use ModelTestTrait;

public function load(ObjectManager $manager): void
{
$csvFile = __DIR__ . '/Subscription.csv';
Expand All @@ -26,32 +31,24 @@ 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();

$insertSubscriptionQuery = "
INSERT INTO phplist_listuser (
userid, listid, entered, modified
) VALUES (
:subscriber_id, :subscriber_list_id, :creation_date, :modification_date
)
";

$subscriptionStmt = $connection->prepare($insertSubscriptionQuery);

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

$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'),
]);
$subscriber = new Subscriber();
$this->setSubjectId($subscriber,(int)$row['userid']);
$manager->persist($subscriber);

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

$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);
}

fclose($handle);
Expand Down

0 comments on commit 38c3ad8

Please sign in to comment.