Skip to content

Commit c0a944d

Browse files
committed
refactor tests
1 parent 036c7a8 commit c0a944d

24 files changed

+776
-1173
lines changed
Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,51 @@
11
<?php
22

3-
namespace Spatie\ServerMonitor\CheckDefinitions\Test;
4-
5-
use Exception;
63
use Spatie\ServerMonitor\CheckDefinitions\CheckDefinition;
74
use Spatie\ServerMonitor\CheckDefinitions\Diskspace;
85
use Spatie\ServerMonitor\Models\Check;
96
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
10-
use Spatie\ServerMonitor\Test\TestCase;
117
use Symfony\Component\Process\Process;
128

13-
class CheckDefinitionTest extends TestCase
14-
{
15-
/** @var \Spatie\ServerMonitor\CheckDefinitions\Diskspace */
16-
protected $diskspaceCheckDefinition;
17-
18-
/** @var \Spatie\ServerMonitor\Models\Check */
19-
protected $check;
20-
21-
public function setUp(): void
22-
{
23-
parent::setUp();
24-
25-
$this->createHost('localhost', 65000, ['diskspace']);
9+
beforeEach(function () {
10+
$this->createHost('localhost', 65000, ['diskspace']);
2611

27-
$this->check = Check::first();
12+
$this->check = Check::first();
2813

29-
$this->diskspaceCheckDefinition = (new Diskspace())->setCheck($this->check);
30-
}
14+
$this->diskspaceCheckDefinition = (new Diskspace())->setCheck($this->check);
15+
});
3116

32-
/** @test */
33-
public function it_will_mark_the_check_as_failed_when_passing_a_failed_process()
34-
{
35-
$process = $this->getFailedProcess();
17+
it('will mark the check as failed when passing a failed process', function () {
18+
$process = $this->getFailedProcess();
3619

37-
$this->diskspaceCheckDefinition->determineResult($process);
20+
$this->diskspaceCheckDefinition->determineResult($process);
3821

39-
$this->check->fresh();
22+
$this->check->fresh();
4023

41-
$this->assertEquals(CheckStatus::FAILED, $this->check->status);
42-
}
24+
expect($this->check->status)->toEqual(CheckStatus::FAILED);
25+
});
4326

44-
/** @test */
45-
public function it_will_mark_the_check_as_failed_when_a_check_definition_throws_an_exception()
46-
{
47-
$checkDefinition = new class extends CheckDefinition {
48-
public function resolve(Process $process)
49-
{
50-
throw new Exception('my exception message');
51-
}
27+
it('will mark the check as failed when a check definition throws an exception', function () {
28+
$checkDefinition = new class extends CheckDefinition {
29+
public function resolve(Process $process)
30+
{
31+
throw new Exception('my exception message');
32+
}
5233

53-
public function performNextRunInMinutes(): int
54-
{
55-
return 0;
56-
}
57-
};
34+
public function performNextRunInMinutes(): int
35+
{
36+
return 0;
37+
}
38+
};
5839

59-
$checkDefinition->setCheck($this->check);
40+
$checkDefinition->setCheck($this->check);
6041

61-
$process = $this->getSuccessfulProcessWithOutput();
42+
$process = $this->getSuccessfulProcessWithOutput();
6243

63-
$checkDefinition->determineResult($process);
44+
$checkDefinition->determineResult($process);
6445

65-
$this->check->fresh();
46+
$this->check->fresh();
6647

67-
$this->assertEquals(CheckStatus::FAILED, $this->check->status);
48+
expect($this->check->status)->toEqual(CheckStatus::FAILED);
6849

69-
$this->assertEquals('Exception occurred: my exception message', $this->check->last_run_message);
70-
}
71-
}
50+
expect($this->check->last_run_message)->toEqual('Exception occurred: my exception message');
51+
});
Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,26 @@
11
<?php
22

