Skip to content

Commit 602c0ee

Browse files
committed
Tests
1 parent d6766b3 commit 602c0ee

File tree

7 files changed

+271
-0
lines changed

7 files changed

+271
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace PhpSchool\PHP8AppreciateTest\Exercise;
4+
5+
use PhpSchool\PHP8Appreciate\Exercise\TheReturnOfStatic;
6+
use PhpSchool\PhpWorkshop\Application;
7+
use PhpSchool\PhpWorkshop\Result\Failure;
8+
use PhpSchool\PhpWorkshop\TestUtils\WorkshopExerciseTest;
9+
10+
class TheReturnOfStaticTest extends WorkshopExerciseTest
11+
{
12+
public function getExerciseClass(): string
13+
{
14+
return TheReturnOfStatic::class;
15+
}
16+
17+
public function getApplication(): Application
18+
{
19+
return require __DIR__ . '/../../app/bootstrap.php';
20+
}
21+
22+
public function testFailureWhenNoClassNameFile(): void
23+
{
24+
$this->runExercise('no-file-class.php');
25+
26+
$this->assertVerifyWasNotSuccessful();
27+
28+
$this->assertResultsHasFailure(Failure::class, 'The method withPermissions cannot be found');
29+
}
30+
31+
public function testFailureWhenNoWithPermissionsMethod(): void
32+
{
33+
$this->runExercise('no-with-permissions-method.php');
34+
35+
$this->assertVerifyWasNotSuccessful();
36+
37+
$this->assertResultsHasFailure(Failure::class, 'The method withPermissions cannot be found');
38+
}
39+
40+
public function testFailureWhenFirstStatementIsNotAnAssign(): void
41+
{
42+
$this->runExercise('no-assign-statement.php');
43+
44+
$this->assertVerifyWasNotSuccessful();
45+
46+
$this->assertResultsHasFailure(Failure::class, 'The first statement in withPermissions is not an assign');
47+
}
48+
49+
public function testFailureWhenAssignIsNotClone(): void
50+
{
51+
$this->runExercise('no-clone.php');
52+
53+
$this->assertVerifyWasNotSuccessful();
54+
55+
$this->assertResultsHasFailure(Failure::class, 'The first statement is not a clone of `$this`');
56+
}
57+
58+
public function testFailureWhenAssignIsNotCloneOfThis(): void
59+
{
60+
$this->runExercise('no-clone-this.php');
61+
62+
$this->assertVerifyWasNotSuccessful();
63+
64+
$this->assertResultsHasFailure(Failure::class, 'The first statement is not a clone of `$this`');
65+
}
66+
67+
public function testSuccessfulSolution(): void
68+
{
69+
$this->runExercise('solution-correct.php');
70+
71+
$this->assertVerifyWasSuccessful();
72+
}
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
class File
4+
{
5+
private ?string $permissions = null;
6+
7+
public function withPermissions(string $permissions): static
8+
{
9+
new static;
10+
$clone = clone $this;
11+
$clone->permissions = $permissions;
12+
return $clone;
13+
}
14+
}
15+
16+
class Image extends File
17+
{
18+
private ?string $ext = null;
19+
private ?string $crop = null;
20+
21+
public function withExt(string $ext): static
22+
{
23+
$clone = clone $this;
24+
$clone->ext = $ext;
25+
return $clone;
26+
}
27+
28+
public function withCrop(string $crop): static
29+
{
30+
$clone = clone $this;
31+
$clone->crop = $crop;
32+
return $clone;
33+
}
34+
}
35+
36+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
37+
var_dump($image);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class File
4+
{
5+
private ?string $permissions = null;
6+
7+
public function withPermissions(string $permissions): static
8+
{
9+
$clone = clone (new static());
10+
$clone->permissions = $permissions;
11+
return $clone;
12+
}
13+
}
14+
15+
class Image extends File
16+
{
17+
private ?string $ext = null;
18+
private ?string $crop = null;
19+
20+
public function withExt(string $ext): static
21+
{
22+
$clone = clone $this;
23+
$clone->ext = $ext;
24+
return $clone;
25+
}
26+
27+
public function withCrop(string $crop): static
28+
{
29+
$clone = clone $this;
30+
$clone->crop = $crop;
31+
return $clone;
32+
}
33+
}
34+
35+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
36+
var_dump($image);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class File
4+
{
5+
private ?string $permissions = null;
6+
7+
public function withPermissions(string $permissions): static
8+
{
9+
$clone = new static;
10+
$clone->permissions = $permissions;
11+
return $clone;
12+
}
13+
}
14+
15+
class Image extends File
16+
{
17+
private ?string $ext = null;
18+
private ?string $crop = null;
19+
20+
public function withExt(string $ext): static
21+
{
22+
$clone = clone $this;
23+
$clone->ext = $ext;
24+
return $clone;
25+
}
26+
27+
public function withCrop(string $crop): static
28+
{
29+
$clone = clone $this;
30+
$clone->crop = $crop;
31+
return $clone;
32+
}
33+
}
34+
35+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
36+
var_dump($image);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class Image extends File
4+
{
5+
private ?string $ext = null;
6+
private ?string $crop = null;
7+
8+
public function withExt(string $ext): static
9+
{
10+
$clone = clone $this;
11+
$clone->ext = $ext;
12+
return $clone;
13+
}
14+
15+
public function withCrop(string $crop): static
16+
{
17+
$clone = clone $this;
18+
$clone->crop = $crop;
19+
return $clone;
20+
}
21+
}
22+
23+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
24+
var_dump($image);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class File
4+
{
5+
private ?string $permissions = null;
6+
}
7+
8+
class Image extends File
9+
{
10+
private ?string $ext = null;
11+
private ?string $crop = null;
12+
13+
public function withExt(string $ext): static
14+
{
15+
$clone = clone $this;
16+
$clone->ext = $ext;
17+
return $clone;
18+
}
19+
20+
public function withCrop(string $crop): static
21+
{
22+
$clone = clone $this;
23+
$clone->crop = $crop;
24+
return $clone;
25+
}
26+
}
27+
28+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
29+
var_dump($image);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class File
4+
{
5+
private ?string $permissions = null;
6+
7+
public function withPermissions(string $permissions): static
8+
{
9+
$clone = clone $this;
10+
$clone->permissions = $permissions;
11+
return $clone;
12+
}
13+
}
14+
15+
class Image extends File
16+
{
17+
private ?string $ext = null;
18+
private ?string $crop = null;
19+
20+
public function withExt(string $ext): static
21+
{
22+
$clone = clone $this;
23+
$clone->ext = $ext;
24+
return $clone;
25+
}
26+
27+
public function withCrop(string $crop): static
28+
{
29+
$clone = clone $this;
30+
$clone->crop = $crop;
31+
return $clone;
32+
}
33+
}
34+
35+
$image = (new Image())->withPermissions('w+')->withExt('jpeg')->withCrop('16x9');
36+
var_dump($image);

0 commit comments

Comments
 (0)