Completed
Push — master ( fb8886...624979 )
by Matthew
9s
created

PickValidatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
namespace PhpDraft\Test;
3
4
use PHPUnit\Framework\TestCase;
5
use PhpDraft\Domain\Entities\Draft;
6
use PhpDraft\Domain\Entities\Pick;
7
use PhpDraft\Domain\Validators\PickValidator;
8
use PhpDraft\Domain\Repositories\DraftDataRepository;
9
use PhpDraft\Domain\Models\PhpDraftResponse;
10
11
class PickValidatorTest extends TestCase {
12
  function setUp() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    $this->app = require dirname(__FILE__).'/../../../../api/config/_app.php';
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
    $draftDataRepository = new DraftDataRepository($this->app);
15
    $this->app['phpdraft.DraftDataRepository'] = $draftDataRepository;
16
    $this->sut = new PickValidator($this->app);
0 ignored issues
show
Bug introduced by
The property sut does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Documentation introduced by
$this->app is of type array<string,object<PhpD...DraftDataRepository>"}>, but the function expects a object<Silex\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17
18
    $this->draft = new Draft();
0 ignored issues
show
Bug introduced by
The property draft does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
    $this->draft->draft_sport = "NFL";
20
21
    $this->pick = new Pick();
0 ignored issues
show
Bug introduced by
The property pick does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
    $this->pick->first_name = "Antonio";
23
    $this->pick->last_name = "Brown";
24
    $this->pick->team = "PIT";
25
    $this->pick->position = "WR";
26
27
    //TODO: How to not re-create this simple wheel?
28
    $this->app['phpdraft.ResponseFactory'] = function() {
29
      return function($success, $errors) {
30
        return new PhpDraftResponse($success, $errors);
31
      };
32
    };
33
  }
34
35
  public function testEnsuresFieldsArentEmptyOnAdd() {
36
    $this->pick->first_name = "";
37
38
    $result = $this->sut->IsPickValidForAdd($this->draft, $this->pick);
39
40
    $this->assertFalse($result->success);
41
    $this->assertContains("One or more missing fields.", $result->errors);
42
43
    $this->pick->first_name = "Antonio";
44
    $this->pick->last_name = "";
45
46
    $result = $this->sut->IsPickValidForAdd($this->draft, $this->pick);
47
48
    $this->assertFalse($result->success);
49
    $this->assertContains("One or more missing fields.", $result->errors);
50
51
    $this->pick->last_name = "Brown";
52
    $this->pick->team = "";
53
54
    $result = $this->sut->IsPickValidForAdd($this->draft, $this->pick);
55
56
    $this->assertFalse($result->success);
57
    $this->assertContains("One or more missing fields.", $result->errors);
58
59
    $this->pick->team = "PIT";
60
    $this->pick->position = "";
61
62
    $result = $this->sut->IsPickValidForAdd($this->draft, $this->pick);
63
64
    $this->assertFalse($result->success);
65
    $this->assertContains("One or more missing fields.", $result->errors);
66
  }
67
68
  public function testDraftIdMatchesOnAdd() {
69
    $this->draft->draft_id = 1;
70
    $this->pick->draft_id = 2;
71
72
    $result = $this->sut->IsPickValidForAdd($this->draft, $this->pick);
73
74
    $this->assertFalse($result->success);
75
    $this->assertContains("Pick does not belong to draft #1.", $result->errors);
76
  }
77
}