3-
namespace Spatie\ServerMonitor\CheckDefinitions\Test;
4-
53
use Spatie\ServerMonitor\CheckDefinitions\Diskspace;
64
use Spatie\ServerMonitor\Models\Check;
7-
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
8-
use Spatie\ServerMonitor\Test\TestCase;
9-
10-
class DiskspaceTest extends TestCase
11-
{
12-
/** @var \Spatie\ServerMonitor\CheckDefinitions\Diskspace */
13-
protected $diskspaceCheckDefinition;
14-
15-
/** @var \Spatie\ServerMonitor\Models\Check */
16-
protected $check;
17-
18-
public function setUp(): void
19-
{
20-
parent::setUp();
21-
22-
$this->createHost('localhost', 65000, ['diskspace']);
23-
24-
$this->check = Check::first();
25-
26-
$this->diskspaceCheckDefinition = (new Diskspace())->setCheck($this->check);
27-
}
28-
29-
/**
30-
* @test
31-
*
32-
* @dataProvider percentageProvider
33-
*/
34-
public function it_can_handle_its_command_output(int $diskspaceUsed, string $status)
35-
{
36-
$process = $this->getSuccessfulProcessWithOutput(
37-
'Filesystem 512-blocks Used Available Capacity Mounted on\n'.
38-
"/dev/disk1 974700800 830137776 144051024 {$diskspaceUsed}% /"
39-
);
40-
41-
$this->diskspaceCheckDefinition->resolve($process);
42-
43-
$this->check->fresh();
44-
45-
$this->assertStringContains("{$diskspaceUsed}%", $this->check->last_run_message);
46-
$this->assertEquals($status, $this->check->status);
47-
}
48-
49-
public function percentageProvider()
50-
{
51-
return [
52-
[40, CheckStatus::SUCCESS],
53-
[50, CheckStatus::SUCCESS],
54-
[79, CheckStatus::SUCCESS],
55-
[80, CheckStatus::WARNING],
56-
[89, CheckStatus::WARNING],
57-
[90, CheckStatus::FAILED],
58-
[95, CheckStatus::FAILED],
59-
];
60-
}
61-
}
5+
6+
beforeEach(function () {
7+
$this->createHost('localhost', 65000, ['diskspace']);
8+
9+
$this->check = Check::first();
10+
11+
$this->diskspaceCheckDefinition = (new Diskspace())->setCheck($this->check);
12+
});
13+
14+
it('can handle its command output', function (int $diskspaceUsed, string $status) {
15+
$process = $this->getSuccessfulProcessWithOutput(
16+
'Filesystem 512-blocks Used Available Capacity Mounted on\n'.
17+
"/dev/disk1 974700800 830137776 144051024 {$diskspaceUsed}% /"
18+
);
19+
20+
$this->diskspaceCheckDefinition->resolve($process);
21+
22+
$this->check->fresh();
23+
24+
expect($this->check->last_run_message)->toContain("{$diskspaceUsed}%");
25+
expect($this->check->status)->toEqual($status);
26+
})->with('percentage');
Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
11
<?php
22

3-
namespace Spatie\ServerMonitor\CheckDefinitions\Test;
4-
53
use Spatie\ServerMonitor\CheckDefinitions\Elasticsearch;
64
use Spatie\ServerMonitor\Models\Check;
75
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
8-
use Spatie\ServerMonitor\Test\TestCase;
9-
10-
class ElasticsearchTest extends TestCase
11-
{
12-
/** @var \Spatie\ServerMonitor\CheckDefinitions\Elasticsearch */
13-
protected $elasticsearchDefintion;
14-
15-
/** @var \Spatie\ServerMonitor\Models\Check */
16-
protected $check;
17-
18-
public function setUp(): void
19-
{
20-
parent::setUp();
216

22-
$this->createHost('localhost', 65000, ['elasticsearch']);
7+
beforeEach(function () {
8+
$this->createHost('localhost', 65000, ['elasticsearch']);
239

24-
$this->check = Check::first();
10+
$this->check = Check::first();
2511

26-
$this->elasticsearchDefintion = (new Elasticsearch())->setCheck($this->check);
27-
}
12+
$this->elasticsearchDefintion = (new Elasticsearch())->setCheck($this->check);
13+
});
2814

29-
/** @test */
30-
public function it_can_determine_success()
31-
{
32-
$process = $this->getSuccessfulProcessWithOutput(
33-
'output something something lucene_version something something'
34-
);
15+
it('can determine success', function () {
16+
$process = $this->getSuccessfulProcessWithOutput(
17+
'output something something lucene_version something something'
18+
);
3519

36-
$this->elasticsearchDefintion->resolve($process);
20+
$this->elasticsearchDefintion->resolve($process);
3721

38-
$this->check->fresh();
22+
$this->check->fresh();
3923

40-
$this->assertStringContains('is running', $this->check->last_run_message);
41-
$this->assertEquals(CheckStatus::SUCCESS, $this->check->status);
42-
}
24+
expect($this->check->last_run_message)->toContain('is running');
25+
expect($this->check->status)->toEqual(CheckStatus::SUCCESS);
26+
});
4327

44-
/** @test */
45-
public function it_can_determine_failure()
46-
{
47-
$process = $this->getSuccessfulProcessWithOutput(
48-
'output something something something something'
49-
);
28+
it('can determine failure', function () {
29+
$process = $this->getSuccessfulProcessWithOutput(
30+
'output something something something something'
31+
);
5032

51-
$this->elasticsearchDefintion->resolve($process);
33+
$this->elasticsearchDefintion->resolve($process);
5234

53-
$this->check->fresh();
35+
$this->check->fresh();
5436

55-
$this->assertStringContains('is not running', $this->check->last_run_message);
56-
$this->assertEquals(CheckStatus::FAILED, $this->check->status);
57-
}
58-
}
37+
expect($this->check->last_run_message)->toContain('is not running');
38+
expect($this->check->status)->toEqual(CheckStatus::FAILED);
39+
});
Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
11
<?php
22

