From dcd0fbe45b6e4a084a1e04bc2f18a3c17bd38e1d Mon Sep 17 00:00:00 2001 From: Divesh Pahuja Date: Wed, 28 Dec 2022 16:32:09 +0100 Subject: [PATCH] Use DI for services & remove aliases - resolves #205 --- src/Newsletter/AddressSource/SegmentAddressSource.php | 10 +++++++++- tests/Model/Activity/ActivityTest.php | 5 +++-- tests/Model/Customer/CustomerListTest.php | 6 +++--- tests/_support/Helper/Model.php | 10 ++++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Newsletter/AddressSource/SegmentAddressSource.php b/src/Newsletter/AddressSource/SegmentAddressSource.php index a414fa81..40efc46f 100644 --- a/src/Newsletter/AddressSource/SegmentAddressSource.php +++ b/src/Newsletter/AddressSource/SegmentAddressSource.php @@ -41,7 +41,7 @@ public function __construct(array $arguments) } else { $operator = SegmentManagerInterface::CONDITION_OR; } - $this->segmentManager = \Pimcore::getContainer()->get(SegmentManagerInterface::class); + $this->sendingParamContainers = $this->setUpSendingParamContainers( array_filter($arguments['segmentIds']), $operator, @@ -49,6 +49,14 @@ public function __construct(array $arguments) ); } + /** + * @required + */ + public function setSegmentManager(SegmentManagerInterface $segmentManager): void + { + $this->segmentManager = $segmentManager; + } + /** * @inheritDoc */ diff --git a/tests/Model/Activity/ActivityTest.php b/tests/Model/Activity/ActivityTest.php index 831c8690..dde15578 100644 --- a/tests/Model/Activity/ActivityTest.php +++ b/tests/Model/Activity/ActivityTest.php @@ -2,6 +2,7 @@ namespace CustomerManagementFrameworkBundle\Tests\Model\Activity; use CustomerManagementFrameworkBundle\ActivityManager\ActivityManagerInterface; +use CustomerManagementFrameworkBundle\ActivityStore\ActivityStoreInterface; use CustomerManagementFrameworkBundle\Model\Activity\GenericActivity; use Pimcore\Model\DataObject\Customer; use Pimcore\Tests\Support\Test\ModelTestCase; @@ -36,8 +37,8 @@ protected function createCustomer(): Customer { public function testTrackActivity() { - $activityManager = \Pimcore::getContainer()->get(ActivityManagerInterface::class); - $activityStore = \Pimcore::getContainer()->get('cmf.activity_store'); + $activityManager = $this->tester->grabService(ActivityManagerInterface::class); + $activityStore = $this->tester->grabService(ActivityStoreInterface::class); $customer = $this->createCustomer(); diff --git a/tests/Model/Customer/CustomerListTest.php b/tests/Model/Customer/CustomerListTest.php index 8cb1e2be..b5415d42 100644 --- a/tests/Model/Customer/CustomerListTest.php +++ b/tests/Model/Customer/CustomerListTest.php @@ -113,7 +113,7 @@ public function tearDown(): void protected function createSegments() { - $segmentManager = \Pimcore::getContainer()->get(SegmentManagerInterface::class); + $segmentManager = $this->tester->grabService(SegmentManagerInterface::class); $segmentManager->resetSegmentsFolder(); foreach ($this->segmentDummyData as $segment) { $segmentManager->createSegment($segment['name'], $segment['group'], $segment['name']); @@ -122,7 +122,7 @@ protected function createSegments() protected function createCustomers() { - $segmentManager = \Pimcore::getContainer()->get(SegmentManagerInterface::class); + $segmentManager = $this->tester->grabService(SegmentManagerInterface::class); foreach ($this->customerDummyData as &$customerData) { @@ -351,7 +351,7 @@ public function testDateBetweenFilter() public function testCustomerSegmentFilter() { - $segmentManager = \Pimcore::getContainer()->get(SegmentManagerInterface::class); + $segmentManager = $this->tester->grabService(SegmentManagerInterface::class); //test one filter $listing = new Customer\Listing(); diff --git a/tests/_support/Helper/Model.php b/tests/_support/Helper/Model.php index 71b25e83..2674e688 100644 --- a/tests/_support/Helper/Model.php +++ b/tests/_support/Helper/Model.php @@ -48,4 +48,14 @@ public function initializeDefinitions() $cm = $this->getModule('\\' . ClassManager::class); $cm->setupClass('Customer', __DIR__ . '/../../../install/class_source/optional/class_Customer_export.json'); } + + /** + * @throws \Codeception\Exception\ModuleException + */ + public function grabService(string $serviceId): mixed + { + $pimcoreHelper = $this->getModule('\\' . Pimcore::class); + + return $pimcoreHelper->grabService($serviceId); + } }