AdjustmentItemTest.php in Commerce Core 8.2
File
modules/order/tests/src/Kernel/AdjustmentItemTest.php
View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_price\Price;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class AdjustmentItemTest extends OrderKernelTestBase {
protected $testEntity;
protected function setUp() : void {
parent::setUp();
$field_storage = FieldStorageConfig::create([
'field_name' => 'test_adjustments',
'entity_type' => 'entity_test',
'type' => 'commerce_adjustment',
'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => 'test_adjustments',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
$entity = EntityTest::create([
'name' => 'Test',
]);
$entity
->save();
$this->testEntity = $entity;
}
public function testAdjustmentItem() {
$adjustment_item_list = $this->testEntity->test_adjustments;
$adjustment_item_list
->appendItem(new Adjustment([
'type' => 'custom',
'label' => '10% off',
'amount' => new Price('-1.00', 'USD'),
'percentage' => '0.1',
'source_id' => '1',
]));
$adjustment = $adjustment_item_list
->first()->value;
$this
->assertEquals('custom', $adjustment
->getType());
$this
->assertEquals('10% off', $adjustment
->getLabel());
$this
->assertEquals('-1.00', $adjustment
->getAmount()
->getNumber());
$this
->assertEquals('USD', $adjustment
->getAmount()
->getCurrencyCode());
$this
->assertEquals('1', $adjustment
->getSourceId());
}
}