Skip to content

Commit ff30e59

Browse files
committed
Tests for UniteTheTypes + verify param is variadic
1 parent a60f4e5 commit ff30e59

10 files changed

+162
-4
lines changed

src/Exercise/UniteTheTypes.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public function getArgs(): array
5252
$numbers = array_map(
5353
function (): string {
5454
if ($this->faker->boolean) {
55-
return $this->faker->numberBetween(0, 50);
55+
return (string) $this->faker->numberBetween(0, 50);
5656
}
57-
return $this->faker->randomFloat(3, 0, 50);
57+
return (string) $this->faker->randomFloat(3, 0, 50);
5858
},
5959
range(0, random_int(5, 15))
6060
);
61-
61+
6262
return [$numbers];
6363
}
6464

@@ -135,6 +135,13 @@ public function check(Input $input): ResultInterface
135135
);
136136
}
137137

138+
if (!$firstParam->variadic) {
139+
return Failure::fromNameAndReason(
140+
$this->getName(),
141+
'Function adder\'s first parameter should be variadic in order to accept multiple arguments'
142+
);
143+
}
144+
138145
return new Success('Union type for adder is correct');
139146
}
140-
}
147+
}

test/Exercise/UniteTheTypesTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace PhpSchool\PHP8AppreciateTest\Exercise;
4+
5+
use PhpSchool\PHP8Appreciate\Exercise\UniteTheTypes;
6+
use PhpSchool\PhpWorkshop\Application;
7+
use PhpSchool\PhpWorkshop\Result\Failure;
8+
use PhpSchool\PhpWorkshop\TestUtils\WorkshopExerciseTest;
9+
10+
class UniteTheTypesTest extends WorkshopExerciseTest
11+
{
12+
public function getExerciseClass(): string
13+
{
14+
return UniteTheTypes::class;
15+
}
16+
17+
public function getApplication(): Application
18+
{
19+
return require __DIR__ . '/../../app/bootstrap.php';
20+
}
21+
22+
public function testFailureWhenNoFunctionNamedAdder(): void
23+
{
24+
$this->runExercise('no-adder-function.php');
25+
26+
$this->assertVerifyWasNotSuccessful();
27+
28+
$this->assertResultsHasFailure(Failure::class, 'No function named adder was found');
29+
}
30+
31+
public function testFailureWhenAdderFunctionHasNoParams(): void
32+
{
33+
$this->runExercise('no-function-params.php');
34+
35+
$this->assertVerifyWasNotSuccessful();
36+
37+
$this->assertResultsHasFailure(Failure::class, 'Function adder has no parameters');
38+
}
39+
40+
public function testFailureWhenAdderFunctionHasNoUnionTypeParam(): void
41+
{
42+
$this->runExercise('no-union-type-param.php');
43+
44+
$this->assertVerifyWasNotSuccessful();
45+
46+
$this->assertResultsHasFailure(
47+
Failure::class,
48+
'Function adder does not use a union type for it\'s first param'
49+
);
50+
}
51+
52+
public function testFailureWhenAdderFunctionHasClassTypeInUnion(): void
53+
{
54+
$this->runExercise('incorrect-union-class-type.php');
55+
56+
$this->assertVerifyWasNotSuccessful();
57+
58+
$this->assertResultsHasFailure(
59+
Failure::class,
60+
'Union type is incorrect, it should only accept the required types'
61+
);
62+
}
63+
64+
public function testFailureWhenAdderFunctionHasIncorrectUnion(): void
65+
{
66+
$this->runExercise('incorrect-union-scalar-type.php');
67+
68+
$this->assertVerifyWasNotSuccessful();
69+
70+
$this->assertResultsHasFailure(
71+
Failure::class,
72+
'Union type is incorrect, it should only accept the required types'
73+
);
74+
}
75+
76+
public function testFailureWhenAdderFunctionParamIsNotVariadic(): void
77+
{
78+
$this->runExercise('union-type-param-not-variadic.php');
79+
80+
$this->assertVerifyWasNotSuccessful();
81+
82+
$this->assertResultsHasFailure(
83+
Failure::class,
84+
'Function adder\'s first parameter should be variadic in order to accept multiple arguments'
85+
);
86+
}
87+
88+
public function testSuccessfulSolution(): void
89+
{
90+
$this->runExercise('correct-union-type-same-order.php');
91+
92+
$this->assertVerifyWasSuccessful();
93+
}
94+
95+
public function testSuccessfulSolutionWithDifferentOrderUnion(): void
96+
{
97+
$this->runExercise('correct-union-type-diff-order.php');
98+
99+
$this->assertVerifyWasSuccessful();
100+
}
101+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
function adder(float|string|int ...$numbers) {
4+
return array_sum($numbers);
5+
}
6+
7+
$nums = $argv;
8+
array_shift($nums);
9+
10+
echo adder(...$nums) . "\n";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
function adder(string|float|int ...$numbers) {
4+
return array_sum($numbers);
5+
}
6+
7+
$nums = $argv;
8+
array_shift($nums);
9+
10+
echo adder(...$nums) . "\n";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
function adder(int|float|Stringable $numbers) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
function adder(int|float|bool $numbers) {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
function lol()
4+
{}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
function adder()
4+
{}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
function adder(int $numbers) {
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
function adder(string|float|int $numbers) {
4+
return array_sum($numbers);
5+
}
6+
7+
$nums = $argv;
8+
array_shift($nums);
9+
10+
echo adder(...$nums) . "\n";

0 commit comments

Comments
 (0)