This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathphpunit.py
1284 lines (1005 loc) · 40.3 KB
/
phpunit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import datetime
import functools
import os
import re
import sublime
import sublime_plugin
import sys
class Prefs:
@staticmethod
def load():
settings = sublime.load_settings('PHPUnit.sublime-settings')
Prefs.folder_search_hints = settings.get('top_folder_hints', [])
Prefs.folder_exclusions = settings.get('folder_exclusions', [])
Prefs.max_search_secs = settings.get('max_search_secs', 2)
Prefs.phpunit_xml_aliases = settings.get('phpunit_xml_aliases', ["phpunit.xml", "phpunit.xml.dist"])
Prefs.phpunit_xml_location_hints = settings.get('phpunit_xml_location_hints', [])
Prefs.phpunit_additional_args = settings.get('phpunit_additional_args', {})
Prefs.debug = settings.get('debug', 0)
Prefs.path_to_phpunit = settings.get('path_to_phpunit', False)
Prefs.copy_env = settings.get('copy_env', True)
Prefs.override_env = settings.get('override_env', {})
Prefs.run_on_save = settings.get('run_on_save', False)
Prefs.context_menu = settings.get('context_menu', True)
# which version of ST are we working inside?
if sys.version_info[0] == 2:
Prefs.st2 = True
Prefs.st3 = False
else:
Prefs.st2 = False
Prefs.st3 = True
Prefs.load()
class Msgs:
operation = 'top-level'
@staticmethod
def debug_msg(msg):
if Prefs.debug == 1:
print("[PHPUnit Plugin " + Msgs.operation + "()] " + msg)
Msgs.debug_msg('')
Msgs.debug_msg('')
Msgs.debug_msg('')
Msgs.debug_msg('=========================================================')
Msgs.debug_msg('PHPUnit Plugin Reloaded')
Msgs.debug_msg('---------------------------------------------------------')
Msgs.debug_msg('')
Msgs.debug_msg('')
Msgs.debug_msg('')
class EraseViewCommand(sublime_plugin.TextCommand):
def run(self, edit, size=0):
self.view.erase(edit, sublime.Region(0, size))
class InsertViewCommand(sublime_plugin.TextCommand):
def run(self, edit, string=''):
self.view.insert(edit, self.view.size(), string)
class OutputView(object):
def __init__(self, name, window, edit=None):
self.output_name = name
self.window = window
self.edit = edit
self.is_running = False
def show_output(self):
self.ensure_output_view()
self.window.run_command("show_panel", {"panel": self.output_name})
def show_empty_output(self):
self.ensure_output_view()
self.clear_output_view()
self.show_output()
def ensure_output_view(self):
if not hasattr(self, 'output_view'):
self.output_view = self.window.get_output_panel(self.output_name)
panel_settings = self.output_view.settings()
panel_settings.set('syntax', 'Packages/PHPUnit/PHPUnit TextRunner.tmLanguage')
panel_settings.set('rulers', [])
panel_settings.set('gutter', False)
panel_settings.set('scroll_past_end', False)
panel_settings.set('draw_centered', False)
panel_settings.set('line_numbers', False)
panel_settings.set('spell_check', False)
panel_settings.set('word_wrap', True)
panel_settings.set('color_scheme', 'Packages/PHPUnit/color-schemes/phix-dark.hidden-tmTheme')
def clear_output_view(self):
self.ensure_output_view()
self.output_view.set_read_only(False)
self.output_view.run_command('erase_view')
self.output_view.set_read_only(True)
def append_data(self, data):
data = data.replace('\r\n', '\n').replace('\r', '\n')
data = re.sub('(.*)(\[2K|;\d+m)', '', data)
data = re.sub('\[(\d+)m', '', data)
self.output_view.set_read_only(False)
self.output_view.run_command('insert_view', {'string': data})
self.output_view.show(self.output_view.size())
self.output_view.set_read_only(True)
class CompatibilityOutputView:
def __init__(self, name, window, edit=None):
self.wrapped_view = OutputView(name, window, edit)
def show_output(self):
if Prefs.st2:
sublime.set_timeout(functools.partial(self.wrapped_view.show_output), 0)
else:
self.wrapped_view.show_output()
def show_empty_output(self):
if Prefs.st2:
sublime.set_timeout(functools.partial(self.wrapped_view.show_empty_output), 0)
else:
self.wrapped_view.show_empty_output()
def clear_output_view(self):
if Prefs.st2:
sublime.set_timeout(functools.partial(self.wrapped_view.clear_output_view), 0)
else:
self.wrapped_view.clear_output_view()
def append_data(self, data):
if Prefs.st2:
sublime.set_timeout(functools.partial(self.wrapped_view.append_data, data), 0)
else:
self.wrapped_view.append_data(data)
class CommandBase:
def __init__(self, window, edit):
self.window = window
self.edit = edit
def show_output(self):
if not hasattr(self, 'output_view'):
self.output_view = CompatibilityOutputView('exec', self.window, self.edit)
self.output_view.show_output()
def show_empty_output(self):
if not hasattr(self, 'output_view'):
self.output_view = CompatibilityOutputView('exec', self.window, self.edit)
self.output_view.clear_output_view()
self.output_view.show_output()
def start_async(self, caption, executable, cwd):
self.is_running = True
self.proc = AsyncProcess(executable, cwd, self)
# StatusProcess(caption, self)
def append_data(self, data):
self.output_view.append_data(data)
def update_status(self, msg, progress):
sublime.status_message(msg + " " + progress)
class PhpunitCommand(CommandBase):
def run(self, folder, configfile, testfile='', classname=''):
self.show_empty_output()
# if os.path.isdir(configfile):
# folder = configfile
# else:
# folder = os.path.dirname(configfile)
if Prefs.path_to_phpunit is not False:
args = [Prefs.path_to_phpunit]
elif os.path.isfile(folder + "/vendor/bin/phpunit"):
args = ["vendor/bin/phpunit"]
else:
# find where PHPUnit is installed
args = ["phpunit"]
# Add the additional arguments from the settings file to the command
for key, value in Prefs.phpunit_additional_args.items():
arg = key
if value != "":
arg += "=" + value
args.append(arg)
# remove the folder from the configfile
if configfile.startswith(folder):
configfile = configfile[len(folder):]
if configfile[0] == "/":
configfile = configfile[1:]
# remove the folder from the testfile
if testfile.startswith(folder):
testfile = testfile[len(folder):]
if testfile[0] == "/":
testfile = testfile[1:]
if os.path.isfile(os.path.join(folder, configfile)):
args.append("-c")
args.append(configfile)
# determine the unit test to run
if classname == '' and testfile != '':
classname = os.path.basename(testfile)
classname = os.path.splitext(classname)[0]
if classname != '':
args.append(classname)
if testfile != '':
args.append(testfile)
self.append_data("# Running in folder: " + folder + "\n")
self.append_data("# Configfile is: " + configfile + "\n")
self.append_data("$ " + ' '.join(args) + "\n")
self.window.run_command('exec', {
'cmd': ' '.join(args),
'working_dir': folder,
'file_regex': '([a-zA-Z0-9\\.\\/_-]+)(?: on line |\:)([0-9]+)$',
'shell': True,
'quiet': False,
})
class FoundFiles:
cache = {}
@staticmethod
def addToCache(top_folder, filename, result):
if top_folder not in FoundFiles.cache:
FoundFiles.cache[top_folder] = {}
Msgs.debug_msg('Adding ' + result + ' to cache for ' + top_folder)
FoundFiles.cache[top_folder][filename] = result
@staticmethod
def removeFromCache(top_folder, filename):
Msgs.debug_msg('Removing ' + filename + ' from cache for ' + top_folder)
if top_folder not in FoundFiles.cache:
Msgs.debug_msg('-- no cache for ' + top_folder)
return
if filename not in FoundFiles.cache[top_folder]:
Msgs.debug_msg('-- ' + filename + ' not found in cache')
return
del FoundFiles.cache[top_folder][filename]
Msgs.debug_msg('-- ' + filename + ' removed from cache')
@staticmethod
def removeCacheFor(top_folder):
Msgs.debug_msg('Removing cache for ' + top_folder)
if top_folder not in FoundFiles.cache:
Msgs.debug_msg('-- no cache for ' + top_folder)
return
del FoundFiles.cache[top_folder]
Msgs.debug_msg('-- removed cache')
@staticmethod
def removeCache():
Msgs.debug_msg('Completely emptying the cache')
FoundFiles.cache = {}
@staticmethod
def getFromCache(top_folder, filename):
Msgs.debug_msg('Get ' + filename + ' from cache for ' + top_folder)
if top_folder not in FoundFiles.cache:
Msgs.debug_msg('-- no cache for ' + top_folder)
return None
if filename not in FoundFiles.cache[top_folder]:
Msgs.debug_msg('-- ' + filename + ' not found in cache')
return None
Msgs.debug_msg('-- found ' + FoundFiles.cache[top_folder][filename])
return FoundFiles.cache[top_folder][filename]
class FindFiles:
searched_folders = {}
searched_for = {}
last_search_time = None
@staticmethod
def find(top_folder, search_from, files_to_find):
for file_to_find in files_to_find:
Msgs.debug_msg("Looking for " + file_to_find)
# check the cache - do we already know the answer?
result = FindFiles.searchCacheFor(top_folder, file_to_find)
if result is not None:
return result
# check the top folder
result = FindFiles.searchTopFolderFor(top_folder, file_to_find)
if result is not None:
# cache the result
FoundFiles.addToCache(top_folder, file_to_find, result)
return result
# check in the places given in hints
result = FindFiles.searchNamedPlacesFor(top_folder, Prefs.phpunit_xml_location_hints, file_to_find)
if result is not None:
# cache the result
FoundFiles.addToCache(top_folder, file_to_find, result)
return result
# if we reach this point, we are going to have to search on disk
dir_name = search_from
if not os.path.isdir(dir_name):
dir_name = os.path.dirname(dir_name)
# straight-line search - fastest for most people
result = FindFiles.searchStraightUpwardsFor(top_folder, dir_name, file_to_find)
if result is not None:
# cache the result
FoundFiles.addToCache(top_folder, file_to_find, result)
return result
# okay, so where is it?
result = ProjectFiles.find(top_folder, file_to_find)
if result is not None:
FoundFiles.addToCache(top_folder, file_to_find, result)
return result
# if we get here, we cannot find the file
return None
@staticmethod
def searchCacheFor(top_folder, file_to_find):
return FoundFiles.getFromCache(top_folder, file_to_find)
@staticmethod
def searchNamedPlacesFor(top_folder, places, file_to_find):
for place in places:
pathToTest = os.path.join(top_folder, place)
filenameToTest = os.path.join(pathToTest, file_to_find)
Msgs.debug_msg('Searching for file ' + filenameToTest)
if os.path.exists(filenameToTest):
return filenameToTest
return None
@staticmethod
def searchTopFolderFor(top_folder, file_to_find):
Msgs.debug_msg('Searching top folder ' + top_folder + ' for ' + file_to_find)
return FindFiles.searchFolderFor(top_folder, file_to_find)
@staticmethod
def searchFolderFor(folder, file_to_find):
Msgs.debug_msg('-- Searching ' + folder + ' for ' + file_to_find)
filenameToTest = os.path.join(folder, file_to_find)
if os.path.exists(filenameToTest):
Msgs.debug_msg('---- Found ' + filenameToTest)
return filenameToTest
return None
@staticmethod
def reachedTopLevelFolders(oldpath, path):
if oldpath == path:
return True
hints = Prefs.folder_search_hints
for hint in hints:
if os.path.exists(os.path.join(path, hint)):
return True
return False
@staticmethod
def reachedTopLevelFolder(folder, oldpath, path):
if oldpath == path:
return True
if folder[:len(path)] == path:
return True
return False
@staticmethod
def searchStraightUpwardsFor(top_folder, path, file_to_find):
Msgs.debug_msg("---- New search straight upwards ----")
# do we know where these files are?
return FindFiles._searchStraightUpwardsFor(top_folder, '', path, file_to_find)
@staticmethod
def _searchStraightUpwardsFor(top_folder, oldpath, path, file_to_find):
Msgs.debug_msg("Looking in " + path)
filenameToTest = os.path.join(path, file_to_find)
Msgs.debug_msg("Looking for " + filenameToTest)
if os.path.exists(filenameToTest):
return filenameToTest
if FindFiles.reachedTopLevelFolder(top_folder, oldpath, path):
return None
return FindFiles._searchStraightUpwardsFor(top_folder, path, os.path.dirname(path), file_to_find)
class ProjectFiles:
files = {}
last_built_time = None
@staticmethod
def buildFilesList(path):
Msgs.debug_msg('Building list of files under ' + path)
# does the path exist?
if not os.path.exists(path):
return None
# is the path root folder?
if path == os.path.dirname(path):
return None
ProjectFiles.files[path] = []
# how long will this take? let's find out
start = datetime.datetime.now()
# we're going to build up a cache of the files inside this project
i = 0
for root, dirs, files in os.walk(path):
# skip over sub-folders that we do not want to visit
for dirname in dirs:
if dirname in Prefs.folder_exclusions:
dirs.remove(dirname)
# add the files that we have
for name in files:
ProjectFiles.files[path].append(os.path.join(root, name))
# Msgs.debug_msg(" -- found file " + os.path.join(root, name))
i = i + 1
end = datetime.datetime.now()
duration = (end - start)
Msgs.debug_msg('-- took ' + str(duration.seconds) + '.' + str(duration.microseconds) + ' second(s) to build')
Msgs.debug_msg('-- found ' + str(i) + ' file(s)')
# print ProjectFiles.files[path]
ProjectFiles.last_built_time = end
@staticmethod
def find(top_folder, filename):
Msgs.debug_msg('Searching ProjectFiles cache for ' + filename)
if top_folder not in ProjectFiles.files:
Msgs.debug_msg('-- no cache for ' + top_folder)
return None
result = [x for x in ProjectFiles.files[top_folder] if x.endswith(filename)]
if len(result) == 0:
Msgs.debug_msg('-- none found')
return None
Msgs.debug_msg('-- found ' + result[0])
return result[0]
@staticmethod
def expired(when):
if when is None:
return True
if when < ProjectFiles.last_built_time:
return True
return False
class ActiveFile:
def is_test_buffer(self):
Msgs.debug_msg('Is buffer a file containing tests?')
filename = self.file_name()
if not os.path.isfile(filename):
Msgs.debug_msg("-- Buffer is not a real file; unsaved new buffer?")
return False
filename = os.path.splitext(filename)[0]
if filename.endswith('Test'):
Msgs.debug_msg("-- Buffer is a test file")
return True
Msgs.debug_msg("-- Buffer is not a test file")
return False
def is_tests_buffer(self):
Msgs.debug_msg('Is buffer a file containing a testsuite?')
filename = self.file_name()
if not os.path.isfile(filename):
Msgs.debug_msg("-- Buffer is not a real file; unsaved new buffer?")
return False
filename = os.path.splitext(filename)[0]
if filename.endswith('Tests'):
Msgs.debug_msg("Buffer is a testsuite file")
return True
Msgs.debug_msg("Buffer is not a testsuite file")
return False
def is_phpunitxml(self):
# is this a phpunit.xml file?
filename = self.file_name()
if not os.path.isfile(filename):
Msgs.debug_msg("Buffer is not phpunit.xml; is not a real file")
return False
filename = os.path.basename(filename)
if filename in Prefs.phpunit_xml_aliases:
Msgs.debug_msg("Buffer is a phpunit.xml file")
return True
Msgs.debug_msg("Buffer is not a phpunit.xml file")
return False
def determineClassToTest(self):
class_to_test = os.path.splitext(os.path.basename(self.file_name()))[0]
if not class_to_test.endswith('Test'):
class_to_test = class_to_test + "Test"
return class_to_test
def determineTestFile(self):
filename = os.path.splitext(os.path.basename(self.file_name()))[0]
if filename.endswith('Test'):
return self.file_name()
return None
def findPhpunitXml(self, search_from):
Msgs.debug_msg("Looking for phpunit.xml of some kind")
Msgs.debug_msg("Project's top folder is: " + self.top_folder())
# what are we looking for?
files_to_find = Prefs.phpunit_xml_aliases
return FindFiles.find(self.top_folder(), search_from, files_to_find)
def error_message(self, message):
sublime.status_message(message)
def cannot_find_xml(self):
return "Cannot find phpunit.xml or phpunit.xml.dist file"
def cannot_find_test_file(self):
return "Cannot find file containing unit tests"
def cannot_find_tested_file(self):
return "Cannot find file to be tested"
def not_in_project(self):
return "Only works if you have a ST2 project open"
def not_php_file(self, syntax):
Msgs.debug_msg(syntax)
matches = re.search("/([^/]+).tmLanguage", syntax)
if matches is not None:
syntax = matches.group(1)
return "Plugin does not support " + syntax + " syntax buffers"
class ActiveView(ActiveFile):
def is_php_buffer(self):
# most reliable way is to check the file extension
# we cannot rely on the buffer syntax; it can sometimes report
# 'HTML' even in a PHP buffer
ext = os.path.splitext(self.file_name())[1]
if ext == '.php':
Msgs.debug_msg("Buffer is a PHP buffer")
return True
# is this a PHP buffer?
if re.search('.+\PHP.tmLanguage', self.view.settings().get('syntax')):
return True
# if we get here, we're not sure what else to try
Msgs.debug_msg("Buffer is not a PHP buffer; extension is: " + ext + "; syntax is: " + self.view.settings().get('syntax'))
return False
def has_project_open(self):
folders = self.view.window().folders()
if folders:
return True
return False
def file_name(self):
return self.view.file_name()
def top_folder(self):
# have we done this before?
if hasattr(self, 'top_folder_path'):
return self.top_folder_path
folders = self.view.window().folders()
path = os.path.dirname(self.file_name())
oldpath = ''
while not path in folders and path != oldpath and not self.top_level_folder_hints(path):
oldpath = path
path = os.path.dirname(path)
if path == oldpath:
# problem - we didn't find ourselves in the list of open folders
# fallback to using heuristics instead
path = os.path.dirname(self.file_name())
while not FindFiles.reachedTopLevelFolders(oldpath, path):
oldpath = path
path = os.path.dirname(path)
Msgs.debug_msg("Top folder for this project is: " + path)
self.top_folder_path = path
return path
def top_level_folder_hints(self, folder):
hints = Prefs.folder_search_hints
for hint in hints:
if os.path.exists(os.path.join(folder, hint)):
return True
return False
def find_tested_file(self):
Msgs.debug_msg("Looking for tested file")
fq_classname = self.determine_full_class_name()
if fq_classname is None:
return None
if fq_classname[-4:] == 'Test':
fq_classname = fq_classname[:-4]
filename = fq_classname + '.php'
Msgs.debug_msg("Looking for tested file: " + os.path.basename(filename))
files_to_find = []
files_to_find.append(filename)
files_to_find.append(os.path.basename(filename))
filename = self.view.file_name()
if filename[-8:] == 'Test.php':
filename = filename[:-8] + '.php'
path_to_search = os.path.dirname(self.file_name())
path = FindFiles.find(self.top_folder(), path_to_search, files_to_find)
if path is None:
return None
return [path, fq_classname]
def find_test_file(self):
Msgs.debug_msg("Looking for test file")
classname = self.determine_full_class_name()
if classname is None:
Msgs.debug_msg("I don't know what the classname is :(")
return None
Msgs.debug_msg("classname is: " + classname)
classname = classname + 'Test'
filename = classname + '.php'
files_to_find = []
files_to_find.append(filename)
files_to_find.append(os.path.basename(filename))
files_to_find.append(os.path.basename(self.view.file_name())[:-4] + 'Test.php')
Msgs.debug_msg("Looking for test files: " + ', '.join(files_to_find))
path_to_search = os.path.dirname(self.file_name())
path = FindFiles.find(self.top_folder(), path_to_search, files_to_find)
if path is None:
return None
return [path, classname]
def determine_full_class_name(self):
namespace = self.extract_namespace()
classname = self.extract_classname()
if classname is None:
return None
path = ''
if len(namespace) > 0:
namespace = namespace.replace('\\', '/')
path = path + namespace + '/'
classname = classname.replace('_', '/')
path = path + classname
return path
def extract_namespace(self):
namespaces = self.view.find_all("namespace ([A-Za-z0-9_\\\]+);")
if namespaces is None or len(namespaces) == 0:
return ''
for namespace in namespaces:
line = self.view.substr(namespace)
return line[10:-1]
def extract_classname(self):
# Look for any classes in the current window
class_regions = self.view.find_by_selector('entity.name.type.class')
for r in class_regions:
# return the first class we find
return self.view.substr(r)
# If we get here, then there are no classes in the current window
return None
class ActiveWindow(ActiveFile):
def file_name(self):
if hasattr(self, '_file_name'):
return self._file_name
return None
def determine_filename(self, args=[]):
if len(args) == 0:
active_view = self.window.active_view()
filename = active_view.file_name()
else:
filename = args[0]
self._file_name = filename
def is_php_buffer(self):
ext = os.path.splitext(self.file_name())[1]
if ext == '.php':
return True
return False
class PhpunitTextBase(sublime_plugin.TextCommand, ActiveView):
last_checked_enabled = None
def run(self, args):
print('Not implemented')
def toggle_active_group(self):
# where will we open it?
num_groups = self.view.window().num_groups()
if num_groups > 1:
active_group = self.view.window().active_group()
active_group = (active_group + 1) % 2
if active_group >= num_groups:
active_group = num_groups - 1
Msgs.debug_msg("switching to group " + str(active_group))
self.view.window().focus_group(active_group)
def enabled_checked(self):
self.last_checked_enabled = datetime.datetime.now()
def needs_enabling(self):
if self.last_checked_enabled is None or ProjectFiles.expired(self.last_checked_enabled):
return True
return False
class PhpunitRunTestsCommand(PhpunitTextBase):
path_to_config = None
file_to_test = None
def run(self, edit):
Msgs.operation = "PhpunitRunTestsClassCommand.run"
cmd = PhpunitCommand(self.view.window(), edit)
cmd.run(self.top_folder(), self.path_to_config, self.file_to_test)
return None
def description(self):
Msgs.operation = "PhpunitRunTestsClassCommand.description"
if self.file_to_test is None:
return self.cannot_find_test_file()
if self.path_to_config is None:
return self.cannot_find_xml()
return 'Run Tests ...'
def is_enabled(self):
Msgs.operation = "PhpunitRunTestsClassCommand.is_enabled"
Msgs.debug_msg('called')
self.enabled_checked()
self.file_to_test = None
self.path_to_config = None
if not self.has_project_open():
return False
if not self.is_php_buffer():
return False
if self.is_test_buffer() or self.is_tests_buffer():
test_file_to_open = [self.view.file_name()]
else:
test_file_to_open = self.find_test_file()
if test_file_to_open is None:
return False
self.file_to_test = test_file_to_open[0]
self.path_to_config = self.findPhpunitXml(self.file_to_test)
if self.path_to_config is None:
return False
return True
def is_visible(self):
# has the user switched off context-menu support?
if not Prefs.context_menu:
return False
if self.needs_enabling():
self.is_enabled()
Msgs.operation = "PhpunitRunTestsClassCommand.is_visible"
Msgs.debug_msg('called')
if self.is_php_buffer() and os.path.exists(self.view.file_name()):
return True
return False
class PhpunitOpenTestClassCommand(PhpunitTextBase):
file_to_open = None
def run(self, edit):
self.edit = edit
Msgs.operation = "PhpunitOpenTestClassCommand.run"
# where will we open the file?
self.toggle_active_group()
# open the file
self.view.window().open_file(self.file_to_open)
def description(self):
return 'Open Test Class'
def is_enabled(self):
Msgs.operation = "PhpunitOpenTestClassCommand.is_enabled"
Msgs.debug_msg('called')
self.enabled_checked()
self.file_to_open = None
if not self.has_project_open():
return False
if not self.is_php_buffer():
return False
if self.is_test_buffer() or self.is_tests_buffer():
return False
path = self.find_test_file()
if path is None:
return False
self.file_to_open = path[0]
return True
def is_visible(self):
# has the user switched off context-menu support?
if not Prefs.context_menu:
return False
if self.needs_enabling():
self.is_enabled()
Msgs.operation = "PhpunitOpenTestClassCommand.is_visible"
Msgs.debug_msg('called')
if self.file_to_open is not None:
return True
return False
class PhpunitOpenClassBeingTestedCommand(PhpunitTextBase):
file_to_open = None
def run(self, edit):
self.edit = edit
Msgs.operation = "PhpunitOpenClassBeingTestedCommand.run"
# where will we open the file?
self.toggle_active_group()
# open the file
self.view.window().open_file(self.file_to_open)
def description(self):
return 'Open Class Being Tested'
def is_enabled(self):
Msgs.operation = "PhpunitOpenClassBeingTestedCommand.is_enabled"
Msgs.debug_msg('called')
self.enabled_checked()
self.file_to_open = None
if not self.has_project_open():
return False
if not self.is_php_buffer():
return False
if not self.is_test_buffer():
return False
if self.is_tests_buffer():
return False
path = self.find_tested_file()
if path is None:
return False
self.file_to_open = path[0]
return True
def is_visible(self):
# has the user switched off context-menu support?
if not Prefs.context_menu:
return False
if self.needs_enabling():
self.is_enabled()
Msgs.operation = "PhpunitOpenClassBeingTestedCommand.is_visible"
Msgs.debug_msg('called')
if self.file_to_open is not None:
return True
return False
class PhpunitToggleClassTestClassCommand(PhpunitTextBase):
file_to_open = None
def run(self, edit):
self.edit = edit
Msgs.operation = "PhpunitToggleClassTestClassCommand.run"
# where will we open the file?
self.toggle_active_group()
# open the file
self.view.window().open_file(self.file_to_open)
def is_enabled(self):
Msgs.operation = "PhpunitToggleClassTestClassCommand.is_enabled"
Msgs.debug_msg('called')
self.enabled_checked()
self.file_to_open = None
if not self.has_project_open():
return False
if not self.is_php_buffer():
return False
if self.is_test_buffer() or self.is_tests_buffer():
file_to_open = self.find_tested_file()
else:
file_to_open = self.find_test_file()
if file_to_open is None:
return False
self.file_to_open = file_to_open[0]
return True
def is_visible(self):
# has the user switched off context-menu support?
if not Prefs.context_menu:
return False
if self.needs_enabling():
self.is_enabled()
Msgs.operation = "PhpunitToggleClassTestClassCommand.is_visible"
Msgs.debug_msg('called')
if self.file_to_open is not None:
return True
return False
def description(self):
return 'Toggle Between Code And Test File'
class PhpunitOpenPhpunitXmlCommand(PhpunitTextBase):
file_to_open = None
def run(self, edit):
self.edit = edit
Msgs.operation = "PhpunitOpenPhpunitXmlCommand.run"
# where will we open the file?
self.toggle_active_group()
# open the file
self.view.window().open_file(self.file_to_open)
def description(self):
return 'Open phpunit.xml'
def is_visible(self):
# has the user switched off context-menu support?
if not Prefs.context_menu:
return False
if self.needs_enabling():
self.is_enabled()
Msgs.operation = "PhpunitOpenPhpunitXmlCommand.is_visible"
Msgs.debug_msg('called')
if self.file_to_open is not None:
return True
return False
def is_enabled(self):
Msgs.operation = "PhpunitOpenPhpunitXmlCommand.is_enabled"
Msgs.debug_msg('called')
self.enabled_checked()
self.file_to_open = None