Passed
Push — master ( c60519...57a530 )
by Francis
01:29
created

RESTTest::testBasicAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
defined('BASEPATH') OR exit('No direct script access allowed');
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class RESTTest extends TestCase {
8
  /**
9
   * Code Igniter Instance.
10
   * @var object
11
   */
12
  private static $ci;
13
  /**
14
   * Package name for simplicity
15
   * @var string
16
   */
17
  private const PACKAGE = "francis94c/ci-rest";
18
  private $obj_count = 0;
19
20
  /**
21
   * Prerquisites for the Unit Tests.
22
   *
23
   * @covers JWT::__construct
24
   */
25
  public static function setUpBeforeClass(): void {
26
    echo "IN TEST";
27
    self::$ci =& get_instance();
0 ignored issues
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
    self::$ci =& /** @scrutinizer ignore-call */ get_instance();
Loading history...
28
    self::$ci->load->database('mysqli://root@localhost/test_db');
29
    self::$ci->load->helper("url");
30
    $queries = explode("#@@@", file_get_contents(FCPATH . 'application/splints/' . self::PACKAGE . '/phpunit/database.sql'));
0 ignored issues
show
Bug introduced by
The constant FCPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
    self::assertTrue(count($queries) > 0);
32
    self::$ci->load->database();
33
    foreach ($queries as $query) {
34
      self::$ci->db->query($query);
35
    }
36
    // Verify that URI can be modified.
37
    self::$ci->config->set_item("st_uri_string", "a/uri");
38
    self::assertEquals("a/uri", uri_string());
0 ignored issues
show
Bug introduced by
The function uri_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
    self::assertEquals("a/uri", /** @scrutinizer ignore-call */ uri_string());
Loading history...
39
    // Everything about this library, happens in it's connstructors.
40
    // Some static PHPUnit assertions will take place in the rest.php file
41
    // traditionally provided by the user and loaded from application/config by
42
    // default.
43
    // However, for the purpose of this test, we are going to Hack Code CodeIgniter
44
    // with a Splint Config variable to allow us load config files from where
45
    // ever we want. This happens below.
46
    self::$ci->load->add_package_path(APPPATH . 'splints/' . self::PACKAGE . "/phpunit/");
0 ignored issues
show
Bug introduced by
The constant APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
    //self::$ci->config->set_item('st_config_path_prefix', '../splints/' . self::PACKAGE . "/phpunit/config/");
48
  }
49
  /**
50
   * [testBasicAuthentication description]
51
   */
52
  public function testBasicAuth():void {
53
    // Simulate Request To 'basic/auth'
54
    self::$ci->config->set_item("st_uri_string", "basic/auth");
55
    // Simulate Basic Authorization
56
    $_SERVER['PHP_AUTH_USER'] = "francis94c";
57
    $_SERVER['PHP_AUTH_PW'] = "0123456789";
58
    self::$ci->load->splint(self::PACKAGE, '+REST', null, 'rest_' . $this->obj_count++);
59
    $this->assertEquals(1, self::$ci->{'rest_'.($this->obj_count - 1)}->userId);
60
  }
61
  /**
62
   * [tearDownAfterClass description]
63
   */
64
  public static function tearDownAfterClass(): void {
65
    self::$ci->db->empty_table("api_keys");
66
    self::$ci->db->empty_table("users");
67
    self::$ci->db->empty_table("rest_api_rate_limit");
68
    self::$ci->load->dbforge();
69
    self::$ci->dbforge->drop_table("api_keys");
70
    self::$ci->dbforge->drop_table("users");
71
    self::$ci->dbforge->drop_table("rest_api_rate_limit");
72
    self::$ci->db->close();
73
  }
74
}
75