| Conditions | 1 |
| Paths | 1 |
| Total Lines | 56 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 31 | public function testProcessActionUseItemOnlyInInventory(): void |
||
| 32 | { |
||
| 33 | // Stub a location to start in |
||
| 34 | $startingLocation = $this->createStub(Location::class); |
||
| 35 | |||
| 36 | // Create a mock location to move to |
||
| 37 | $responseLocation = $this->createMock(Location::class); |
||
| 38 | $responseLocation->expects($this->once()) |
||
| 39 | ->method('getLocationDetails') |
||
| 40 | ->willReturn('A testy test location.'); |
||
| 41 | |||
| 42 | // Create and configure a response mock |
||
| 43 | $response = $this->createMock(MoveLocationResponse::class); |
||
| 44 | $response->expects($this->once()) |
||
| 45 | ->method('doLocationResponse') |
||
| 46 | ->with($startingLocation) |
||
| 47 | ->willReturn($responseLocation); |
||
| 48 | |||
| 49 | // Create and configure use action mock |
||
| 50 | $action = $this->createMock(UseAction::class); |
||
| 51 | $action->method('getTextResponse')->willReturn('You use the key.'); |
||
| 52 | $action->method('getLocationResponse')->willReturn($response); |
||
| 53 | |||
| 54 | // Create and configure item mock |
||
| 55 | $item = $this->createMock(Item::class); |
||
| 56 | $item->method('hasAction')->with('use')->willReturn(true); |
||
| 57 | $item->method('getAction')->with('use')->willReturn($action); |
||
| 58 | $item->method('getName')->willReturn('key'); |
||
| 59 | |||
| 60 | // Configure inventory mock to find the item |
||
| 61 | $inventory = $this->createMock(Inventory::class); |
||
| 62 | $inventory->expects($this->once()) |
||
| 63 | ->method('getItem') |
||
| 64 | ->with('key') |
||
| 65 | ->willReturn($item); |
||
| 66 | |||
| 67 | $inventory->expects($this->exactly(2)) |
||
| 68 | ->method('hasItem') |
||
| 69 | ->with('key') |
||
| 70 | ->willReturn(true); |
||
| 71 | |||
| 72 | // Expect item to be removed after use |
||
| 73 | $inventory->expects($this->once()) |
||
| 74 | ->method('removeItem') |
||
| 75 | ->with($item); |
||
| 76 | |||
| 77 | // Perform action |
||
| 78 | $this->game->setInventory($inventory); |
||
| 79 | $this->game->setCurrentLocation($startingLocation); |
||
| 80 | $response = $this->game->processAction('use', 'key'); |
||
| 81 | |||
| 82 | // Assert use message is returned with the resuling locations details |
||
| 83 | $this->assertSame("You use the key.\n\nA change has occurred. Behold, the updated location awaits:\nA testy test location.\n", $response); |
||
| 84 | |||
| 85 | // Also, assert location is changed |
||
| 86 | $this->assertSame($responseLocation, $this->game->getCurrentLocation()); |
||
| 87 | } |
||
| 135 |