Passed
Pull Request — master (#16)
by Eric
12:51
created

CounterTest::testWithDefaultNewVisitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
nc 1
nop 0
dl 0
loc 19
c 1
b 0
f 0
cc 1
rs 9.8333

1 Method

Rating   Name   Duplication   Size   Complexity  
A CounterTest::testFetchCurrentIpList() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\SimpleCounter.
7
 *
8
 * (c) Eric Sizemore <https://github.com/ericsizemore>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\SimpleCounter\Tests;
16
17
use Esi\SimpleCounter\Configuration\FlatfileConfiguration;
18
use Esi\SimpleCounter\Counter;
19
use Esi\SimpleCounter\Storage\FlatfileStorage;
1 ignored issue
show
Bug introduced by
The type Esi\SimpleCounter\Storage\FlatfileStorage 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...
20
use Esi\Utility\Arrays;
21
use Esi\Utility\Environment;
22
use Esi\Utility\Filesystem;
23
use PHPUnit\Framework\Attributes\CoversClass;
24
use PHPUnit\Framework\Attributes\TestDox;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\TestDox 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...
25
use PHPUnit\Framework\Attributes\UsesClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\UsesClass 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...
26
use PHPUnit\Framework\TestCase;
27
28
use function sprintf;
29
30
use const DIRECTORY_SEPARATOR;
31
32
/**
33
 * @internal
34
 * @psalm-suppress NullReference
35
 */
36
#[CoversClass(Counter::class)]
37
#[UsesClass(FlatfileStorage::class)]
38
#[UsesClass(FlatfileConfiguration::class)]
39
class CounterTest extends TestCase
40
{
41
    private ?Counter $counter;
42
43
    /**
44
     * @var string[]
45
     */
46
    private static array $logFiles;
47
48
    /**
49
     * @var string[]
50
     */
51
    private static array $testDirectories;
52
53
    #[\Override]
54
    protected function setUp(): void
55
    {
56
        Arrays::set($_SERVER, 'REMOTE_ADDR', '127.0.0.1');
57
58
        self::$testDirectories = [
59
            'logDir'   => sprintf('%s%s%s', \dirname(__FILE__, 2), DIRECTORY_SEPARATOR, 'logs'),
60
            'imageDir' => sprintf('%s%s%s', \dirname(__FILE__, 2), DIRECTORY_SEPARATOR, 'images'),
61
        ];
62
63
        self::$logFiles = [
64
            'countFile' => sprintf('%s%s%s', self::$testDirectories['logDir'], DIRECTORY_SEPARATOR, 'counter.json'),
65
            'ipFile'    => sprintf('%s%s%s', self::$testDirectories['logDir'], DIRECTORY_SEPARATOR, 'ips.json'),
66
        ];
67
68
        $this->counter = new Counter(new FlatfileStorage(FlatfileConfiguration::initOptions(
69
            [
70
                'logDir'   => self::$testDirectories['logDir'],
71
                'imageDir' => self::$testDirectories['imageDir'],
72
            ]
73
        )));
74
    }
75
76
    #[\Override]
77
    protected function tearDown(): void
78
    {
79
        $this->counter = null;
80
81
        Arrays::set($_SERVER, 'REMOTE_ADDR', Environment::var('REMOTE_ADDR', null));
82
83
        Filesystem::fileWrite(self::$logFiles['countFile'], '{"currentCount":"0"}');
84
        Filesystem::fileWrite(self::$logFiles['ipFile'], '{"ipList":[""]}');
85
    }
86
87
    #[TestDox('fetchCurrentCount is able to return the current count data accurately.')]
88
    public function testFetchCurrentCount(): void
89
    {
90
        self::assertInstanceOf(Counter::class, $this->counter);
91
92
        $currentCount = $this->counter->fetchCurrentCount();
1 ignored issue
show
Bug introduced by
The method fetchCurrentCount() does not exist on null. ( Ignorable by Annotation )

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

92
        /** @scrutinizer ignore-call */ 
93
        $currentCount = $this->counter->fetchCurrentCount();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        self::assertSame(0, $currentCount);
94
95
        $this->counter->display();
96
        $newCount = $this->counter->fetchCurrentCount();
97
        self::assertSame(1, $newCount);
98
    }
99
100
    #[TestDox('fetchCurrentIpList is able to return the current ip data accurately.')]
101
    public function testFetchCurrentIpList(): void
102
    {
103
        self::assertInstanceOf(Counter::class, $this->counter);
104
        self::assertEmpty($this->counter->fetchCurrentIpList());
105
        $this->counter->display();
106
        self::assertSame(['127.0.0.1'], $this->counter->fetchCurrentIpList());
107
    }
108
109
    #[TestDox('getOption is able to return the value of a given option.')]
110
    public function testGetOption(): void
111
    {
112
        self::assertInstanceOf(Counter::class, $this->counter);
113
        self::assertSame(self::$testDirectories['logDir'], $this->counter->getOption('logDir'));
114
    }
115
116
    #[TestDox('Was able to instantiate Counter with default options and retrieve count information.')]
117
    public function testWithDefaultOptions(): void
118
    {
119
        self::assertInstanceOf(Counter::class, $this->counter);
120
121
        $count    = $this->counter->display();
122
        $countTwo = $this->counter->display();
123
124
        self::assertNotEmpty($count);
125
        self::assertNotEmpty($countTwo);
126
        self::assertMatchesRegularExpression('/([A-Za-z]+( [A-Za-z]+)+)\s#[0-9]+/i', $count);
127
        self::assertMatchesRegularExpression('/([A-Za-z]+( [A-Za-z]+)+)\s#[0-9]+/i', $countTwo);
128
        self::assertSame($count, $countTwo);
129
    }
130
}
131