Test Failed
Push — main ( 99efcd...df6632 )
by Alex
14:53
created

HandleExamineTest::testProcessActionExamineItemOnlyInInventory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.552
1
<?php
2
3
namespace App\Tests\Adventure;
4
5
use App\Adventure\Game;
6
use App\Adventure\Location;
7
use App\Adventure\Inventory;
8
use App\Adventure\Item;
9
use App\Adventure\InputActions\ExamineAction;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * Unit tests for Game class. Specifically testing 'examine' action.
14
 */
15
class HandleExamineTest extends TestCase
16
{
17
    /**
18
     * @var Game
19
     */
20
    private $game;
21
22
    protected function setUp(): void
23
    {
24
        $this->game = new Game();
25
    }
26
27
    /**
28
     * Test case for procesAction with 'examine' with item in location.
29
     */
30
    public function testProcessActionExamineItemOnlyInLocation(): void
31
    {
32
        // Create and configure examine action mock
33
        $action = $this->createMock(ExamineAction::class);
34
        $action->method('getTextResponse')->willReturn('You examine the key.');
35
36
        // Create and configure item mock
37
        $item = $this->createMock(Item::class);
38
        $item->method('hasAction')->with('examine')->willReturn(true);
39
        $item->method('getAction')->with('examine')->willReturn($action);
40
41
        // Configure location mock to find the item
42
        $location = $this->createMock(Location::class);
43
        $location->expects($this->once())
44
            ->method('hasItem')
45
            ->with('key')
46
            ->willReturn(true);
47
48
        $location->expects($this->once())
49
            ->method('getItem')
50
            ->with('key')
51
            ->willReturn($item);
52
53
        // If the item is found in the location, it will not be looked for in the inventory
54
        $inventory = $this->createMock(Inventory::class);
55
        $inventory->expects($this->never())
56
            ->method('hasItem')
57
            ->with('key');
58
59
        // Perform action
60
        $this->game->setInventory($inventory);
61
        $this->game->setCurrentLocation($location);
62
        $response = $this->game->processAction('examine', 'key');
63
64
        // Assert examine message is returned
65
        $this->assertSame('You examine the key.', $response);
66
    }
67
68
    /**
69
     * Test case for procesAction with 'examine' with item only in inventory.
70
     */
71
    public function testProcessActionExamineItemOnlyInInventory(): void
72
    {
73
        // Create and configure examine action mock
74
        $action = $this->createMock(ExamineAction::class);
75
        $action->method('getTextResponse')->willReturn('You examine the key.');
76
77
        // Create and configure item mock
78
        $item = $this->createMock(Item::class);
79
        $item->method('hasAction')->with('examine')->willReturn(true);
80
        $item->method('getAction')->with('examine')->willReturn($action);
81
82
        // Configure location mock to NOT find the item
83
        $location = $this->createMock(Location::class);
84
        $location->expects($this->once())
85
            ->method('hasItem')
86
            ->with('key')
87
            ->willReturn(false);
88
89
        // Configure inventory mock to find the item
90
        $inventory = $this->createMock(Inventory::class);
91
        $inventory->expects($this->once())
92
            ->method('hasItem')
93
            ->with('key')
94
            ->willReturn(true);
95
96
        $inventory->expects($this->once())
97
            ->method('getItem')
98
            ->with('key')
99
            ->willReturn($item);
100
101
        // Perform action
102
        $this->game->setCurrentLocation($location);
103
        $this->game->setInventory($inventory);
104
        $response = $this->game->processAction('examine', 'key');
105
106
        // Assert examine message is returned
107
        $this->assertSame('You examine the key.', $response);
108
    }
109
110
    /**
111
     * Test case for procesAction with 'examine' with no/empty text response.
112
     */
113
    public function testProcessActionExamineItemDefaultResponse(): void
114
    {
115
        // Create and configure examine action mock
116
        $action = $this->createMock(ExamineAction::class);
117
        $action->method('getTextResponse')->willReturn(''); // Empty response
118
119
        // Create and configure item mock
120
        $item = $this->createMock(Item::class);
121
        $item->method('hasAction')->with('examine')->willReturn(true);
122
        $item->method('getAction')->with('examine')->willReturn($action);
123
124
        // Configure location mock to find the item
125
        $location = $this->createMock(Location::class);
126
        $location->expects($this->once())
127
            ->method('hasItem')
128
            ->with('key')
129
            ->willReturn(true);
130
131
        $location->expects($this->once())
132
            ->method('getItem')
133
            ->with('key')
134
            ->willReturn($item);
135
136
        // Get a different name just to make it obvious that the method is being called
137
        $item->expects($this->once())
138
            ->method('getName')
139
            ->willReturn('pizza');
140
141
        $inventory = $this->createStub(Inventory::class);
142
143
        // Perform action
144
        $this->game->setInventory($inventory);
145
        $this->game->setCurrentLocation($location);
146
        $response = $this->game->processAction('examine', 'key');
147
148
        // Assert default examine message is returned
149
        $this->assertSame('Upon examining the pizza, you find nothing noteworthy. It appears to be just an ordinary pizza.', $response);
150
    }
151
}
152