Skip to content

Commit d75ca64

Browse files
committed
issue-X add phpunit
1 parent 911fe50 commit d75ca64

7 files changed

Lines changed: 139 additions & 9 deletions

File tree

.travis.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
dist: trusty
1+
language: php
22

3-
services:
4-
- docker
3+
php:
4+
- '7.1'
5+
- '7.2'
56

67
install:
7-
- cp .env.example .env
8-
- eval "$(ssh-agent -s)"
9-
- docker-compose up -d
8+
- composer install
109

1110
script:
12-
- curl localhost:8080
11+
- vendor/bin/phpunit --bootstrap vendor/autoload.php tests
12+
13+
jobs:
14+
include:
15+
- stage: build
16+
services:
17+
- docker
18+
install:
19+
- cp .env.example .env
20+
- eval "$(ssh-agent -s)"
21+
- docker-compose up -d
22+
script:
23+
- curl localhost:8080

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
"description": "This app is an example application for using docker as a dev environment",
44
"type": "project",
55
"require-dev": {
6-
"phpro/grumphp": "^0.13.1",
76
"friendsofphp/php-cs-fixer": "^2.10",
8-
"jakub-onderka/php-parallel-lint": "^0.9.2"
7+
"jakub-onderka/php-parallel-lint": "^0.9.2",
8+
"phpro/grumphp": "^0.13.1",
9+
"phpunit/phpunit": "^7.0"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Matters\\": "src/"
14+
}
915
},
1016
"license": "MIT",
1117
"authors": [

phpunit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false">
11+
</phpunit>

src/Armor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Matters;
4+
5+
class Armor
6+
{
7+
private $defense;
8+
9+
public function __construct($defense)
10+
{
11+
$this->defense = $defense;
12+
}
13+
14+
public function getDefense()
15+
{
16+
return $this->defense;
17+
}
18+
}

src/Character.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Matters;
4+
5+
class Character
6+
{
7+
private $life;
8+
private $force;
9+
private $armor;
10+
private $weapon;
11+
12+
public function __construct($life, $force)
13+
{
14+
$this->life = $life;
15+
$this->force = $force;
16+
$this->weapon = new Weapon(0);
17+
$this->armor = new Armor(0);
18+
}
19+
20+
public function attack(Character $opponent)
21+
{
22+
$opponent->receiveDamage($this->weapon->getDamages() + $this->force);
23+
}
24+
25+
public function receiveDamage($damages)
26+
{
27+
$this->life = $damages - ($this->armor->getDefense() + $this->force);
28+
}
29+
30+
public function equip(Weapon $weapon)
31+
{
32+
$this->weapon = $weapon;
33+
}
34+
35+
public function putsOn(Armor $armor)
36+
{
37+
$this->armor = $armor;
38+
}
39+
40+
public function isAlive()
41+
{
42+
return $this->life > 0;
43+
}
44+
};

src/Weapon.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Matters;
4+
5+
class Weapon
6+
{
7+
private $damages;
8+
9+
public function __construct($damages)
10+
{
11+
$this->damages = $damages;
12+
}
13+
14+
public function getDamages()
15+
{
16+
return $this->damages;
17+
}
18+
}

tests/CharacterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Matters;
4+
5+
use \PHPUnit\Framework\TestCase;
6+
7+
class CharacterTest extends TestCase
8+
{
9+
public function testAttack() {
10+
$hero = new Character(30, 5);
11+
$vilain = new Character(25, 8);
12+
13+
$theBiggestSwordOfAllTime = new Weapon(50);
14+
$hero->equip($theBiggestSwordOfAllTime);
15+
16+
$simpleHoodie = new Armor(1);
17+
$vilain->putsOn($simpleHoodie);
18+
19+
$hero->attack($vilain);
20+
self::assertFalse($vilain->isAlive());
21+
}
22+
}

0 commit comments

Comments
 (0)