John Chandler avatar John Chandler committed 46c62d1

Added tests and code to handle multiple includes/requires

Comments (0)

Files changed (2)

 """
 import re
 
-INCLUDE_RE = re.compile( '(?P<type>include|require)(?:_once)? "(?P<filename>[^"]+)"' )
+INCLUDE_RE = re.compile( '(include|require)(?:_once)? "([^"]+)"' )
 
 class PHPFileParser( object ):
     """ PHP File Parser """
     def parse( self, file_text ):
         """ parse the file_text and return a list of include/require dependencies """
-        match = INCLUDE_RE.search( file_text )
-        if match:
-            return [ (match.group("type"), match.group("filename") ) ]
+        matches = INCLUDE_RE.findall( file_text )
         
-        return []
+        dependencies = []
+        if matches:
+            for dependency_found in matches:
+                dependencies.append( dependency_found )
+     
+        return dependencies
     

tests/test_parser.py

         """ Test for presence of one require_once statement """
         self.assertEquals( self.parser.parse('require_once "foo.php"'), [ ("require", "foo.php")] )
 
+    def test_parse_multiple_includes( self ):
+        """ Test for presence of multiple include statements """
+        php_code = '''
+        include "foo.php"
+        include "bar.php"
+        include "baz.php"
+        '''
+        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("include", "bar.php"), ("include", "baz.php") ] )
+
+    def test_parse_multiple_requires( self ):
+        """ Test for presence of multiple require statements """
+        php_code = '''
+        require "foo.php"
+        require "bar.php"
+        require "baz.php"
+        '''
+        self.assertEquals( self.parser.parse(php_code), [ ("require", "foo.php"), ("require", "bar.php"), ("require", "baz.php") ] )
+
+    def test_parse_multiple_include_onces( self ):
+        """ Test for presence of multiple include_once statements """
+        php_code = '''
+        include_once "foo.php"
+        include_once "bar.php"
+        include_once "baz.php"
+        '''
+        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("include", "bar.php"), ("include", "baz.php") ] )
+
+    def test_parse_multiple_require_onces( self ):
+        """ Test for presence of multiple require_once statements """
+        php_code = '''
+        require_once "foo.php"
+        require_once "bar.php"
+        require_once "baz.php"
+        '''
+        self.assertEquals( self.parser.parse(php_code), [ ("require", "foo.php"), ("require", "bar.php"), ("require", "baz.php") ] )
+
+    def test_mixed_includes_and_requires( self ):
+        """ Test for presence of multiple includes and requires """
+        php_code = '''
+        include_once "foo.php"
+        require "bar.php"
+        include "baz.php"
+        require_once "qux.php"
+        '''
+        self.assertEquals( self.parser.parse(php_code), [ ("include", "foo.php"), ("require", "bar.php"), ("include", "baz.php"), ("require", "qux.php") ] )
+        
+
+        
 
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.