Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # License: MIT
- # Run like: "python phpunit4_killer.py /var/lib/mediawiki/extensions/Wikibase/"
- import os
- import sys
- import re
- import subprocess
- def find_files(path):
- files = []
- for r, d, f in os.walk(path):
- for file_name in f:
- if file_name.endswith('.php'):
- files.append(os.path.join(r, file_name))
- return files
- def check_file(path):
- with open(path, 'r', encoding="utf-8") as f:
- content = f.read()
- old_content = content
- cases = re.findall(r'\n\t*?use \\?PHPUnit4And6Compat;', content)
- literal = '\'(?:[^\'\\\\]|\\\\\')+\'|"(?:[^"\\\\]|\\\\")+"'
- clname = '(?:[a-zA-Z\\\\]+::class|\$\w+|' + literal + ')'
- content = re.sub(r'(\$this->)setExpected(Exception\(\s+' + clname + '\s+\);)', r'\1expect\2', content)
- content = re.sub(r'^(\\t*)(\$this->)setExpectedException\(\s+(' + clname + '),\s+(' + clname + ')\s+\);', r'\1\2expectException( \3 );\n\1\$this->expectExceptionMessage( \4 );', content)
- content = re.sub(r'(\$this->)get(Mock\(\s+' + clname + '\s+\))', r'\1create\2', content)
- content = re.sub(r'(\$this->)getMock\(\s+(' + clname + '),\s+\[\],\s+\[\],\s+[\'"]{2},\s+false\s+\)', r'\1createMock( \2 )', content)
- content = re.sub(r'^(\\t*)(\$this->)getMock\(\s+(' + clname + '),\s+(\[(?:\s*' + literal + ',?)+\])\s+\);', r'\1\2createMock( \3 )\n\1\t->setMethods( \4 )\n\1\t->getMock();', content)
- if '$this->getMock(' in content or '$this->setExpectedException(' in content:
- print('Nooooo /o\\')
- with open(path, 'w', encoding="utf-8") as f:
- print(path)
- f.write(content)
- return
- for case in cases:
- content = content.replace(case, '')
- content = content.replace(', PHPUnit4And6Compat, ', ', ')
- content = content.replace('use PHPUnit4And6Compat, ', 'use ')
- content = content.replace(', PHPUnit4And6Compat;', ';')
- if content == old_content:
- return
- with open(path, 'w', encoding="utf-8") as f:
- print(path)
- f.write(content)
- for f in find_files(sys.argv[1]):
- check_file(f)
- subprocess.run(["composer", "update"], cwd=sys.argv[1], shell=True)
- subprocess.run(["composer", "fix"], cwd=sys.argv[1], shell=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement