Skip to content

Commit bc958c7

Browse files
author
Chris Holland
committed
refactored to further minimize couplings between tests and production code.
1 parent 65926ab commit bc958c7

File tree

11 files changed

+91
-46
lines changed

11 files changed

+91
-46
lines changed

src/AppBundle/Entity/AppUser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function __construct($firstName, $lastName)
5959
$this->roles = new ArrayCollection();
6060
}
6161

62+
/**
63+
* @return Uuid
64+
*/
6265
public function getId()
6366
{
6467
return $this->id;

src/AppBundle/Repository/RideRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AppBundle\Entity\AppLocation;
66
use AppBundle\Entity\AppUser;
77
use AppBundle\Entity\Ride;
8+
use Ramsey\Uuid\Uuid;
89

910
class RideRepository extends AppRepository
1011
{
@@ -15,10 +16,10 @@ public function assignDestinationToRide(Ride $ride, AppLocation $destination)
1516
}
1617

1718
/**
18-
* @param $id
19+
* @param Uuid $id
1920
* @return Ride
2021
*/
21-
public function getRideById($id)
22+
public function getRideById(Uuid $id)
2223
{
2324
return $this->em->createQuery(
2425
'select r from E:Ride r where r.id = :id'

src/AppBundle/Repository/UserRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use AppBundle\Entity\AppRole;
66
use AppBundle\Entity\AppUser;
77
use AppBundle\Exception\DuplicateRoleAssignmentException;
8+
use Ramsey\Uuid\Uuid;
89

910
class UserRepository extends AppRepository
1011
{
1112
/**
12-
* @param $userId
13+
* @param Uuid $userId
1314
* @return AppUser
1415
*/
15-
public function getUserById($userId)
16+
public function getUserById(Uuid $userId)
1617
{
1718
return $this->em->createQuery(
1819
'select u from E:AppUser u where u.id = :userId'

src/AppBundle/Service/RideService.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public function acceptRide(Ride $ride, AppUser $driver)
6464
$this->validateUserHasDriverRole($driver);
6565
$this->validateRideIsRequested($ride);
6666

67-
$this->rideEventRepository->markRideStatusByActor(
68-
$ride,
69-
$driver,
70-
RideEventType::accepted()
71-
);
67+
$this->markRide($ride, $driver, RideEventType::accepted());
7268

7369
$this->rideRepository->assignDriverToRide(
7470
$ride,
@@ -84,7 +80,7 @@ public function markRideInProgress(Ride $acceptedRide, AppUser $driver)
8480
$this->validateUserHasDriverRole($driver);
8581
$this->validateAttemptingDriverIsAssignedDriver($acceptedRide, $driver);
8682

87-
$this->rideEventRepository->markRideStatusByActor(
83+
$this->markRide(
8884
$acceptedRide,
8985
$driver,
9086
RideEventType::inProgress()
@@ -97,7 +93,7 @@ public function markRideCompleted(Ride $rideInProgress, AppUser $driver)
9793
$this->validateAttemptingDriverIsAssignedDriver($rideInProgress, $driver);
9894
$this->validateRideIsInProgress($rideInProgress);
9995

100-
$this->rideEventRepository->markRideStatusByActor(
96+
$this->markRide(
10197
$rideInProgress,
10298
$driver,
10399
RideEventType::completed()
@@ -164,4 +160,13 @@ private function validateRideIsInProgress(Ride $rideInProgress)
164160
throw new RideLifeCycleException();
165161
}
166162
}
163+
164+
private function markRide(Ride $ride, AppUser $driver, RideEventType $status)
165+
{
166+
$this->rideEventRepository->markRideStatusByActor(
167+
$ride,
168+
$driver,
169+
$status
170+
);
171+
}
167172
}

src/AppBundle/Service/UserService.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AppBundle\Entity\AppRole;
66
use AppBundle\Entity\AppUser;
77
use AppBundle\Repository\UserRepository;
8+
use Ramsey\Uuid\Uuid;
89

910
class UserService
1011
{
@@ -30,17 +31,17 @@ public function newUser($firstName, $lastName)
3031
}
3132

3233
/**
33-
* @param int $userId
34+
* @param Uuid $userId
3435
* @return AppUser
3536
*/
36-
public function getUserById($userId)
37+
public function getUserById(Uuid $userId)
3738
{
3839
return $this->userRepository->getUserById($userId);
3940
}
4041

4142
public function makeUserDriver(AppUser $user)
4243
{
43-
$this->userRepository->assignRoleToUser($user, AppRole::driver());
44+
$this->assignRole($user, AppRole::driver());
4445
}
4546

4647
public function isDriver(AppUser $user)
@@ -50,11 +51,20 @@ public function isDriver(AppUser $user)
5051

5152
public function makeUserPassenger(AppUser $user)
5253
{
53-
$this->userRepository->assignRoleToUser($user, AppRole::passenger());
54+
$this->assignRole($user, AppRole::passenger());
5455
}
5556

5657
public function isPassenger(AppUser $user)
5758
{
5859
return $user->hasRole(AppRole::passenger());
5960
}
61+
62+
/**
63+
* @param AppUser $user
64+
* @param $role
65+
*/
66+
protected function assignRole(AppUser $user, AppRole $role)
67+
{
68+
$this->userRepository->assignRoleToUser($user, $role);
69+
}
6070
}

tests/AppBundle/AppTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected function getNewDriverWithName($first, $last)
146146
protected function getSavedRide()
147147
{
148148
$ridePassenger = $this->getNewPassenger();
149-
$this->savedPassenger = $this->getUserById($ridePassenger);
149+
$this->savedPassenger = $this->getServiceUserById($ridePassenger->getId());
150150
$departure = $this->getSavedHomeLocation();
151151
$ride = new Ride($this->savedPassenger, $departure);
152152
$this->rideRepository->save($ride);
@@ -168,7 +168,7 @@ protected function getSavedHomeLocation()
168168
* @param $userId
169169
* @return AppUser
170170
*/
171-
protected function getUserById($userId)
171+
protected function getServiceUserById($userId)
172172
{
173173
return $this->userService->getUserById($userId);
174174
}

tests/AppBundle/LocationRepositoryTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testGetExistingLocationByLatLong()
3333
$savedLocation = $this->getSavedLocation();
3434
$lookupLocation = AppLocation::cloneFrom($savedLocation);
3535

36-
$retrievedLocation = $this->locationRepository->getLocation($lookupLocation);
36+
$retrievedLocation = $this->getOrCreateLocation($lookupLocation);
3737

3838
self::assertTrue($retrievedLocation->equals($savedLocation));
3939
}
@@ -43,7 +43,7 @@ public function testCreateAndGetNewLocation()
4343
$this->getSavedLocation();
4444
$workLocation = new AppLocation(self::WORK_LOCATION_LAT, self::WORK_LOCATION_LONG);
4545

46-
$retrievedLocation = $this->locationRepository->getLocation($workLocation);
46+
$retrievedLocation = $this->getOrCreateLocation($workLocation);
4747

4848
self::assertTrue($retrievedLocation->equals($workLocation));
4949
}
@@ -63,4 +63,8 @@ private function getSavedLocation()
6363
return $homeLocation;
6464
}
6565

66+
protected function getOrCreateLocation(AppLocation $lookupLocation)
67+
{
68+
return $this->locationRepository->getLocation($lookupLocation);
69+
}
6670
}

tests/AppBundle/RideEventRepositoryTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\AppBundle;
44

5+
use AppBundle\Entity\AppUser;
56
use AppBundle\Entity\Ride;
67
use AppBundle\Entity\RideEvent;
78
use AppBundle\Entity\RideEventType;
@@ -59,9 +60,7 @@ public function testRideIsCurrentlyRequested()
5960
{
6061
$this->getSavedRequestedRideEvent();
6162

62-
$lastEventForRide = $this->rideEventRepository->getLastEventForRide(
63-
$this->savedRide
64-
);
63+
$lastEventForRide = $this->getLastEvent($this->savedRide);
6564

6665
self::assertTrue($lastEventForRide->is(RideEventType::requested()));
6766
}
@@ -93,14 +92,12 @@ public function testRideIsCurrentlyRejected()
9392

9493
public function testMarkRideAsStatus()
9594
{
96-
$this->rideEventRepository->markRideStatusByActor(
95+
$this->markRide(
9796
$this->savedRide,
9897
$this->savedPassenger,
9998
RideEventType::requested()
10099
);
101-
$lastEventForRide = $this->rideEventRepository->getLastEventForRide(
102-
$this->savedRide
103-
);
100+
$lastEventForRide = $this->getLastEvent($this->savedRide);
104101

105102
self::assertTrue($lastEventForRide->is(RideEventType::requested()));
106103
}
@@ -110,13 +107,11 @@ public function testMarkRideAsStatus()
110107
*/
111108
private function getSavedRequestedRideEvent()
112109
{
113-
$rideEvent = $this->rideEventRepository->markRideStatusByActor(
110+
return $this->markRide(
114111
$this->savedRide,
115112
$this->savedPassenger,
116113
$this->requestedType
117114
);
118-
119-
return $rideEvent;
120115
}
121116

122117
/**
@@ -133,13 +128,25 @@ private function assertLastEventIsOfType(RideEventType $eventTypeToAssert)
133128
$eventTypeToAssert
134129
));
135130

136-
$lastEventForRide = $this->rideEventRepository->getLastEventForRide(
137-
$this->savedRide
138-
);
131+
$lastEventForRide = $this->getLastEvent($this->savedRide);
139132

140133
self::assertFalse($lastEventForRide->is(RideEventType::requested()));
141134
self::assertTrue($lastEventForRide->is($eventTypeToAssert));
142135

143136
return $lastEventForRide;
144137
}
138+
139+
protected function getLastEvent(Ride $ride)
140+
{
141+
return $this->rideEventRepository->getLastEventForRide($ride);
142+
}
143+
144+
protected function markRide(Ride $ride, AppUser $passenger, RideEventType $status)
145+
{
146+
return $this->rideEventRepository->markRideStatusByActor(
147+
$ride,
148+
$passenger,
149+
$status
150+
);
151+
}
145152
}

tests/AppBundle/RideRepositoryTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AppBundle\Entity\AppUser;
66
use AppBundle\Entity\Ride;
7+
use Ramsey\Uuid\Uuid;
78

89
/**
910
* Class RideRepositoryTest
@@ -51,20 +52,18 @@ public function testAssignDriverToRide()
5152
{
5253
/** @var AppUser $driver */
5354
$driver = $this->getSavedUserWithName('Jamie', 'Isaacs');
54-
5555
$rideWithDestination = $this->getRideWithDestination();
5656

5757
$this->rideRepository->assignDriverToRide($rideWithDestination, $driver);
58-
59-
$retrievedRide = $this->rideRepository->getRideById($rideWithDestination->getId());
58+
$retrievedRide = $this->getRideById($rideWithDestination->getId());
6059

6160
self::assertTrue($retrievedRide->isDrivenBy($driver));
6261
}
6362

6463
/**
6564
* @return Ride
6665
*/
67-
private function getRideWithDestination()
66+
protected function getRideWithDestination()
6867
{
6968
$ride = $this->getSavedRide();
7069

@@ -74,8 +73,17 @@ private function getRideWithDestination()
7473
);
7574

7675
/** @var Ride $retrievedRide */
77-
$retrievedRide = $this->rideRepository->getRideById($ride->getId());
76+
$retrievedRide = $this->getRideById($ride->getId());
7877

7978
return $retrievedRide;
8079
}
80+
81+
/**
82+
* @param Uuid $id
83+
* @return Ride
84+
*/
85+
protected function getRideById(Uuid $id)
86+
{
87+
return $this->rideRepository->getRideById($id);
88+
}
8189
}

tests/AppBundle/UserRepositoryTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\AppBundle;
44

55
use AppBundle\Entity\AppRole;
6+
use AppBundle\Entity\AppUser;
67
use AppBundle\Exception\DuplicateRoleAssignmentException;
78

89
class UserRepositoryTest extends AppTestCase
@@ -23,7 +24,7 @@ public function testGetUserById()
2324
{
2425
$savedUser = $this->getSavedUser();
2526

26-
$retrievedUser = $this->userRepository->getUserById($savedUser->getId());
27+
$retrievedUser = $this->getServiceUserById($savedUser->getId());
2728

2829
self::assertSame($savedUser->getId(), $retrievedUser->getId());
2930
self::assertTrue($savedUser->is($retrievedUser));
@@ -43,10 +44,10 @@ public function testUserCanHaveBothRoles()
4344
{
4445
$savedUser = $this->getSavedUser();
4546

46-
$this->userRepository->assignRoleToUser($savedUser, AppRole::driver());
47-
$this->userRepository->assignRoleToUser($savedUser, AppRole::passenger());
47+
$this->assignRoleToUser($savedUser, AppRole::driver());
48+
$this->assignRoleToUser($savedUser, AppRole::passenger());
4849

49-
$retrievedUser = $this->userRepository->getUserById($savedUser->getId());
50+
$retrievedUser = $this->getServiceUserById($savedUser->getId());
5051

5152
self::assertTrue($retrievedUser->hasRole(AppRole::driver()));
5253
self::assertTrue($retrievedUser->hasRole(AppRole::passenger()));
@@ -56,19 +57,24 @@ public function testDuplicateRoleAssignmentThrows()
5657
{
5758
$savedUser = $this->getSavedUser();
5859

59-
$this->userRepository->assignRoleToUser($savedUser, AppRole::driver());
60+
$this->assignRoleToUser($savedUser, AppRole::driver());
6061
$this->expectException(DuplicateRoleAssignmentException::class);
6162

62-
$this->userRepository->assignRoleToUser($savedUser, AppRole::driver());
63+
$this->assignRoleToUser($savedUser, AppRole::driver());
6364
}
6465

6566
private function assertUserHasExpectedRole(AppRole $role)
6667
{
6768
$savedUser = $this->getSavedUser();
6869

69-
$this->userRepository->assignRoleToUser($savedUser, $role);
70+
$this->assignRoleToUser($savedUser, $role);
7071
$retrievedUser = $this->userRepository->getUserById($savedUser->getId());
7172

7273
self::assertTrue($retrievedUser->hasRole($role));
7374
}
75+
76+
protected function assignRoleToUser(AppUser $user, AppRole $role)
77+
{
78+
$this->userRepository->assignRoleToUser($user, $role);
79+
}
7480
}

0 commit comments

Comments
 (0)