Skip to content

Commit

Permalink
Source Entity expansion with Pickup Location attributes #2182.
Browse files Browse the repository at this point in the history
Adjusted Pickup Location entity
  • Loading branch information
novikor committed May 18, 2019
1 parent 7426756 commit 7559630
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 106 deletions.
16 changes: 0 additions & 16 deletions app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ class PickupLocation implements PickupLocationInterface
*/
private $phone;

/**
* @var string[]|null
*/
private $openHours;

/**
* @var string|null
*/
Expand All @@ -117,7 +112,6 @@ class PickupLocation implements PickupLocationInterface
* @param string|null $street
* @param string|null $postcode
* @param string|null $phone
* @param string[]|null $openHours
* @param PickupLocationExtensionInterface|null $extensionAttributes
*/
public function __construct(
Expand All @@ -136,7 +130,6 @@ public function __construct(
?string $street = null,
?string $postcode = null,
?string $phone = null,
?array $openHours = null,
?PickupLocationExtensionInterface $extensionAttributes = null
) {
$this->sourceCode = $sourceCode;
Expand All @@ -154,7 +147,6 @@ public function __construct(
$this->street = $street;
$this->postcode = $postcode;
$this->phone = $phone;
$this->openHours = $openHours;
$this->extensionAttributes = $extensionAttributes;
}

Expand Down Expand Up @@ -278,14 +270,6 @@ public function getPhone(): ?string
return $this->phone;
}

/**
* @inheritdoc
*/
public function getOpenHours(): ?array
{
return $this->openHours;
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@

namespace Magento\InventoryInStorePickup\Test\Integration\PickupLocation;

use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\InventoryApi\Api\Data\SourceExtensionInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface;
use Magento\InventoryInStorePickupApi\Model\Mapper;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationExtensionInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class MapperTest extends \PHPUnit\Framework\TestCase
class MapperTest extends TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var \Magento\InventoryApi\Api\SourceRepositoryInterface
* @var SourceRepositoryInterface
*/
private $sourceRepository;

Expand All @@ -36,13 +35,14 @@ protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->sourceRepository = $this->objectManager->create(SourceRepositoryInterface::class);
$this->sourceCode = 'source-code-1';
$this->sourceCode = 'pickup';
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong mapping provided for Magento\InventoryApi\Api\Data\SourceInterface. Field 'source_fail_field' is not found.
* @expectedExceptionMessage Wrong mapping provided for Magento\InventoryApi\Api\Data\SourceInterface. Field
* 'source_fail_field' is not found.
*/
public function testWrongMappingForSource()
{
Expand All @@ -55,9 +55,11 @@ public function testWrongMappingForSource()
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong mapping provided for Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'extension_attributes.fail_field' is not found.
* @expectedExceptionMessage Wrong mapping provided for
* Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'extension_attributes.fail_field'
* is not found.
*/
public function testWrongMappingForPickupLocationExtensionAttributes()
{
Expand All @@ -70,9 +72,10 @@ public function testWrongMappingForPickupLocationExtensionAttributes()
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Wrong mapping provided for Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'fail_field' is not found.
* @expectedExceptionMessage Wrong mapping provided for
* Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'fail_field' is not found.
*/
public function testWrongMappingForPickupLocation()
{
Expand All @@ -85,86 +88,25 @@ public function testWrongMappingForPickupLocation()
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php
*/
public function testMapPickupLocation()
{
$source = $this->sourceRepository->get($this->sourceCode);
/** @var Mapper $mapper */
$mapper = $this->objectManager->create(Mapper::class, ['map' => $this->getMap()]);
$mapper = $this->objectManager->create(Mapper::class);
$pickupLocation = $mapper->map($source);

$this->assertEquals($source->getSourceCode(), $pickupLocation->getSourceCode());
$this->assertEquals($source->getExtensionAttributes()->getFrontendName(), $pickupLocation->getName());
$this->assertNotEquals($source->getDescription(), $pickupLocation->getDescription());
$this->assertEquals($source->getEmail(), $pickupLocation->getEmail());
$this->assertEquals($source->getContactName(), $pickupLocation->getContactName());
$this->assertEquals($source->getDescription(), $pickupLocation->getDescription());
$this->assertEquals($source->getLatitude(), $pickupLocation->getLatitude());
$this->assertEquals($source->getLongitude(), $pickupLocation->getLongitude());
$this->assertEquals($source->getCountryId(), $pickupLocation->getCountryId());
$this->assertEquals($source->getRegionId(), $pickupLocation->getRegionId());
$this->assertEquals($source->getRegion(), $pickupLocation->getRegion());
$this->assertEquals($source->getCity(), $pickupLocation->getCity());
$this->assertEquals($source->getStreet(), $pickupLocation->getStreet());
$this->assertEquals($source->getPostcode(), $pickupLocation->getPostcode());
$this->assertEquals($source->getPhone(), $pickupLocation->getPhone());
$this->assertEquals($source->getFax(), $pickupLocation->getFax());
$this->assertInstanceOf(PickupLocationExtensionInterface::class, $pickupLocation->getExtensionAttributes());
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
*/
public function testMapPickupLocationWithExtensionAttributes()
{
$source = $this->sourceRepository->get($this->sourceCode);

$sourceExtensionAttributes = $this->getMockBuilder(SourceExtensionInterface::class)
->disableOriginalConstructor()
->setMethods(['getOpenHours', 'getSomeAttribute'])
->getMockForAbstractClass();
$sourceExtensionAttributes->expects($this->once())
->method('getOpenHours')
->willReturn(['open', 'hours']);
$sourceExtensionAttributes->expects($this->once())
->method('getSomeAttribute')
->willReturn('some_value');
$source->setExtensionAttributes($sourceExtensionAttributes);

$pickupLocationExtension = $this->getMockBuilder(PickupLocationExtensionInterface::class)
->disableOriginalConstructor()
->setMethods(['setPickupLocationAttribute'])
->getMock();
$pickupLocationExtension->expects($this->once())
->method('setPickupLocationAttribute')
->with('some_value');

$extensionAttributesFactory = $this->getMockBuilder(ExtensionAttributesFactory::class)
->disableOriginalConstructor()
->getMock();
$extensionAttributesFactory->expects($this->once())
->method('create')
->willReturn($pickupLocationExtension);

$createFromSource = $this->objectManager->create(
CreateFromSourceInterface::class,
['extensionAttributesFactory' => $extensionAttributesFactory]
);

$map = $this->getMap();
$map['extension_attributes.open_hours'] = 'open_hours';
$map['extension_attributes.some_attribute'] = 'extension_attributes.pickup_location_attribute';

/** @var Mapper $mapper */
$mapper = $this->objectManager->create(
Mapper::class,
['map' => $map, 'createFromSource' => $createFromSource]
$this->assertEquals(
$source->getExtensionAttributes()->getFrontendDescription(),
$pickupLocation->getDescription()
);
$pickupLocation = $mapper->map($source);

$this->assertEquals($source->getSourceCode(), $pickupLocation->getSourceCode());
$this->assertEquals($source->getEmail(), $pickupLocation->getEmail());
$this->assertEquals($source->getContactName(), $pickupLocation->getContactName());
$this->assertEquals($source->getDescription(), $pickupLocation->getDescription());
$this->assertNotEquals($source->getName(), $pickupLocation->getName());
$this->assertEquals($source->getLatitude(), $pickupLocation->getLatitude());
$this->assertEquals($source->getLongitude(), $pickupLocation->getLongitude());
$this->assertEquals($source->getCountryId(), $pickupLocation->getCountryId());
Expand All @@ -175,7 +117,7 @@ public function testMapPickupLocationWithExtensionAttributes()
$this->assertEquals($source->getPostcode(), $pickupLocation->getPostcode());
$this->assertEquals($source->getPhone(), $pickupLocation->getPhone());
$this->assertEquals($source->getFax(), $pickupLocation->getFax());
$this->assertEquals(['open', 'hours'], $pickupLocation->getOpenHours());
$this->assertInstanceOf(PickupLocationExtensionInterface::class, $pickupLocation->getExtensionAttributes());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Api\DataObjectHelper;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var SourceInterfaceFactory $sourceFactory */
$sourceFactory = Bootstrap::getObjectManager()->get(SourceInterfaceFactory::class);
/** @var DataObjectHelper $dataObjectHelper */
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
/** @var SourceRepositoryInterface $sourceRepository */
$sourceRepository = Bootstrap::getObjectManager()->get(SourceRepositoryInterface::class);

/** @var SourceInterface $source */
$source = $sourceFactory->create();
$dataObjectHelper->populateWithArray(
$source,
[
SourceInterface::SOURCE_CODE => 'pickup',
SourceInterface::NAME => 'source-name-1',
SourceInterface::CONTACT_NAME => 'source-contact-name',
SourceInterface::EMAIL => 'source-email',
SourceInterface::ENABLED => true,
SourceInterface::DESCRIPTION => 'source-description',
SourceInterface::LATITUDE => 11.123456,
SourceInterface::LONGITUDE => 12.123456,
SourceInterface::COUNTRY_ID => 'US',
SourceInterface::REGION_ID => 10,
SourceInterface::CITY => 'source-city',
SourceInterface::STREET => 'source-street',
SourceInterface::POSTCODE => 'source-postcode',
SourceInterface::PHONE => 'source-phone',
SourceInterface::FAX => 'source-fax',
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
SourceInterface::CARRIER_LINKS => [],
],
SourceInterface::class
);
$sourceRepository->save($source);

$source = $sourceRepository->get('pickup');
$source->getExtensionAttributes()
->setFrontendDescription('frontend-description')
->setFrontendName('frontend-name')
->setIsPickupLocationActive(true);

$sourceRepository->save($source);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\App\ResourceConnection;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var ResourceConnection $connection */
$connection = Bootstrap::getObjectManager()->get(ResourceConnection::class);
$connection->getConnection()->delete($connection->getTableName('inventory_source'), [
SourceInterface::SOURCE_CODE . ' IN (?)' => ['pickup']
]);
8 changes: 8 additions & 0 deletions app/code/Magento/InventoryInStorePickup/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@
</arguments>
</type>
<preference for="Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface" type="Magento\InventoryInStorePickup\Model\PickupLocation\Mapper\CreateFromSource" />
<type name="Magento\InventoryInStorePickupApi\Model\Mapper">
<arguments>
<argument name="map" xsi:type="array">
<item name="extension_attributes.frontend_name" xsi:type="string">name</item>
<item name="extension_attributes.frontend_description" xsi:type="string">description</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ public function getPostcode(): ?string;
*/
public function getPhone(): ?string;

/**
* Get Pickup Location open hours.
*
* @return string[]|null
*/
public function getOpenHours(): ?array;

/**
* Set Extension Attributes for Pickup Location.
*
Expand Down

0 comments on commit 7559630

Please sign in to comment.