Skip to content

Commit

Permalink
NEXT-22679 - Add array value types
Browse files Browse the repository at this point in the history
  • Loading branch information
keulinho committed Aug 1, 2022
1 parent 13f841d commit 2503d95
Show file tree
Hide file tree
Showing 39 changed files with 346 additions and 1,447 deletions.
1,365 changes: 0 additions & 1,365 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function testUpdateOrder(): void
'stateId' => $this->getStateId(),
'currencyFactor' => 1,
'orderDateTime' => new \DateTime(),
'price' => json_decode(json_encode(new CartPrice(1, 1, 1, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_FREE)), true),
'shippingCosts' => json_decode(json_encode(new CalculatedPrice(1, 1, new CalculatedTaxCollection(), new TaxRuleCollection())), true),
'price' => json_decode(json_encode(new CartPrice(1, 1, 1, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_FREE), \JSON_THROW_ON_ERROR), true),
'shippingCosts' => json_decode(json_encode(new CalculatedPrice(1, 1, new CalculatedTaxCollection(), new TaxRuleCollection()), \JSON_THROW_ON_ERROR), true),
];

$this->getContainer()->get('order.repository')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Flow\Aggregate\FlowSequence\FlowSequenceCollection;
use Shopware\Core\Content\Flow\Dispatching\Action\SendMailAction;
use Shopware\Core\Content\Flow\FlowEntity;
use Shopware\Core\Defaults;
Expand All @@ -24,11 +25,11 @@ class Migration1639992771MoveDataFromEventActionToFlowTest extends TestCase

private TestDataCollection $ids;

private ?Connection $connection;
private Connection $connection;

private ?EntityRepositoryInterface $eventActionRepository;
private EntityRepositoryInterface $eventActionRepository;

private ?EntityRepositoryInterface $flowRepository;
private EntityRepositoryInterface $flowRepository;

