/
BusinessControllerTest.php
159 lines (138 loc) · 4.74 KB
/
BusinessControllerTest.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
<?php
namespace TheRestartProject\RepairDirectory\Tests\Feature\Http\Controllers\Admin;
use TheRestartProject\Fixometer\Domain\Entities\User;
use TheRestartProject\RepairDirectory\Domain\Enums\PublishingStatus;
use TheRestartProject\RepairDirectory\Domain\Models\Business;
use TheRestartProject\RepairDirectory\Domain\Enums\ReviewSource;
use TheRestartProject\RepairDirectory\Domain\Models\Point;
use TheRestartProject\RepairDirectory\Domain\Repositories\BusinessRepository;
use TheRestartProject\RepairDirectory\Testing\DatabaseMigrations;
use TheRestartProject\RepairDirectory\Testing\FixometerDatabaseMigrations;
use TheRestartProject\RepairDirectory\Tests\IntegrationTestCase;
/**
* Admin\BusinessController Test
*
* @category Test
* @package TheRestartProject\RepairDirectory\Tests\Feature\Http\Controllers\Admin;
* @author Joaquim d'Souza <joaquim@outlandish.com>
* @license GPLv2 https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
* @link http://tactician.thephpleague.com/
*/
class BusinessControllerTest extends IntegrationTestCase
{
use DatabaseMigrations;
use FixometerDatabaseMigrations;
/**
* Asserts that the BusinessController->create function creates a new Business
* and persists this in the database
*
* @return void
*
* @test
*/
public function i_can_create()
{
$user = entity(User::class)->create(['role' => User::HOST]);
$this->be($user);
/**
* The business to create
*
* @var Business $business
*/
$business = entity(Business::class, 'real')->make();
$response = $this->post(
route('admin.business.create'),
$this->convertBusinessToHttpData($business)
);
$response->assertStatus(302);
$this->assertDatabaseHas(
'businesses',
[
'uid' => 1,
'name' => $business->getName()
]
);
$businessRepository = $this->app->make(BusinessRepository::class);
$this->assertEquals(new Point(51.3813963, -2.3613877), $businessRepository->findById(1)->getGeolocation());
}
/**
* Asserts that the BusinessController->update function updates an existing Business
* and persists this in the database
*
* @return void
*
* @test
*/
public function i_can_update()
{
$user = entity(User::class)->create(['role' => User::HOST]);
$this->be($user);
/**
* The business to create
*
* @var Business $business
*/
$business = entity(Business::class)->create();
$business->setName('This is a new name');
$business->setDescription('This is a new description.');
$response = $this->put(
route('admin.business.update', ['id' => $business->getUid()]),
$this->convertBusinessToHttpData($business)
);
$response->assertStatus(302);
$this->assertDatabaseHas(
'businesses',
[
'uid' => 1,
'name' => 'This is a new name',
'description' => 'This is a new description.'
]
);
}
/**
* Converts a business into http data
*
* @param Business $business The business to convert
*
* @return array
*/
protected function convertBusinessToHttpData(Business $business)
{
$data = $business->toArray();
if (array_key_exists('warrantyOffered', $data)) {
$data['warrantyOffered'] = 'Yes';
}
if (array_key_exists('productsRepaired', $data)) {
$data['productsRepaired'] = '';
}
if (array_key_exists('authorisedBrands', $data)) {
$data['authorisedBrands'] = '';
}
return $data;
}
/**
* Tests that the scraping of reviews works (currently only for Google Places).
*
* @return void
*
* @test
*/
public function i_can_scrape_review()
{
$user = entity(User::class)->create(['role' => User::HOST]);
$this->be($user);
$response = $this->get(
route(
'admin.business.scrape-review',
[
'url' => 'https://www.google.co.uk/maps/place/KFC/@51.3963959,-2.4904243,12z/data=!4m8!1m2!2m1!1skfc!3m4!1s0x0:0xdf6f3803ac00dc83!8m2!3d51.3795758!4d-2.3584342'
]
)
);
$response->assertStatus(200);
$content = $response->decodeResponseJson();
self::assertEquals(ReviewSource::GOOGLE, $content['reviewSource']);
self::assertGreaterThan(2, $content['reviewAggregation']['averageScore']);
self::assertLessThan(4, $content['reviewAggregation']['averageScore']);
}
}