Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit f7366c5

Browse files
committed
Rewrite WPAnalyticsTests in Swift
1 parent 80727b3 commit f7366c5

File tree

6 files changed

+178
-148
lines changed

6 files changed

+178
-148
lines changed

WordPressShared.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
2EB9C57E3BFEDA1676FE857C /* Pods_WordPressShared.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AE15A6EE80D7766A21B83BF5 /* Pods_WordPressShared.framework */; };
1414
32E1BFD524A63DE6007A08F0 /* WPStyleGuide+SerifFonts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32E1BFD424A63DE6007A08F0 /* WPStyleGuide+SerifFonts.swift */; };
1515
3F338B76289BE0F90014ADC5 /* BuildkiteTestCollector in Frameworks */ = {isa = PBXBuildFile; productRef = 3F338B75289BE0F90014ADC5 /* BuildkiteTestCollector */; };
16+
4A2B7D132919E294007E5917 /* WPAnalyticsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A2B7D122919E294007E5917 /* WPAnalyticsTests.swift */; };
1617
740B23CC1F17F1FF00067A2A /* DisplayableImageHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 740B23CA1F17F1FF00067A2A /* DisplayableImageHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
1718
740B23CD1F17F1FF00067A2A /* DisplayableImageHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 740B23CB1F17F1FF00067A2A /* DisplayableImageHelper.m */; };
1819
740B23CF1F17F28E00067A2A /* DisplayableImageHelperTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 740B23CE1F17F28E00067A2A /* DisplayableImageHelperTest.m */; };
@@ -87,7 +88,6 @@
8788
E157E126239527700051AE41 /* Secret.swift in Sources */ = {isa = PBXBuildFile; fileRef = E157E125239527700051AE41 /* Secret.swift */; };
8889
E157E128239527AD0051AE41 /* SecretTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E157E127239527AD0051AE41 /* SecretTests.swift */; };
8990
E18EABEA1F0E2C6800BFCB0B /* TestAnalyticsTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = E18EABE81F0E2C6800BFCB0B /* TestAnalyticsTracker.m */; };
90-
E18EABEB1F0E2C6800BFCB0B /* WPAnalyticsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E18EABE91F0E2C6800BFCB0B /* WPAnalyticsTests.m */; };
9191
E1A444281F063CAB00F6AA8A /* WPAnalytics.h in Headers */ = {isa = PBXBuildFile; fileRef = E1A444261F063CAB00F6AA8A /* WPAnalytics.h */; settings = {ATTRIBUTES = (Public, ); }; };
9292
E1A444291F063CAB00F6AA8A /* WPAnalytics.m in Sources */ = {isa = PBXBuildFile; fileRef = E1A444271F063CAB00F6AA8A /* WPAnalytics.m */; };
9393
F106FA61226FA72E00706DE4 /* StringURLValidationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F19847DD226F92EA0004A8BC /* StringURLValidationTests.swift */; };
@@ -122,6 +122,7 @@
122122
32E1BFD424A63DE6007A08F0 /* WPStyleGuide+SerifFonts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WPStyleGuide+SerifFonts.swift"; sourceTree = "<group>"; };
123123
405129D7901B7D4AD3B46442 /* Pods-WordPressShared.release-internal.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShared.release-internal.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressShared/Pods-WordPressShared.release-internal.xcconfig"; sourceTree = "<group>"; };
124124
47629DF6D2C813279CBF93C4 /* Pods_WordPressSharedTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WordPressSharedTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
125+
4A2B7D122919E294007E5917 /* WPAnalyticsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WPAnalyticsTests.swift; sourceTree = "<group>"; };
125126
5AC1280B7CDCD44A2F3A20AC /* Pods-WordPressShared.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShared.release-alpha.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressShared/Pods-WordPressShared.release-alpha.xcconfig"; sourceTree = "<group>"; };
126127
5B7FC757233F785AA7F785E7 /* Pods-WordPressShared.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShared.release.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressShared/Pods-WordPressShared.release.xcconfig"; sourceTree = "<group>"; };
127128
716C92B0CD72E5CD8250C04E /* Pods-WordPressSharedTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressSharedTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressSharedTests/Pods-WordPressSharedTests.debug.xcconfig"; sourceTree = "<group>"; };
@@ -211,7 +212,6 @@
211212
E157E127239527AD0051AE41 /* SecretTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretTests.swift; sourceTree = "<group>"; };
212213
E18EABE71F0E2C6800BFCB0B /* TestAnalyticsTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestAnalyticsTracker.h; sourceTree = "<group>"; };
213214
E18EABE81F0E2C6800BFCB0B /* TestAnalyticsTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestAnalyticsTracker.m; sourceTree = "<group>"; };
214-
E18EABE91F0E2C6800BFCB0B /* WPAnalyticsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPAnalyticsTests.m; sourceTree = "<group>"; };
215215
E1A444261F063CAB00F6AA8A /* WPAnalytics.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WPAnalytics.h; sourceTree = "<group>"; };
216216
E1A444271F063CAB00F6AA8A /* WPAnalytics.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WPAnalytics.m; sourceTree = "<group>"; };
217217
E5A4545AEF641B125A2ABA89 /* Pods-WordPressShared.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressShared.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WordPressShared/Pods-WordPressShared.debug.xcconfig"; sourceTree = "<group>"; };
@@ -378,7 +378,7 @@
378378
F1C83C1123E20F7000A8CCF1 /* StringStripGutenbergContentForExcerptTests.swift */,
379379
E18EABE71F0E2C6800BFCB0B /* TestAnalyticsTracker.h */,
380380
E18EABE81F0E2C6800BFCB0B /* TestAnalyticsTracker.m */,
381-
E18EABE91F0E2C6800BFCB0B /* WPAnalyticsTests.m */,
381+
4A2B7D122919E294007E5917 /* WPAnalyticsTests.swift */,
382382
827070911ECA4E1D00155CBF /* WPImageSourceTest.m */,
383383
93A73ABE1EE9DDB000C0F2F9 /* WPMapFilterReduceTest.m */,
384384
8270708F1ECA4E1C00155CBF /* WordPressSharedTests-Bridging-Header.h */,
@@ -763,9 +763,9 @@
763763
E157E128239527AD0051AE41 /* SecretTests.swift in Sources */,
764764
740B23CF1F17F28E00067A2A /* DisplayableImageHelperTest.m in Sources */,
765765
F1C83C0C23E205D900A8CCF1 /* NSStringSummaryTests.swift in Sources */,
766-
E18EABEB1F0E2C6800BFCB0B /* WPAnalyticsTests.m in Sources */,
767766
7430C9DD1F1934190051B8E6 /* RichContentFormatterTests.swift in Sources */,
768767
B5393FDC206D6169007BF9D4 /* EmailFormatValidatorTests.swift in Sources */,
768+
4A2B7D132919E294007E5917 /* WPAnalyticsTests.swift in Sources */,
769769
748710AC1F06C465008095AB /* StringHelperTests.swift in Sources */,
770770
9A1329DF22170BE2009EE02A /* NSDateHelperTest.swift in Sources */,
771771
);