public function setUp(): void
{
Expand Down Expand Up @@ -75,7 +76,6 @@ public function testMigrateSimpleEventActionToFlow(): void
$this->eventActionRepository->create($data, Context::createDefaultContext());

$migration = new Migration1639992771MoveDataFromEventActionToFlow();
$migration->internal = true;
$migration->update($this->connection);

$flows = $this->connection->fetchFirstColumn('SELECT `event_name` FROM `flow`');
Expand All @@ -102,7 +102,6 @@ public function testMigrateEventActionToFlowWithSalesChannelAndRule(): void
$this->createEventActionWithSalesChannelAndRule();

$migration = new Migration1639992771MoveDataFromEventActionToFlow();
$migration->internal = true;
$migration->update($this->connection);

$criteria = new Criteria();
Expand All @@ -113,7 +112,8 @@ public function testMigrateEventActionToFlowWithSalesChannelAndRule(): void
$flowSequences = $flow->getSequences();

static::assertSame('checkout.order.placed', $flow->getEventName());
static::assertSame(3, $flowSequences->count());
static::assertInstanceOf(FlowSequenceCollection::class, $flowSequences);
static::assertCount(3, $flowSequences);

foreach ($flowSequences->getElements() as $flowSequence) {
if ($flowSequence->getActionName() === null) {
Expand Down
7 changes: 5 additions & 2 deletions src/Core/Migration/Traits/ImportTranslationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ protected function importTranslation(string $table, Translations $translations,
return new TranslationWriteResult($englishIds, $germanIds);
}

/**
* @return string[]
*/
protected function getLanguageIds(Connection $connection, string $locale): array
{
$ids = $connection->fetchAllAssociative('
$ids = $connection->fetchFirstColumn('
SELECT LOWER(HEX(`language`.id)) as id
FROM `language`
INNER JOIN locale
ON `language`.`locale_id` = `locale`.`id`
AND locale.code = :locale
', ['locale' => $locale]);

return array_unique(array_filter(array_column($ids, 'id')));
return array_unique(array_filter($ids));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

trait StorefrontControllerTestBehaviour
{
/**
* @param array<string, mixed> $data
*/
public function request(string $method, string $path, array $data): Response
{
$browser = KernelLifecycleManager::createBrowser($this->getKernel());
Expand All @@ -24,6 +27,11 @@ public function request(string $method, string $path, array $data): Response
return $browser->getResponse();
}

/**
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
public function tokenize(string $route, array $data): array
{
$requestStack = new RequestStack();
Expand Down
12 changes: 12 additions & 0 deletions tests/migration/Core/V6_3/Migration1590408550AclResourcesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function restoreOldDatabase(): void

/**
* @dataProvider migrationCases
*
* @param array<string, string[]> $roles
*/
public function testMigration(array $roles): void
{
Expand All @@ -96,6 +98,9 @@ public function testMigration(array $roles): void
static::assertEquals($roles, $actual);
}

/**
* @return array<string, array<string, string[]>[]>
*/
public function migrationCases(): array
{
return [
Expand Down Expand Up @@ -123,6 +128,9 @@ public function migrationCases(): array
];
}

/**
* @param array<string, string[]> $roles
*/
private function insert(array $roles): void
{
foreach ($roles as $name => $privileges) {
Expand All @@ -147,8 +155,12 @@ private function insert(array $roles): void
}
}

/**
* @return array<string, string[]>
*/
private function fetchRoles(): array
{
/** @var array{name: string, priv: string}[] $roles */
$roles = $this->connection->fetchAllAssociative("
SELECT `role`.name,
CONCAT(`resource`.`resource`, ':', `resource`.`privilege`) as priv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @internal
* @group slow
* @covers \Shopware\Core\Migration\V6_3\Migration1590758953ProductFeatureSet
*
* @phpstan-type DbColumn array{name: string, type: Type, notnull: bool}
*/
class Migration1590758953ProductFeatureSetTest extends TestCase
{
Expand Down Expand Up @@ -65,6 +67,8 @@ public function initialise(): void

/**
* @dataProvider tableInformationProvider
*
* @param DbColumn[] $expectedColumns
*/
public function testTablesAreComplete(string $table, array $expectedColumns): void
{
Expand Down Expand Up @@ -140,6 +144,9 @@ public function testDefaultFeatureSetTranslationIsCreated(): void
static::assertEquals($expectedTranslations, $actual);
}

/**
* @return array{0: string, 1: DbColumn[]}[]
*/
public function tableInformationProvider(): array
{
return [
Expand All @@ -166,12 +173,15 @@ public function tableInformationProvider(): array
];
}

/**
* @return DbColumn
*/
private static function getColumn(string $name, Type $type, ?bool $notNull = false): array
{
return [
'name' => $name,
'type' => $type,
'notnull' => $notNull,
'notnull' => (bool) $notNull,
];
}

Expand All @@ -188,6 +198,9 @@ private function getJsonType(): Type
->hasNativeJsonType() ? new JsonType() : new TextType();
}

/**
* @return DbColumn[]
*/
private function fetchTableInformation(string $name): array
{
$columns = $this->connection
Expand All @@ -204,13 +217,19 @@ private function fetchTableInformation(string $name): array
}, $columns);
}

/**
* @return array<string|int, mixed>
*/
private function fetchDefaultFeatureSet(): array
{
return (array) $this->connection->fetchAssociative(
'SELECT * FROM `product_feature_set` ORDER BY `created_at` ASC LIMIT 1;'
);
}

/**
* @return array<string, mixed>[]
*/
private function fetchFeatureSetTranslation(): array
{
return $this->connection->fetchAllAssociative(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ protected function rollbackMigrationChanges(): void
$this->connection->executeStatement("DELETE FROM state_machine_state WHERE technical_name = 'authorized'");
}

/**
* @return array{action_name: string, from_state_name: string, to_state_name: string}[]
*/
private function fetchTransitions(): array
{
return $this->connection->fetchAllAssociative("
/** @var array{action_name: string, from_state_name: string, to_state_name: string}[] $result */
$result = $this->connection->fetchAllAssociative("
SELECT trans.action_name, from_state.technical_name as from_state, to_state.technical_name as to_state
FROM state_machine_transition trans
INNER JOIN state_machine_state from_state
Expand All @@ -89,6 +93,8 @@ private function fetchTransitions(): array
ORDER BY trans.action_name, from_state.technical_name, to_state.technical_name
;
");

return $result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/**
* @internal
* @covers \Shopware\Core\Migration\V6_3\Migration1592978289ProductCustomFieldSets
*
* @phpstan-type DbColumn array{name: string, type: Type, notnull: bool}
*/
class Migration1592978289ProductCustomFieldSetsTest extends TestCase
{
Expand Down Expand Up @@ -61,6 +63,8 @@ public function testGlobalColumnExists(): void

/**
* @dataProvider tableInformationProvider
*
* @param DbColumn[] $expectedColumns
*/
public function testTablesAreComplete(string $table, array $expectedColumns): void
{
Expand All @@ -72,6 +76,9 @@ public function testTablesAreComplete(string $table, array $expectedColumns): vo
static::assertEquals($expectedColumns, $actualColumns);
}

/**
* @return array{0: string, 1: DbColumn[]}[]
*/
public function tableInformationProvider(): array
{
return [
Expand All @@ -86,15 +93,21 @@ public function tableInformationProvider(): array
];
}

/**
* @return DbColumn
*/
private static function getColumn(string $name, Type $type, ?bool $notNull = false): array
{
return [
'name' => $name,
'type' => $type,
'notnull' => $notNull,
'notnull' => (bool) $notNull,
];
}

/**
* @return DbColumn[]
*/
private function fetchTableInformation(string $name): array
{
$columns = $this->connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
/**
* @internal
* @covers \Shopware\Core\Migration\V6_3\Migration1595422169AddProductSorting
*
* @phpstan-type Sorting array{url_key: string, fields:string, priority: string, label: string}
*/
class Migration1595422169ProductSortingTest extends TestCase
{
Expand Down Expand Up @@ -134,6 +136,9 @@ public function testMigrationWithFranceAsDefault(): void
$connection->beginTransaction();
}

/**
* @return Sorting[]
*/
private function migrationCases(): array
{
return [
Expand All @@ -146,7 +151,7 @@ private function migrationCases(): array
'priority' => 0,
'naturalSorting' => 0,
],
]),
], \JSON_THROW_ON_ERROR),
'priority' => '1',
'label' => 'A-Z',
],
Expand All @@ -165,13 +170,16 @@ private function migrationCases(): array
'priority' => 0,
'naturalSorting' => 0,
],
]),
], \JSON_THROW_ON_ERROR),
'priority' => '0',
'label' => 'Custom Sort',
],
];
}

/**
* @param Sorting[] $sortings
*/
private function insert(array $sortings): void
{
foreach ($sortings as $sorting) {
Expand All @@ -196,9 +204,13 @@ private function insert(array $sortings): void
}
}

/**
* @return Sorting[]
*/
private function fetchSortings(): array
{
return $this->connection->fetchAllAssociative('
/** @var Sorting[] $result */
$result = $this->connection->fetchAllAssociative('
SELECT `product_sorting`.url_key,
`product_sorting`.fields,
`product_sorting`.priority,
Expand All @@ -208,6 +220,8 @@ private function fetchSortings(): array
ON `product_sorting`.id = `product_sorting_translation`.product_sorting_id
ORDER BY `product_sorting`.priority DESC;
');

return $result;
}

private function fetchSystemConfig(): string
Expand Down
Loading

0 comments on commit 2503d95

Please sign in to comment.