Skip to content

Commit

Permalink
chore: update unit tests to work with latest phpunit (#298)
Browse files Browse the repository at this point in the history
chore: 
- update unit tests to work with latest phpunit
- update main.yml to use php 8.2 8.3 and 8.4
- set php 8.2 as min version for mono repo
- set bundle min php version to 8.2
  • Loading branch information
julianzimmermann authored Nov 26, 2024
1 parent a92ce5f commit 20e5012
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: [ "8.0", "8.1", "8.2" ]
php-versions: [ "8.2", "8.3", "8.4" ]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"phpstan": "make phpstan"
},
"require": {
"php": ">=8.0",
"php": ">=8.2",
"fond-of-impala/company-user-carts-rest-api-extension": "^1.0.0",
"fond-of-impala/company-user-reference": "^1.0.0",
"spryker/cart": "^7.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceBuilderInterface;
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResourceInterface;
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface;
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
use Symfony\Component\HttpFoundation\Response;

class RestResponseBuilderTest extends Unit
Expand Down Expand Up @@ -210,6 +211,8 @@ static function (RestErrorMessageTransfer $restErrorMessageTransfer) {
*/
public function testBuildRestResponse(): void
{
$self = $this;

$uuid = 'e6b02939-18fc-4857-837e-f0e8063c306e';
$groupKey = 'foo.bar-1';
$companyUserReference = 'FOO--CU-1';
Expand All @@ -223,23 +226,36 @@ public function testBuildRestResponse(): void
->with($this->quoteTransferMock)
->willReturn($this->restCartsAttributesTransferMock);

$this->restResourceBuilderMock->expects(static::atLeastOnce())
$callCount = $this->atLeastOnce();
$this->restResourceBuilderMock->expects($callCount)
->method('createRestResource')
->withConsecutive(
[
CompanyUserCartsRestApiConfig::RESOURCE_COMPANY_USER_CARTS,
$uuid,
$this->restCartsAttributesTransferMock,
],
[
CompanyUserCartsRestApiConfig::RESOURCE_CART_ITEMS,
$groupKey,
$this->restItemsAttributesTransferMock,
],
)->willReturnOnConsecutiveCalls(
$this->restResourceMock,
$this->relatedRestResourceMock,
);
->willReturnCallback(static function (string $type, ?string $id = null, ?AbstractTransfer $attributeTransfer = null) use ($self, $callCount, $uuid, $groupKey) {
/** @phpstan-ignore-next-line */
if (method_exists($callCount, 'getInvocationCount')) {
/** @phpstan-ignore-next-line */
$count = $callCount->getInvocationCount();
} else {
/** @phpstan-ignore-next-line */
$count = $callCount->numberOfInvocations();
}

switch ($count) {
case 1:
$self->assertSame(CompanyUserCartsRestApiConfig::RESOURCE_COMPANY_USER_CARTS, $type);
$self->assertSame($uuid, $id);
$self->assertSame($self->restCartsAttributesTransferMock, $attributeTransfer);

return $self->restResourceMock;
case 2:
$self->assertSame(CompanyUserCartsRestApiConfig::RESOURCE_CART_ITEMS, $type);
$self->assertSame($groupKey, $id);
$self->assertSame($self->restItemsAttributesTransferMock, $attributeTransfer);

return $self->relatedRestResourceMock;
}

throw new Exception('Unexpected call count');
});

$this->restResourceMock->expects(static::atLeastOnce())
->method('setPayload')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayObject;
use Codeception\Test\Unit;
use Exception;
use FondOfImpala\Zed\CompanyUserCartsRestApi\Business\Finder\ItemFinderInterface;
use FondOfImpala\Zed\CompanyUserCartsRestApi\Business\Grouper\ItemsGrouperInterface;
use FondOfImpala\Zed\CompanyUserCartsRestApi\Business\Mapper\ItemMapperInterface;
Expand Down Expand Up @@ -131,6 +132,8 @@ protected function _before(): void
*/
public function testCategorize(): void
{
$self = $this;

$newQuantities = [2, 0, 2];
$currentQuantities = [1, 1];

Expand All @@ -143,29 +146,70 @@ public function testCategorize(): void
->method('getItems')
->willReturn(new ArrayObject($this->restCartItemTransferMocks));

$this->itemFinderMock->expects(static::atLeastOnce())
$callCount = $this->atLeastOnce();
$this->itemFinderMock->expects($callCount)
->method('findInGroupedItemsByRestCartItem')
->withConsecutive(
[$this->itemTransferMocks, $this->restCartItemTransferMocks[0]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[1]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
null,
$this->itemTransferMocks['foo.bar-1'],
$this->itemTransferMocks['foo.bar-2'],
);

$this->itemMapperMock->expects(static::atLeastOnce())
->willReturnCallback(static function (array $groupedItemTransfers, RestCartItemTransfer $restCartItemTransfer) use ($self, $callCount) {
/** @phpstan-ignore-next-line */
if (method_exists($callCount, 'getInvocationCount')) {
/** @phpstan-ignore-next-line */
$count = $callCount->getInvocationCount();
} else {
/** @phpstan-ignore-next-line */
$count = $callCount->numberOfInvocations();
}

switch ($count) {
case 1:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[0], $restCartItemTransfer);

return null;
case 2:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[1], $restCartItemTransfer);

return $self->itemTransferMocks['foo.bar-1'];
case 3:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[2], $restCartItemTransfer);

return $self->itemTransferMocks['foo.bar-2'];
}

throw new Exception('Unexpected call count');
});

$callCount2 = $this->atLeastOnce();
$this->itemMapperMock->expects($callCount2)
->method('fromRestCartItem')
->withConsecutive(
[$this->restCartItemTransferMocks[0]],
[$this->restCartItemTransferMocks[1]],
[$this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
$this->newItemTransferMocks[0],
$this->newItemTransferMocks[1],
$this->newItemTransferMocks[2],
);
->willReturnCallback(static function (RestCartItemTransfer $restCartItemTransfer) use ($self, $callCount2) {
/** @phpstan-ignore-next-line */
if (method_exists($callCount2, 'getInvocationCount')) {
/** @phpstan-ignore-next-line */
$count = $callCount2->getInvocationCount();
} else {
/** @phpstan-ignore-next-line */
$count = $callCount2->numberOfInvocations();
}

switch ($count) {
case 1:
$self->assertSame($self->restCartItemTransferMocks[0], $restCartItemTransfer);

return $self->newItemTransferMocks[0];
case 2:
$self->assertSame($self->restCartItemTransferMocks[1], $restCartItemTransfer);

return $self->newItemTransferMocks[1];
case 3:
$self->assertSame($self->restCartItemTransferMocks[2], $restCartItemTransfer);

return $self->newItemTransferMocks[2];
}

throw new Exception('Unexpected call count');
});

$this->restCartItemTransferMocks[0]->expects(static::atLeastOnce())
->method('getQuantity')
Expand Down Expand Up @@ -226,6 +270,8 @@ public function testCategorize(): void
*/
public function testCategorizeWithString(): void
{
$self = $this;

$newQuantities = [2, 0, ''];
$currentQuantities = [1, 1];

Expand All @@ -238,29 +284,70 @@ public function testCategorizeWithString(): void
->method('getItems')
->willReturn(new ArrayObject($this->restCartItemTransferMocks));

$this->itemFinderMock->expects(static::atLeastOnce())
$callCount = $this->atLeastOnce();
$this->itemFinderMock->expects($callCount)
->method('findInGroupedItemsByRestCartItem')
->withConsecutive(
[$this->itemTransferMocks, $this->restCartItemTransferMocks[0]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[1]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
null,
$this->itemTransferMocks['foo.bar-1'],
$this->itemTransferMocks['foo.bar-2'],
);

$this->itemMapperMock->expects(static::atLeastOnce())
->willReturnCallback(static function (array $groupedItemTransfers, RestCartItemTransfer $restCartItemTransfer) use ($self, $callCount) {
/** @phpstan-ignore-next-line */
if (method_exists($callCount, 'getInvocationCount')) {
/** @phpstan-ignore-next-line */
$count = $callCount->getInvocationCount();
} else {
/** @phpstan-ignore-next-line */
$count = $callCount->numberOfInvocations();
}

switch ($count) {
case 1:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[0], $restCartItemTransfer);

return null;
case 2:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[1], $restCartItemTransfer);

return $self->itemTransferMocks['foo.bar-1'];
case 3:
$self->assertSame($self->itemTransferMocks, $groupedItemTransfers);
$self->assertSame($self->restCartItemTransferMocks[2], $restCartItemTransfer);

return $self->itemTransferMocks['foo.bar-2'];
}

throw new Exception('Unexpected call count');
});

$callCount2 = $this->atLeastOnce();
$this->itemMapperMock->expects($callCount2)
->method('fromRestCartItem')
->withConsecutive(
[$this->restCartItemTransferMocks[0]],
[$this->restCartItemTransferMocks[1]],
[$this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
$this->newItemTransferMocks[0],
$this->newItemTransferMocks[1],
$this->newItemTransferMocks[2],
);
->willReturnCallback(static function (RestCartItemTransfer $restCartItemTransfer) use ($self, $callCount2) {
/** @phpstan-ignore-next-line */
if (method_exists($callCount2, 'getInvocationCount')) {
/** @phpstan-ignore-next-line */
$count = $callCount2->getInvocationCount();
} else {
/** @phpstan-ignore-next-line */
$count = $callCount2->numberOfInvocations();
}

switch ($count) {
case 1:
$self->assertSame($self->restCartItemTransferMocks[0], $restCartItemTransfer);

return $self->newItemTransferMocks[0];
case 2:
$self->assertSame($self->restCartItemTransferMocks[1], $restCartItemTransfer);

return $self->newItemTransferMocks[1];
case 3:
$self->assertSame($self->restCartItemTransferMocks[2], $restCartItemTransfer);

return $self->newItemTransferMocks[2];
}

throw new Exception('Unexpected call count');
});

$this->restCartItemTransferMocks[0]->expects(static::atLeastOnce())
->method('getQuantity')
Expand Down
Loading

0 comments on commit 20e5012

Please sign in to comment.