@@ -5,11 +5,54 @@ class YMLAPI < ::HBW::Common::YMLAPI
55
66 class << self
77 def build ( path )
8- new ( YAML . load_file ( path , aliases : true ) , [ ] )
8+ from_file ( path )
99 end
1010
1111 def for_prosess_keys ( path , process_keys )
12- new ( YAML . load_file ( path , aliases : true ) , process_keys )
12+ from_file ( path , process_keys )
13+ end
14+
15+ def with_global ( path , global )
16+ responses = merge_responses ( load_file ( global ) , load_file ( path ) )
17+ log_stub ( "#{ path } + global #{ global } " )
18+ new ( responses , [ ] )
19+ end
20+
21+ private
22+
23+ def from_file ( path , process_keys = [ ] )
24+ log_stub ( path + ( process_keys . empty? ? '' : " #{ process_keys . inspect } " ) )
25+ new ( load_file ( path ) , process_keys )
26+ end
27+
28+ def load_file ( path )
29+ YAML . load_file ( path , aliases : true )
30+ end
31+
32+ def log_stub ( desc )
33+ Rails . logger . info ( "[camunda-mock] stub <- #{ desc } " )
34+ end
35+
36+ # Combine two {method => {url => [entry]}} mocks. `overlay` entries come before `base` so
37+ # fetch_response's find-by-params returns them first: the test mock wins and the global mock
38+ # only fills endpoints or params the test mock does not define.
39+ def merge_responses ( base , overlay )
40+ # Not Array(): Array(Hash) splits it into [key, value] pairs instead of wrapping it.
41+ entries = lambda do |mock , method , url |
42+ case ( found = mock . dig ( method , url ) )
43+ when nil then [ ]
44+ when Array then found
45+ else [ found ]
46+ end
47+ end
48+
49+ ( base . keys | overlay . keys ) . to_h do |method |
50+ urls = ( base [ method ] || { } ) . keys | ( overlay [ method ] || { } ) . keys
51+ merged = urls . to_h do |url |
52+ [ url , entries . call ( overlay , method , url ) + entries . call ( base , method , url ) ]
53+ end
54+ [ method , merged ]
55+ end
1356 end
1457 end
1558
0 commit comments