Completed
Push — master ( d96a7a...d56a4b )
by Mahmoud
04:03
created

TrackCloseTest::testTrackClose()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Tracker\UI\API\Tests\Functional;
4
5
use App\Containers\Tracker\Models\TimeTracker;
6
use App\Containers\User\Models\User;
7
use App\Port\Tests\PHPUnit\Abstracts\TestCase;
8
use Carbon\Carbon;
9
10
/**
11
 * Class TrackCloseTest
12
 *
13
 * @author  Mahmoud Zalt  <[email protected]>
14
 */
15
class TrackCloseTest extends TestCase
16
{
17
18
    private $endpoint = '/track/close';
19
20
    public function testTrackClose()
21
    {
22
        $visitorId = str_random('40');
23
24
        $visitor = new User();
25
        $visitor->visitor_id = $visitorId;
0 ignored issues
show
Documentation introduced by
The property visitor_id does not exist on object<App\Containers\User\Models\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
        $visitor->save();
27
28
        $headers['visitor-id'] = $visitorId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
29
30
        $timeTracker = new TimeTracker();
31
        $timeTracker->open_at = Carbon::yesterday();
0 ignored issues
show
Documentation introduced by
The property open_at does not exist on object<App\Containers\Tracker\Models\TimeTracker>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
        $timeTracker->status = TimeTracker::PENDING;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\Containers\Tracker\Models\TimeTracker>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
33
        $timeTracker->user()->associate($visitor);
34
        $timeTracker->save();
35
36
        // send the HTTP request
37
        $response = $this->apiCall($this->endpoint, 'post', [], true, $headers);
38
39
        // assert response status is correct
40
        $this->assertEquals($response->getStatusCode(), '202');
41
42
        $responseObject = $this->getResponseObject($response);
43
44
        $this->assertEquals($responseObject->message, 'Session (close) Tracked Successfully.');
45
46
        $this->seeInDatabase('time_tracker', ['status' => TimeTracker::SUCCEEDED]);
47
48
    }
49
50
}
51