3-
namespace Spatie\ServerMonitor\CheckDefinitions\Test;
4-
53
use Spatie\ServerMonitor\CheckDefinitions\Memcached;
64
use Spatie\ServerMonitor\Models\Check;
75
use Spatie\ServerMonitor\Models\Enums\CheckStatus;
8-
use Spatie\ServerMonitor\Test\TestCase;
9-
10-
class MemcachedTest extends TestCase
11-
{
12-
/** @var \Spatie\ServerMonitor\CheckDefinitions\Memcached */
13-
protected $memcachedDefinition;
14-
15-
/** @var \Spatie\ServerMonitor\Models\Check */
16-
protected $check;
17-
18-
public function setUp(): void
19-
{
20-
parent::setUp();
216

22-
$this->createHost('localhost', 65000, ['elasticsearch']);
7+
beforeEach(function () {
8+
$this->createHost('localhost', 65000, ['elasticsearch']);
239

24-
$this->check = Check::first();
10+
$this->check = Check::first();
2511

26-
$this->memcachedDefinition = (new Memcached())->setCheck($this->check);
27-
}
12+
$this->memcachedDefinition = (new Memcached())->setCheck($this->check);
13+
});
2814

29-
/** @test */
30-
public function it_can_determine_success()
31-
{
32-
$process = $this->getSuccessfulProcessWithOutput(
33-
'* memcached is running'
34-
);
15+
it('can determine success', function () {
16+
$process = $this->getSuccessfulProcessWithOutput(
17+
'* memcached is running'
18+
);
3519

36-
$this->memcachedDefinition->resolve($process);
20+
$this->memcachedDefinition->resolve($process);
3721

38-
$this->check->fresh();
22+
$this->check->fresh();
3923

40-
$this->assertStringContains('is running', $this->check->last_run_message);
41-
$this->assertEquals(CheckStatus::SUCCESS, $this->check->status);
42-
}
24+
expect($this->check->last_run_message)->toContain('is running');
25+
expect($this->check->status)->toEqual(CheckStatus::SUCCESS);
26+
});
4327

44-
/** @test */
45-
public function it_can_determine_failure()
46-
{
47-
$process = $this->getSuccessfulProcessWithOutput(
48-
''
49-
);
28+
it('can determine failure', function () {
29+
$process = $this->getSuccessfulProcessWithOutput(
30+
''
31+
);
5032

51-
$this->memcachedDefinition->resolve($process);
33+
$this->memcachedDefinition->resolve($process);
5234

53-
$this->check->fresh();
35+
$this->check->fresh();
5436

55-
$this->assertStringContains('is not running', $this->check->last_run_message);
56-
$this->assertEquals(CheckStatus::FAILED, $this->check->status);
57-
}
58-
}
37+
expect($this->check->last_run_message)->toContain('is not running');
38+
expect($this->check->status)->toEqual(CheckStatus::FAILED);
39+
});

0 commit comments

Comments
 (0)