Skip to content

Commit d2a3237

Browse files
committed
refactor updatesitetest to not rely on db
1 parent 15116de commit d2a3237

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

tests/Unit/Jobs/UpdateSiteTest.php

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@
55
use App\Exceptions\UpdateException;
66
use App\Jobs\UpdateSite;
77
use App\Models\Site;
8-
use App\Models\Update;
98
use App\RemoteSite\Connection;
109
use App\RemoteSite\Responses\FinalizeUpdate;
1110
use App\RemoteSite\Responses\GetUpdate;
1211
use App\RemoteSite\Responses\HealthCheck;
1312
use App\RemoteSite\Responses\PrepareUpdate;
14-
use Illuminate\Foundation\Testing\RefreshDatabase;
13+
use Illuminate\Database\Eloquent\Relations\HasMany;
1514
use Illuminate\Support\Facades\App;
1615
use Illuminate\Support\Facades\Log;
1716
use Tests\TestCase;
1817

1918
class UpdateSiteTest extends TestCase
2019
{
21-
use RefreshDatabase;
22-
2320
public function testJobQuitsIfTargetVersionIsEqualOrNewer()
2421
{
2522
$site = $this->getSiteMock(['checkHealth' => $this->getHealthCheckMock(["cms_version" => "1.0.0"])]);
@@ -105,25 +102,24 @@ public function testJobFailsIfFinalizeUpdateReturnsFalse()
105102

106103
public function testJobWritesFailLogOnFailing()
107104
{
108-
$siteMock = $this->getMockBuilder(Site::class)
109-
->onlyMethods(['getConnectionAttribute', 'getFrontendStatus'])
110-
->getMock();
111-
112-
$siteMock->id = 1;
113-
$siteMock->url = "http://example.org";
114-
$siteMock->cms_version = "1.0.0";
105+
$siteMock = $this->getSiteMock(
106+
[
107+
'checkHealth' => $this->getHealthCheckMock(),
108+
'getUpdate' => $this->getGetUpdateMock("1.0.1"),
109+
'prepareUpdate' => $this->getPrepareUpdateMock(),
110+
'finalizeUpdate' => $this->getFinalizeUpdateMock(false)
111+
],
112+
[
113+
'result' => false,
114+
'old_version' => '1.0.0',
115+
'new_version' => '1.0.1',
116+
'failed_message' => 'This is a test',
117+
'failed_step' => 'finalize'
118+
]
119+
);
115120

116121
$object = new UpdateSite($siteMock, "1.0.1");
117122
$object->failed(new UpdateException("finalize", "This is a test"));
118-
119-
$failedUpdate = Update::first();
120-
121-
$this->assertEquals(false, $failedUpdate->result);
122-
$this->assertEquals("1.0.0", $failedUpdate->old_version);
123-
$this->assertEquals("1.0.1", $failedUpdate->new_version);
124-
$this->assertEquals("finalize", $failedUpdate->failed_step);
125-
$this->assertEquals("This is a test", $failedUpdate->failed_message);
126-
$this->assertNotEmpty($failedUpdate->failed_trace);
127123
}
128124

129125
public function testJobWritesSuccessLogForSuccessfulJobs()
@@ -134,22 +130,21 @@ public function testJobWritesSuccessLogForSuccessfulJobs()
134130
'getUpdate' => $this->getGetUpdateMock("1.0.1"),
135131
'prepareUpdate' => $this->getPrepareUpdateMock(),
136132
'finalizeUpdate' => $this->getFinalizeUpdateMock(true)
133+
],
134+
[
135+
'result' => true,
136+
'old_version' => '1.0.0',
137+
'new_version' => '1.0.1'
137138
]
138139
);
139140

140141
App::bind(Connection::class, fn () => $this->getSuccessfulExtractionMock());
141142

142143
$object = new UpdateSite($site, "1.0.1");
143144
$object->handle();
144-
145-
$updateRow = Update::first();
146-
147-
$this->assertEquals(true, $updateRow->result);
148-
$this->assertEquals("1.0.0", $updateRow->old_version);
149-
$this->assertEquals("1.0.1", $updateRow->new_version);
150145
}
151146

152-
protected function getSiteMock(array $responses)
147+
protected function getSiteMock(array $responses, array $expectedLogRow = null)
153148
{
154149
$connectionMock = $this->getMockBuilder(Connection::class)
155150
->disableOriginalConstructor()
@@ -163,10 +158,29 @@ function ($method) use ($responses) {
163158
}
164159
);
165160

161+
$updateMock = $this->getMockBuilder(HasMany::class)
162+
->disableOriginalConstructor()
163+
->onlyMethods(['create'])
164+
->getMock();
165+
166+
if ($expectedLogRow) {
167+
$updateMock
168+
->expects($this->once())
169+
->method('create')
170+
->with(self::callback(function ($args) use ($expectedLogRow) {
171+
return $expectedLogRow['result'] === $args['result']
172+
&& $expectedLogRow['old_version'] === $args['old_version']
173+
&& $expectedLogRow['new_version'] === $args['new_version']
174+
&& (empty($expectedLogRow['failed_step']) || $expectedLogRow['failed_step'] === $args['failed_step'])
175+
&& (empty($expectedLogRow['failed_message']) || $expectedLogRow['failed_message'] === $args['failed_message']);
176+
}));
177+
}
178+
166179
$siteMock = $this->getMockBuilder(Site::class)
167-
->onlyMethods(['getConnectionAttribute', 'getFrontendStatus'])
180+
->onlyMethods(['getConnectionAttribute', 'getFrontendStatus', 'updates'])
168181
->getMock();
169182

183+
$siteMock->method('updates')->willReturn($updateMock);
170184
$siteMock->method('getConnectionAttribute')->willReturn($connectionMock);
171185
$siteMock->method('getFrontendStatus')->willReturn(200);
172186
$siteMock->id = 1;

0 commit comments

Comments
 (0)