-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppTestCase.php
175 lines (148 loc) · 4.31 KB
/
AppTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Tests\AppBundle;
use AppBundle\Entity\AppLocation;
use AppBundle\Entity\AppUser;
use AppBundle\Entity\Ride;
use AppBundle\Repository\LocationRepository;
use AppBundle\Repository\RideEventRepository;
use AppBundle\Repository\RideRepository;
use AppBundle\Repository\UserRepository;
use AppBundle\Service\LocationService;
use AppBundle\Service\UserService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
abstract class AppTestCase extends WebTestCase
{
const HOME_LOCATION_LAT = 37.773160;
const HOME_LOCATION_LONG = -122.432444;
const WORK_LOCATION_LAT = 37.7721718;
const WORK_LOCATION_LONG = -122.4310872;
/** @var EntityManagerInterface */
private $em;
protected function setUp()
{
parent::setUp();
self::bootKernel();
$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();
$this->setUpEntityManager();
$this->userRepository = new UserRepository($this->em());
$this->userService = new UserService(new UserRepository($this->em()));
$this->locationService = new LocationService(new LocationRepository($this->em()));
$this->rideRepository = new RideRepository($this->em());
$this->rideEventRepository = new RideEventRepository($this->em());
}
protected function em()
{
return $this->em;
}
protected function save($entity)
{
$this->em->persist($entity);
$this->em->flush();
}
private function setUpEntityManager()
{
$classes = $this->em()->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($this->em);
$tool->dropSchema($classes);
$tool->createSchema($classes);
}
/** @var UserRepository */
protected $userRepository;
/** @var UserService */
protected $userService;
/** @var LocationService */
protected $locationService;
/** @var RideRepository */
protected $rideRepository;
/** @var RideEventRepository */
protected $rideEventRepository;
/** @var AppUser $savedPassenger */
protected $savedPassenger;
/**
* @return AppUser
*/
protected function getSavedUser()
{
return $this->getSavedUserWithName('chris', 'holland');
}
protected function getSavedUserWithName($first, $last)
{
return $this->userService->newUser($first, $last);
}
/**
* @return AppUser
*/
protected function getNewPassenger()
{
$passenger = $this->getSavedUser();
$this->makeUserPassenger($passenger);
return $passenger;
}
/**
* @param $passenger
*/
protected function makeUserPassenger(AppUser $passenger)
{
$this->userService->makeUserPassenger($passenger);
}
/**
* @param $driver
*/
protected function makeUserDriver(AppUser $driver)
{
$this->userService->makeUserDriver($driver);
}
/**
* @param $notPassengerUser
* @return bool
*/
protected function isPassenger(AppUser $notPassengerUser)
{
return $this->userService->isPassenger($notPassengerUser);
}
/**
* @return AppUser
*/
protected function getNewDriver()
{
return $this->getNewDriverWithName('new', 'driver');
}
protected function getNewDriverWithName($first, $last)
{
$driver = $this->getSavedUserWithName($first, $last);
$this->makeUserDriver($driver);
return $driver;
}
/**
* @return Ride
*/
protected function getSavedRide()
{
$ridePassenger = $this->getNewPassenger();
$this->savedPassenger = $this->getServiceUserById($ridePassenger->getId());
$departure = $this->getSavedHomeLocation();
$ride = new Ride($this->savedPassenger, $departure);
$this->rideRepository->save($ride);
return $ride;
}
/**
* @return AppLocation
*/
protected function getSavedHomeLocation()
{
return $this->locationService->getLocation(
self::HOME_LOCATION_LAT,
self::HOME_LOCATION_LONG
);
}
/**
* @param $userId
* @return AppUser
*/
protected function getServiceUserById($userId)
{
return $this->userService->getUserById($userId);
}
}