|
| 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 | +} |
0 commit comments