Skip to content

Commit

Permalink
Simplify integration tests (RAD-745)
Browse files Browse the repository at this point in the history
  • Loading branch information
foglcz committed Dec 5, 2017
1 parent c7c0da3 commit 53588a1
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 331 deletions.
62 changes: 26 additions & 36 deletions tests/integration/RequestBuilder/CampaignRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lmc\Matej\IntegrationTests\RequestBuilder;

use Lmc\Matej\Exception\RequestException;
use Lmc\Matej\Exception\LogicException;
use Lmc\Matej\IntegrationTests\IntegrationTestCase;
use Lmc\Matej\Model\Command\Sorting;
use Lmc\Matej\Model\Command\UserRecommendation;
Expand All @@ -13,27 +13,15 @@
class CampaignRequestBuilderTest extends IntegrationTestCase
{
/** @test */
public function shouldExecuteRecommendationCommandOnly(): void
public function shouldThrowExceptionWhenSendingBlankRequest(): void
{
$response = $this->createMatejInstance()
->request()
->campaign()
->addRecommendation($this->createRecommendationCommand())
->send();

$this->assertResponseCommandStatuses($response, 'OK');
}
$this->expectException(LogicException::class);
$this->expectExceptionMessage('At least one command must be added to the builder before sending the request');

/** @test */
public function shouldExecuteSortingCommandOnly(): void
{
$response = $this->createMatejInstance()
$this->createMatejInstance()
->request()
->campaign()
->addSorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC']))
->send();

$this->assertResponseCommandStatuses($response, 'OK');
}

/** @test */
Expand All @@ -42,35 +30,37 @@ public function shouldExecuteRecommendationAndSortingCommands(): void
$response = $this->createMatejInstance()
->request()
->campaign()
->addRecommendation($this->createRecommendationCommand())
->addSorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC']))
->addRecommendation($this->createRecommendationCommand('a'))
->addRecommendations([
$this->createRecommendationCommand('b'),
$this->createRecommendationCommand('c'),
])
->addSorting($this->createSortingCommand('a'))
->addSortings([
$this->createSortingCommand('b'),
$this->createSortingCommand('c'),
])
->send();

$this->assertResponseCommandStatuses($response, 'OK', 'OK');
$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(6));
}

/** @test */
public function shouldExecuteThreeThousandCommandsAndFail(): void
{
$this->expectException(RequestException::class);
$this->expectExceptionCode(400);
$this->expectExceptionMessage('BAD REQUEST');

$builder = $this->createMatejInstance()->request()->campaign();
for ($i = 0; $i < 3000; $i++) {
$builder->addSorting(Sorting::create('integration-test-php-client-user-id-A', ['itemA', 'itemB', 'itemC']));
}
$builder->send();
}

private function createRecommendationCommand(): UserRecommendation
private function createRecommendationCommand(string $letter): UserRecommendation
{
return UserRecommendation::create(
'integration-test-php-client-user-id-A',
'user-' . $letter,
1,
'integration-test-scenario',
1,
3600
);
}

private function createSortingCommand(string $letter): Sorting
{
return Sorting::create(
'user-' . $letter,
['itemA', 'itemB', 'itemC']
);
}
}
154 changes: 30 additions & 124 deletions tests/integration/RequestBuilder/EventsRequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,147 +2,53 @@

namespace Lmc\Matej\IntegrationTests\RequestBuilder;

use Lmc\Matej\Exception\RequestException;
use Lmc\Matej\Exception\LogicException;
use Lmc\Matej\IntegrationTests\IntegrationTestCase;
use Lmc\Matej\Model\Command\Interaction;
use Lmc\Matej\Model\Command\ItemProperty;
use Lmc\Matej\Model\Command\UserMerge;
use Lmc\Matej\RequestBuilder\EventsRequestBuilder;

/**
* @covers \Lmc\Matej\RequestBuilder\EventsRequestBuilder
*/
class EventsRequestBuilderTest extends IntegrationTestCase
{
/** @test */
public function shouldExecuteInteractionCommandOnly(): void
public function shouldThrowExceptionWhenSendingBlankRequest(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder);
$response = $builder->send();
$this->expectException(LogicException::class);
$this->expectExceptionMessage('At least one command must be added to the builder before sending the request');

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(1));
}