WordPressSharedTests/TestAnalyticsTracker.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#import <Foundation/Foundation.h>
22
#import "WPAnalytics.h"
33

4+
NS_ASSUME_NONNULL_BEGIN
5+
46
@interface TestAnalyticsTracker : NSObject<WPAnalyticsTracker>
57

68
- (void)track:(WPAnalyticsStat)stat;
@@ -11,3 +13,11 @@
1113
- (void)refreshMetadata;
1214

1315
@end
16+
17+
typedef void (^WPAnalyticsMethodBehaviorInvocation)(id<WPAnalyticsTracker>);
18+
19+
FOUNDATION_EXTERN void WPAnalyticsTestVerifyUnregistered(WPAnalyticsMethodBehaviorInvocation invocation);
20+
FOUNDATION_EXTERN void WPAnalyticsTestVerifyRegistered(WPAnalyticsMethodBehaviorInvocation invocation);
21+
FOUNDATION_EXTERN void WPAnalyticsTestVerifyMultipleTrackers(WPAnalyticsMethodBehaviorInvocation invocation);
22+
23+
NS_ASSUME_NONNULL_END

WordPressSharedTests/TestAnalyticsTracker.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#import "TestAnalyticsTracker.h"
22

3+
#import <OCMock/OCMock.h>
4+
35
@implementation TestAnalyticsTracker
46

57
- (void)track:(WPAnalyticsStat)stat
@@ -38,3 +40,31 @@ - (void)trackString:(NSString *)event withProperties:(NSDictionary *)properties
3840

3941

