• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12739985319

13 Jan 2025 03:15AM UTC coverage: 84.486%. Remained the same
12739985319

push

github

paulbalandan
Merge branch 'develop' into 4.6

351 of 417 new or added lines in 121 files covered. (84.17%)

1 existing line in 1 file now uncovered.

20798 of 24617 relevant lines covered (84.49%)

189.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.13
/system/Commands/Generators/TestGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Commands\Generators;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18
use CodeIgniter\CLI\GeneratorTrait;
19

20
/**
21
 * Generates a skeleton command file.
22
 */
23
class TestGenerator extends BaseCommand
24
{
25
    use GeneratorTrait;
26

27
    /**
28
     * The Command's Group
29
     *
30
     * @var string
31
     */
32
    protected $group = 'Generators';
33

34
    /**
35
     * The Command's Name
36
     *
37
     * @var string
38
     */
39
    protected $name = 'make:test';
40

41
    /**
42
     * The Command's Description
43
     *
44
     * @var string
45
     */
46
    protected $description = 'Generates a new test file.';
47

48
    /**
49
     * The Command's Usage
50
     *
51
     * @var string
52
     */
53
    protected $usage = 'make:test <name> [options]';
54

55
    /**
56
     * The Command's Arguments
57
     *
58
     * @var array<string, string>
59
     */
60
    protected $arguments = [
61
        'name' => 'The test class name.',
62
    ];
63

64
    /**
65
     * The Command's Options
66
     *
67
     * @var array<string, string>
68
     */
69
    protected $options = [
70
        '--namespace' => 'Set root namespace. Default: "Tests".',
71
        '--force'     => 'Force overwrite existing file.',
72
    ];
73

74
    /**
75
     * Actually execute a command.
76
     */
77
    public function run(array $params)
78
    {
79
        $this->component = 'Test';
1✔
80
        $this->template  = 'test.tpl.php';
1✔
81

82
        $this->classNameLang = 'CLI.generator.className.test';
1✔
83

84
        $autoload = service('autoloader');
1✔
85
        $autoload->addNamespace('CodeIgniter', TESTPATH . 'system');
1✔
86
        $autoload->addNamespace('Tests', ROOTPATH . 'tests');
1✔
87

88
        $this->generateClass($params);
1✔
89
    }
90

91
    /**
92
     * Gets the namespace from input or the default namespace.
93
     */
94
    protected function getNamespace(): string
95
    {
96
        if ($this->namespace !== null) {
1✔
97
            return $this->namespace;
×
98
        }
99

100
        if ($this->getOption('namespace') !== null) {
1✔
101
            return trim(
×
102
                str_replace(
×
103
                    '/',
×
104
                    '\\',
×
NEW
105
                    $this->getOption('namespace'),
×
106
                ),
×
NEW
107
                '\\',
×
108
            );
×
109
        }
110

111
        $class      = $this->normalizeInputClassName();
1✔
112
        $classPaths = explode('\\', $class);
1✔
113

114
        $namespaces = service('autoloader')->getNamespace();
1✔
115

116
        while ($classPaths !== []) {
1✔
117
            array_pop($classPaths);
1✔
118
            $namespace = implode('\\', $classPaths);
1✔
119

120
            foreach (array_keys($namespaces) as $prefix) {
1✔
121
                if ($prefix === $namespace) {
1✔
122
                    // The input classname is FQCN, and use the namespace.
123
                    return $namespace;
×
124
                }
125
            }
126
        }
127

128
        return 'Tests';
1✔
129
    }
130

131
    /**
132
     * Builds the test file path from the class name.
133
     *
134
     * @param string $class namespaced classname.
135
     */
136
    protected function buildPath(string $class): string
137
    {
138
        $namespace = $this->getNamespace();
1✔
139

140
        $base = $this->searchTestFilePath($namespace);
1✔
141

142
        if ($base === null) {
1✔
143
            CLI::error(
×
144
                lang('CLI.namespaceNotDefined', [$namespace]),
×
145
                'light_gray',
×
NEW
146
                'red',
×
147
            );
×
148
            CLI::newLine();
×
149

150
            return '';
×
151
        }
152

153
        $realpath = realpath($base);
1✔
154
        $base     = ($realpath !== false) ? $realpath : $base;
1✔
155

156
        $file = $base . DIRECTORY_SEPARATOR
1✔
157
            . str_replace(
1✔
158
                '\\',
1✔
159
                DIRECTORY_SEPARATOR,
1✔
160
                trim(str_replace($namespace . '\\', '', $class), '\\'),
1✔
161
            ) . '.php';
1✔
162

163
        return implode(
1✔
164
            DIRECTORY_SEPARATOR,
1✔
165
            array_slice(
1✔
166
                explode(DIRECTORY_SEPARATOR, $file),
1✔
167
                0,
1✔
168
                -1,
1✔
169
            ),
1✔
170
        ) . DIRECTORY_SEPARATOR . $this->basename($file);
1✔
171
    }
172

173
    /**
174
     * Returns test file path for the namespace.
175
     */
176
    private function searchTestFilePath(string $namespace): ?string
177
    {
178
        $bases = service('autoloader')->getNamespace($namespace);
1✔
179

180
        $base = null;
1✔
181

182
        foreach ($bases as $candidate) {
1✔
183
            if (str_contains($candidate, '/tests/')) {
1✔
184
                $base = $candidate;
1✔
185

186
                break;
1✔
187
            }
188
        }
189

190
        return $base;
1✔
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc