Skip to content

Commit

Permalink
Refactor rezone test
Browse files Browse the repository at this point in the history
  • Loading branch information
WaveHack committed Jul 28, 2017
1 parent 62ca995 commit 9e9166c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/resources/views/pages/dominion/rezone.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col-sm-12 col-md-9">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"><i class="ra ra-cycle"></i> Re-zone land</h3>
<h3 class="box-title"><i class="ra ra-cycle"></i> Re-zone Land</h3>
</div>
<form action="{{ route('dominion.rezone') }}" method="post" role="form">
{!! csrf_field() !!}
Expand Down
29 changes: 28 additions & 1 deletion tests/AbstractBrowserKitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OpenDominion\Models\Realm;
use OpenDominion\Models\Round;
use OpenDominion\Models\User;
use OpenDominion\Services\Dominion\SelectorService;

abstract class AbstractBrowserKitTestCase extends TestCase
{
Expand All @@ -34,6 +35,8 @@ protected function setUp()
// Queue::fake();
}

// todo: move below to a trait so we can also include this in the abstractdusktestcase

/**
* Creates a user for testing purposes.
*
Expand Down Expand Up @@ -107,7 +110,6 @@ protected function createRealm(Round $round, $alignment = 'good')
* @param Round $round
* @param Race $race
* @return Dominion
* @internal param Realm $realm
*/
protected function createDominion(User $user, Round $round, Race $race = null)
{
Expand All @@ -123,4 +125,29 @@ protected function createDominion(User $user, Round $round, Race $race = null)

return $dominion;
}

/**
* @param Dominion $dominion
* @return Dominion
*/
protected function selectDominion(Dominion $dominion)
{
$dominionSelectorService = app(SelectorService::class);

$dominionSelectorService->selectUserDominion($dominion);

return $dominion;
}

/**
* @param User $user
* @param Round $round
* @param Race $race
* @return Dominion
*/
protected function createAndSelectDominion(User $user, Round $round, Race $race = null)
{
$dominion = $this->createDominion($user, $round, $race);
return $this->selectDominion($dominion);
}
}
67 changes: 34 additions & 33 deletions tests/Feature/Dominion/RezoneTest.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
<?php

namespace Tests\Feature\Dominion;
namespace OpenDominion\Tests\Feature\Dominion;

use CoreDataSeeder;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use OpenDominion\Models\Dominion;
use OpenDominion\Services\DominionSelectorService;
use OpenDominion\Tests\AbstractBrowserKitTestCase;

class RezoneTest extends AbstractBrowserKitTestCase
{
use DatabaseMigrations;

/** @var \OpenDominion\Models\Dominion */
protected $originalDominion;
/** @var \OpenDominion\Services\DominionSelectorService */
protected $dominionSelectorService;

const REZONE_COST = 250;
/** @var Dominion */
protected $dominion;

public function setUp()
{
parent::setUp();

$this->seed(CoreDataSeeder::class);

$user = $this->createAndImpersonateUser();
$round = $this->createRound();
$dominion = $this->createDominion($user, $round);
$dominion->update(['resource_platinum' => 2 * self::REZONE_COST]);
// @TODO: create dominion with specified amounts of land and resources
// so that this test doesn't break if the defaults in the factory change.
$this->dominionSelectorService = app(DominionSelectorService::class);
$this->dominionSelectorService->selectUserDominion($dominion);
$this->originalDominion = clone $this->dominionSelectorService->getUserSelectedDominion();
$this->dominion = $this->createAndSelectDominion($user, $round);
}

/**
Expand All @@ -39,7 +32,7 @@ public function setUp()
public function testOpeningRezonePage()
{
$this->visitRoute('dominion.rezone')
->see('Re-zone land');
->see('Re-zone Land');
}

/**
Expand All @@ -51,12 +44,12 @@ public function testRezoningPlainToMountain()
->type('2', 'remove[plain]')
->type('2', 'add[mountain]')
->press('Re-Zone')
->see('Your land has been re-zoned');
$dominion = $this->dominionSelectorService->getUserSelectedDominion();
$this->assertEquals($this->originalDominion->land_plain - 2, $dominion->land_plain);
$this->assertEquals($this->originalDominion->land_mountain + 2, $dominion->land_mountain);
$this->assertEquals($this->originalDominion->resource_platinum - 2 * self::REZONE_COST,
$dominion->resource_platinum);
->see('Your land has been re-zoned')
->seeInDatabase('dominions', [
'id' => $this->dominion->id,
'land_plain' => 108,
'land_mountain' => 22,
]);
}

/**
Expand All @@ -68,22 +61,25 @@ public function testCreatingLandOutOfNowhereFails()
->type('10', 'add[plain]')
->press('Re-Zone')
->see('One or more errors occurred')
->see('Rezoning must remove and add equal amounts of land.');
$this->assertDominionUnchanged();
->see('Rezoning must remove and add equal amounts of land.')
->assertDominionUnchanged();
}

/**
* Test that rezoning fails if you cannot afford it.
*/
public function testRezoningWithoutEnoughPlatinumFails()
{
$this->dominion->resource_platinum = 0;
$this->dominion->save();

$this->visitRoute('dominion.rezone')
->type('3', 'remove[plain]')
->type('3', 'add[mountain]')
->press('Re-Zone')
->see('One or more errors occurred')
->see('Not enough platinum.');
$this->assertDominionUnchanged();
->see('Not enough platinum.')
->assertDominionUnchanged(['resource_platinum' => 0]);
}

/**
Expand All @@ -96,19 +92,24 @@ public function testRezoningWithoutEnoughBarrenLandFails()
->type('50', 'add[mountain]')
->press('Re-Zone')
->see('One or more errors occurred')
->see('Can only rezone 40 plains');
$this->assertDominionUnchanged();
->see('Can only rezone 40 plains')
->assertDominionUnchanged();
}

/**
* Helper function to assert the affected properties have not been changed.
*
* @param array $attributes
* @return static
*/
protected function assertDominionUnchanged()
protected function assertDominionUnchanged(array $attributes = [])
{
$dominion = $this->dominionSelectorService->getUserSelectedDominion();
foreach (['land_plain', 'land_mountain', 'resource_platinum'] as $affectedProperty) {
$this->assertEquals($this->originalDominion->{$affectedProperty}, $dominion->{$affectedProperty},
'Unexpected change to ' . $affectedProperty);
}
// todo: pull up
return $this->seeInDatabase('dominions', array_merge([
'id' => $this->dominion->id,
'resource_platinum' => 100000,
'land_plain' => 110,
'land_mountain' => 20,
], $attributes));
}
}
2 changes: 1 addition & 1 deletion tests/Feature/DominionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testUserCanSeeStatusPage()
$round = $this->createRound();
$dominion = $this->createDominion($user, $round);
$dominionSelectorService = app(SelectorService::class);

// todo: $this->selectDominion
$dominionSelectorService->selectUserDominion($dominion);

$this->visit('/dominion/status')
Expand Down

0 comments on commit 9e9166c

Please sign in to comment.