4042
@end
43+
44+
void WPAnalyticsTestVerifyUnregistered(WPAnalyticsMethodBehaviorInvocation invocation) {
45+
id trackerMock = OCMStrictClassMock([TestAnalyticsTracker class]);
46+
id expectation = [trackerMock reject];
47+
invocation(expectation);
48+
[trackerMock verify];
49+
}
50+
51+
void WPAnalyticsTestVerifyRegistered(WPAnalyticsMethodBehaviorInvocation invocation) {
52+
id trackerMock = OCMStrictClassMock([TestAnalyticsTracker class]);
53+
id expectation = [trackerMock expect];
54+
invocation(expectation);
55+
invocation(trackerMock);
56+
[trackerMock verify];
57+
}
58+
59+
void WPAnalyticsTestVerifyMultipleTrackers(WPAnalyticsMethodBehaviorInvocation invocation) {
60+
id trackerMock = OCMStrictClassMock([TestAnalyticsTracker class]);
61+
id trackerMock2 = OCMStrictClassMock([TestAnalyticsTracker class]);
62+
id expectation = [trackerMock expect];
63+
id expectation2 = [trackerMock2 expect];
64+
invocation(expectation);
65+
invocation(expectation2);
66+
invocation(trackerMock);
67+
invocation(trackerMock2);
68+
[trackerMock verify];
69+
[trackerMock2 verify];
70+
}

WordPressSharedTests/WPAnalyticsTests.m

Lines changed: 0 additions & 144 deletions
This file was deleted.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import Quick
2+
3+
@testable import WordPressShared
4+
5+
class WPAnalyticsMethodBehaviorExamplesConfiguration: QuickConfiguration {
6+
override class func configure(_ configuration: QCKConfiguration!) {
7+
sharedExamples("a WPAnalyticsTracker method") { context in
8+
guard let context else {
9+
XCTFail("Test case needs to provide a context")
10+
return
11+
}
12+
guard let invocation = context()?["invocation"] else {
13+
XCTFail("invocation not found in the provided context")
14+
return
15+
}
16+
guard let theInvocation = invocation as? WPAnalyticsMethodBehaviorInvocation else {
17+
XCTFail("invocation not found in the provided context")
18+
return
19+
}
20+
21+
it("should not be called if tqracker isn't registered") {
22+
WPAnalyticsTestVerifyUnregistered(theInvocation)
23+
}
24+
25+
it("should be called if tracker is registered") {
26+
WPAnalyticsTestVerifyRegistered(theInvocation)
27+
}
28+
29+
it("should be called on multiple trackers if registered") {
30+
WPAnalyticsTestVerifyMultipleTrackers(theInvocation)
31+
}
32+
}
33+
}
34+
}
35+
36+
class WPAnalyticsTests: QuickSpec {
37+
override func spec() {
38+
beforeEach {
39+
WPAnalytics.clearTrackers()
40+
}
41+
42+
describe("beginSession") {
43+
itBehavesLike("a WPAnalyticsTracker method") {
44+
[
45+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
46+
tracker.beginSession!()
47+
}
48+
]
49+
}
50+
}
51+
52+
describe("endSession") {
53+
itBehavesLike("a WPAnalyticsTracker method") {
54+
[
55+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
56+
tracker.endSession!()
57+
}
58+
]
59+
}
60+
}
61+
62+
describe("refreshMetadata") {
63+
itBehavesLike("a WPAnalyticsTracker method") {
64+
[
65+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
66+
tracker.refreshMetadata!()
67+
}
68+
]
69+
}
70+
}
71+
72+
describe("beginTimerForStat:") {
73+
itBehavesLike("a WPAnalyticsTracker method") {
74+
[
75+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
76+
tracker.beginTimer!(for: .applicationOpened)
77+
}
78+
]
79+
}
80+
}
81+
82+
describe("endTimerForStat:withProperties") {
83+
itBehavesLike("a WPAnalyticsTracker method") {
84+
[
85+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
86+
tracker.endTimer!(for: .applicationOpened, withProperties: [:])
87+
}
88+
]
89+
}
90+
}
91+
92+
describe("track:") {
93+
itBehavesLike("a WPAnalyticsTracker method") {
94+
[
95+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
96+
tracker.track(.applicationOpened)
97+
}
98+
]
99+
}
100+
}
101+
102+
describe("track:withProperties:") {
103+
itBehavesLike("a WPAnalyticsTracker method") {
104+
[
105+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
106+
tracker.track(.applicationOpened, withProperties: [:])
107+
}
108+
]
109+
110+
}
111+
}
112+
113+
describe("trackString:") {
114+
itBehavesLike("a WPAnalyticsTracker method") {
115+
[
116+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
117+
tracker.trackString("my_event")
118+
}
119+
]
120+
}
121+
}
122+
123+
describe("trackString:withProperties:") {
124+
itBehavesLike("a WPAnalyticsTracker method") {
125+
[
126+
"invocation": { (tracker: WPAnalyticsTracker) -> Void in
127+
tracker.trackString("my_event", withProperties: [:])
128+
}
129+
]
130+
}
131+
}
132+
}
133+
}

WordPressSharedTests/WordPressSharedTests-Bridging-Header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
// Use this file to import your target's public headers that you would like to expose to Swift.
33
//
44

5+
#import "TestAnalyticsTracker.h"

0 commit comments

Comments
 (0)