InfiniteTest.php in Commerce Core 8.2
File
modules/number_pattern/tests/src/Kernel/Plugin/Commerce/NumberPattern/InfiniteTest.php
View source
<?php
namespace Drupal\Tests\commerce_number_pattern\Kernel\Plugin\Commerce\NumberPattern;
use Drupal\commerce_number_pattern\Entity\NumberPattern;
use Drupal\commerce_number_pattern_test\Entity\EntityTestWithStore;
use Drupal\Tests\commerce_number_pattern\Kernel\NumberPatternKernelTestBase;
class InfiniteTest extends NumberPatternKernelTestBase {
public function testGenerate() {
$entity = EntityTestWithStore::create([
'store_id' => $this->store,
]);
$entity
->save();
$number_pattern = NumberPattern::create([
'id' => 'test',
'plugin' => 'infinite',
'configuration' => [
'padding' => 0,
'pattern' => 'INV-[pattern:number]',
'per_store_sequence' => TRUE,
'initial_number' => 1000,
],
]);
$number_pattern_plugin = $number_pattern
->getPlugin();
$initial_sequence = $number_pattern_plugin
->getInitialSequence($entity);
$this
->assertEquals('1000', $initial_sequence
->getNumber());
$this
->assertEquals(\Drupal::time()
->getRequestTime(), $initial_sequence
->getGeneratedTime());
$this
->assertEquals($this->store
->id(), $initial_sequence
->getStoreId());
$this
->assertNull($number_pattern_plugin
->getCurrentSequence($entity));
$this
->assertEquals('INV-1000', $number_pattern_plugin
->generate($entity));
$this
->assertEquals('INV-1001', $number_pattern_plugin
->generate($entity));
$current_sequence = $number_pattern_plugin
->getCurrentSequence($entity);
$this
->assertEquals('1001', $current_sequence
->getNumber());
$this
->assertEquals(\Drupal::time()
->getRequestTime(), $current_sequence
->getGeneratedTime());
$this
->assertEquals($this->store
->id(), $current_sequence
->getStoreId());
$number_pattern_plugin
->resetSequence();
$this
->assertNull($number_pattern_plugin
->getCurrentSequence($entity));
$this
->assertEquals('INV-1000', $number_pattern_plugin
->generate($entity));
$this
->assertEquals('INV-1001', $number_pattern_plugin
->generate($entity));
$configuration = $number_pattern_plugin
->getConfiguration();
$configuration['pattern'] = 'INV-[entity_test_with_store:store_id:target_id]-[pattern:number]';
$number_pattern_plugin
->setConfiguration($configuration);
$this
->assertEquals('INV-1-1002', $number_pattern_plugin
->generate($entity));
$second_store = $this
->createStore('Second store', 'admin2@example.com', 'online', FALSE);
$entity
->setStoreId($second_store
->id());
$entity
->save();
$this
->assertEquals('INV-2-1000', $number_pattern_plugin
->generate($entity));
$configuration = $number_pattern_plugin
->getConfiguration();
$configuration['padding'] = 4;
$configuration['initial_number'] = 1;
$number_pattern_plugin
->setConfiguration($configuration);
$number_pattern_plugin
->resetSequence();
$this
->assertEquals('INV-2-0001', $number_pattern_plugin
->generate($entity));
}
}