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

kenjis / ci-phpunit-test / 1025

pending completion
1025

push

travis-ci-com

web-flow
Merge pull request #394 from dezsi-istvan/3.x

pre_controller can modify $class / $method

4 of 4 new or added lines in 1 file covered. (100.0%)

1321 of 1739 relevant lines covered (75.96%)

35.36 hits per line

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

79.63
/application/tests/_ci_phpunit_test/CIPHPUnitTestAutoloader.php
1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10

11
class CIPHPUnitTestAutoloader
12
{
13
        private $alias = [
14
                'MonkeyPatch',
15
                'ReflectionHelper',
16
        ];
17

18
        /**
19
         * @var directories to search file
20
         */
21
        private $dirs = [];
22

23
        /**
24
         * @var CIPHPUnitTestFileCache
25
         */
26
        private $cache;
27

28
        /**
29
         * @param CIPHPUnitTestFileCache $cache
30
         * @param array $dirs directories to search file
31
         */
32
        public function __construct(
33
                CIPHPUnitTestFileCache $cache = null,
34
                array $dirs = null
35
        )
36
        {
37
                $this->cache = $cache;
×
38
                if ($dirs === null)
×
39
                {
40
                        $this->dirs = [
×
41
                                APPPATH.'models',
×
42
                                APPPATH.'libraries',
×
43
                                APPPATH.'controllers',
×
44
                                APPPATH.'modules',
×
45
                                APPPATH.'hooks',
×
46
                        ];
×
47
                }
48
                else
49
                {
50
                        $this->dirs = $dirs;
×
51
                }
52
        }
53

54
        public function load($class)
55
        {
56
                if ($this->cache)
24✔
57
                {
58
                        if ($this->loadFromCache($class))
24✔
59
                        {
60
                                return;
1✔
61
                        }
62
                }
63

64
                $this->loadCIPHPUnitTestAliasClass($class);
23✔
65
                $this->loadCIPHPUnitTestClass($class);
23✔
66
                $this->loadApplicationClass($class);
23✔
67
        }
68

69
        protected function loadCIPHPUnitTestAliasClass($class)
70
        {
71
                if (in_array($class, $this->alias))
23✔
72
                {
73
                        $dir = __DIR__ . '/alias';
2✔
74
                        $this->loadClassFile($dir, $class);
2✔
75
                }
76
        }
77

78
        protected function loadCIPHPUnitTestClass($class)
79
        {
80
                if (substr($class, 0, 13) !== 'CIPHPUnitTest')
23✔
81
                {
82
                        return;
16✔
83
                }
84

85
                if (substr($class, -9) !== 'Exception')
10✔
86
                {
87
                        $dir = __DIR__;
6✔
88
                        $this->loadClassFile($dir, $class);
6✔
89
                }
90
                else
91
                {
92
                        $dir = __DIR__ . '/exceptions';
4✔
93
                        $this->loadClassFile($dir, $class);
4✔
94
                }
95
        }
96

97
        protected function loadApplicationClass($class)
98
        {
99
                foreach ($this->dirs as $dir)
23✔
100
                {
101
                        if ( ! is_dir($dir))
23✔
102
                        {
103
                                continue;
13✔
104
                        }
105

106
                        if ($this->loadClassFile($dir, $class))
23✔
107
                        {
108
                                return true;
9✔
109
                        }
110

111
                        $iterator = new \RecursiveIteratorIterator(
23✔
112
                                new \RecursiveDirectoryIterator(
23✔
113
                                        $dir, \RecursiveDirectoryIterator::SKIP_DOTS
23✔
114
                                ),
23✔
115
                                \RecursiveIteratorIterator::SELF_FIRST
23✔
116
                        );
23✔
117

118
                        foreach ($iterator as $file)
23✔
119
                        {
120
                                if ($file->isDir())
23✔
121
                                {
122
                                        if ($this->loadClassFile($file, $class))
23✔
123
                                        {
124
                                                return true;
2✔
125
                                        }
126
                                }
127
                        }
128
                }
129
        }
130

131
        protected function loadClassFile($dir, $class)
132
        {
133
                $class_file = $dir . '/' . $class . '.php';
23✔
134
                if (file_exists($class_file))
23✔
135
                {
136
                        require $class_file;
22✔
137
                        if ($this->cache)
22✔
138
                        {
139
                                $this->cache[$class] = $class_file;
22✔
140
                        }
141
                        return true;
22✔
142
                }
143
                
144
                return false;
23✔
145
        }
146

147
        protected function loadFromCache($class)
148
        {
149
                if ($filename = $this->cache[$class])
24✔
150
                {
151
                        if (is_readable($filename))
1✔
152
                        {
153
                                require $filename;
1✔
154
                                return true;
1✔
155
                        }
156
                        else
157
                        {
158
                                unset($this->cache[$class]);
×
159
                        }
160
                }
161
                
162
                return false;
23✔
163
        }
164
}
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

© 2025 Coveralls, Inc