Completed
Push — master ( 265d55...500671 )
by Benedikt
04:27
created

AuthenticatedTestCase::getUserDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Created by PhpStorm.
6
 * User: benedikt
7
 * Date: 9/16/17
8
 * Time: 2:04 PM
9
 */
10
11
namespace Tfboe\FmLib\TestHelpers;
12
13
use Doctrine\DBAL\Connection;
14
use Illuminate\Support\Facades\Auth;
15
use LaravelDoctrine\ORM\Facades\EntityManager;
16
use Tfboe\FmLib\Entity\UserInterface;
17
18
/**
19
 * Class AuthenticatedTestCase
20
 * @package Tfboe\FmLib\TestHelpers
21
 */
22
abstract class AuthenticatedTestCase extends DatabaseTestCase
23
{
24
//<editor-fold desc="Fields">
25
  /**
26
   * Authentication token if logged in
27
   * @var string
28
   */
29
  protected $token;
30
31
  /**
32
   * User corresponding to authentication token if logged in
33
   * @var UserInterface
34
   */
35
  protected $user;
36
//</editor-fold desc="Fields">
37
38
//<editor-fold desc="Protected Methods">
39
  /**
40
   * sends a json request with an authentication token
41
   * @param string $method the method to use (GET, POST, ...)
42
   * @param string $uri the uri of the request
43
   * @param array $data the post data
44
   * @param array $headers the request headers
45
   * @return $this
46
   */
47
  protected function jsonAuth(string $method, string $uri, array $data = [], array $headers = [])
48
  {
49
    $headers['Authorization'] = 'Bearer ' . $this->token;
50
    return $this->json($method, $uri, $data, $headers);
51
  }
52
53
  protected function workOnDatabaseDestroy()
54
  {
55
    $this->clearUsers();
56
    parent::workOnDatabaseDestroy();
57
  }
58
59
  protected function getUserDb()
60
  {
61
    return "users";
62
  }
63
64
  protected function workOnDatabaseSetUp()
65
  {
66
    $this->clearUsers();
67
    parent::workOnDatabaseSetUp();
68
    $password = $this->newPassword();
69
    $this->user = entity($this->resolveEntity(UserInterface::class))->create(['originalPassword' => $password]);
70
    /** @noinspection PhpUnhandledExceptionInspection */
71
    $this->token = Auth::attempt(['email' => $this->user->getEmail(), 'password' => $password]);
0 ignored issues
show
Documentation Bug introduced by
The property $token was declared of type string, but \Illuminate\Support\Faca...assword' => $password)) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
72
    $this->refreshApplication();
73
    /** @noinspection PhpUndefinedMethodInspection */
74
    $this->user = EntityManager::find(UserInterface::class, $this->user->getId());
75
  }
76
//</editor-fold desc="Protected Methods">
77
78
//<editor-fold desc="Private Methods">
79
  private function clearUsers()
80
  {
81
    /** @var Connection $connection */
82
    /** @noinspection PhpUndefinedMethodInspection */
83
    $connection = EntityManager::getConnection();
84
    $sql = sprintf('SET FOREIGN_KEY_CHECKS=0;TRUNCATE TABLE %s;SET FOREIGN_KEY_CHECKS=1;', $this->getUserDb());
85
    /** @noinspection PhpUnhandledExceptionInspection */
86
    $connection->query($sql);
87
  }
88
//</editor-fold desc="Private Methods">
89
}