/** @test */
public function shouldExecuteUserMergeCommandOnly(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendUserMergeCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(1));
}

/** @test */
public function shouldExecuteItemPropertyCommandOnly(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendItemPropertyCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(1));
}

/** @test */
public function shouldExecuteInteractionAndUserMergeCommandsOnly(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder);
$this->appendUserMergeCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(2));
}

/** @test */
public function shouldExecuteInteractionAndItemPropertyCommandsOnly(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder);
$this->appendItemPropertyCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(2));
}

/** @test */
public function shouldExecuteUserMergeAndItemPropertyCommandsOnly(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendUserMergeCommands($builder);
$this->appendItemPropertyCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(2));
$this->createMatejInstance()
->request()
->events()
->send();
}

/** @test */
public function shouldExecuteInteractionAndUserMergeAndItemPropertyCommands(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder);
$this->appendUserMergeCommands($builder);
$this->appendItemPropertyCommands($builder);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(3));
}

/** @test */
public function shouldExecuteOneThousandCommands(): void
{
$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder, null, 1);
$this->appendUserMergeCommands($builder, 1);
$this->appendItemPropertyCommands($builder, 2);
$this->appendInteractionCommands($builder, null, 332);
$this->appendUserMergeCommands($builder, 332);
$this->appendItemPropertyCommands($builder, 332);
$response = $builder->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(1000));
}

/** @test */
public function shouldExecuteThreeThousandCommandsAndFail(): void
{
$this->expectException(RequestException::class);
$this->expectExceptionCode(400);
$this->expectExceptionMessage('BAD REQUEST');

$builder = $this->createMatejInstance()->request()->events();
$this->appendInteractionCommands($builder, null, 1000);
$this->appendUserMergeCommands($builder, 1000);
$this->appendItemPropertyCommands($builder, 1000);
$builder->send();
}

private function appendInteractionCommands(EventsRequestBuilder $builder, int $seed = null, int $amount = 1): void
{
$map = ['bookmark', 'detailView', 'purchase', 'rating'];
for ($i = 0; $i < $amount; $i++) {
$constructor = $map[($seed ?? $i) % 4];
$builder->addInteraction(Interaction::$constructor('integration-test-php-client-user-id-' . $i, 'test-item-id'));
}
}

private function appendUserMergeCommands(EventsRequestBuilder $builder, int $amount = 1): void
{
for ($i = 0; $i < $amount; $i++) {
$builder->addUserMerge(UserMerge::mergeInto(
'integration-test-php-client-target-user-id-' . $i,
'integration-test-php-client-source-user-id-' . $i
));
}
}

private function appendItemPropertyCommands(EventsRequestBuilder $builder, int $amount = 1): void
{
for ($i = 0; $i < $amount; $i++) {
$builder->addItemProperty(
ItemProperty::create('test-item-id' . $i, ['test_boolean' => (bool) ($i % 2)])
);
}
$response = $this->createMatejInstance()
->request()
->events()
->addInteraction(Interaction::bookmark('user-a', 'item-a'))
->addInteractions([
Interaction::detailView('user-b', 'item-a'),
Interaction::rating('user-c', 'item-a'),
Interaction::purchase('user-d', 'item-a'),
])
->addUserMerge(UserMerge::mergeInto('user-a', 'user-b'))
->addUserMerges([
UserMerge::mergeInto('user-a', 'user-c'),
UserMerge::mergeInto('user-a', 'user-d'),
])
->addItemProperty(ItemProperty::create('item-a', ['test_property_a' => 'test-value-a']))
->addItemProperties([
ItemProperty::create('item-a', ['test_property_b' => 'test-value-b']),
ItemProperty::create('item-a', ['test_property_c' => 'test-value-c']),
])
->send();

$this->assertResponseCommandStatuses($response, ...$this->generateOkStatuses(10));
}
}
Loading

0 comments on commit 53588a1

Please sign in to comment.