Skip to content

Commit 8f563cc

Browse files
authored
Merge pull request #7080 from woocommerce/issue/2551-add-date-from-string-helper-function
Add dateFromString helper function to TestKit
2 parents 46c4b75 + 40b9f09 commit 8f563cc

18 files changed

+163
-251
lines changed

Networking/NetworkingTests/Mapper/ProductVariationMapperTests.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ private extension ProductVariationMapperTests {
5151
productVariationID: id,
5252
attributes: sampleProductVariationAttributes(),
5353
image: ProductImage(imageID: 2432,
54-
dateCreated: dateFromGMT("2020-03-13T03:13:57"),
55-
dateModified: dateFromGMT("2020-07-21T08:29:16"),
54+
dateCreated: DateFormatter.dateFromString(with: "2020-03-13T03:13:57"),
55+
dateModified: DateFormatter.dateFromString(with: "2020-07-21T08:29:16"),
5656
src: imageSource,
5757
name: "DSC_0010",
5858
alt: ""),
5959
permalink: "https://chocolate.com/marble",
60-
dateCreated: dateFromGMT("2020-06-12T14:36:02"),
61-
dateModified: dateFromGMT("2020-07-21T08:35:47"),
60+
dateCreated: DateFormatter.dateFromString(with: "2020-06-12T14:36:02"),
61+
dateModified: DateFormatter.dateFromString(with: "2020-07-21T08:35:47"),
6262
dateOnSaleStart: nil,
6363
dateOnSaleEnd: nil,
6464
status: .published,
@@ -98,9 +98,4 @@ private extension ProductVariationMapperTests {
9898
ProductVariationAttribute(id: 0, name: "Shape", option: "marble")
9999
]
100100
}
101-
102-
func dateFromGMT(_ dateStringInGMT: String) -> Date {
103-
let dateFormatter = DateFormatter.Defaults.dateTimeFormatter
104-
return dateFormatter.date(from: dateStringInGMT)!
105-
}
106101
}

Networking/NetworkingTests/Remote/CouponsRemoteTests.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ private extension CouponsRemoteTests {
405405
couponID: 720,
406406
code: "free shipping",
407407
amount: "10.00",
408-
dateCreated: date(with: "2017-03-21T18:25:02"),
409-
dateModified: date(with: "2017-03-21T18:25:02"),
408+
dateCreated: DateFormatter.dateFromString(with: "2017-03-21T18:25:02"),
409+
dateModified: DateFormatter.dateFromString(with: "2017-03-21T18:25:02"),
410410
discountType: .fixedCart,
411411
description: "Coupon description",
412-
dateExpires: date(with: "2017-03-31T15:25:02"),
412+
dateExpires: DateFormatter.dateFromString(with: "2017-03-31T15:25:02"),
413413
usageCount: 10,
414414
individualUse: true, productIds: [12893712, 12389],
415415
excludedProductIds: [12213],
@@ -425,11 +425,4 @@ private extension CouponsRemoteTests {
425425
emailRestrictions: ["*@a8c.com", "[email protected]"],
426426
427427
}
428-
429-
func date(with dateString: String) -> Date {
430-
guard let date = DateFormatter.Defaults.dateTimeFormatter.date(from: dateString) else {
431-
return Date()
432-
}
433-
return date
434-
}
435428
}

Networking/NetworkingTests/Remote/ProductVariationsRemoteTests.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ final class ProductVariationsRemoteTests: XCTestCase {
4848
XCTAssertEqual(expectedVariation.sku, "99%-nuts-marble")
4949
XCTAssertEqual(expectedVariation.permalink, "https://chocolate.com/marble")
5050

51-
XCTAssertEqual(expectedVariation.dateCreated, self.dateFromGMT("2019-11-14T12:40:55"))
52-
XCTAssertEqual(expectedVariation.dateModified, self.dateFromGMT("2019-11-14T13:06:42"))
53-
XCTAssertEqual(expectedVariation.dateOnSaleStart, self.dateFromGMT("2019-10-15T21:30:00"))
54-
XCTAssertEqual(expectedVariation.dateOnSaleEnd, self.dateFromGMT("2019-10-27T21:29:59"))
51+
XCTAssertEqual(expectedVariation.dateCreated, DateFormatter.dateFromString(with: "2019-11-14T12:40:55"))
52+
XCTAssertEqual(expectedVariation.dateModified, DateFormatter.dateFromString(with: "2019-11-14T13:06:42"))
53+
XCTAssertEqual(expectedVariation.dateOnSaleStart, DateFormatter.dateFromString(with: "2019-10-15T21:30:00"))
54+
XCTAssertEqual(expectedVariation.dateOnSaleEnd, DateFormatter.dateFromString(with: "2019-10-27T21:29:59"))
5555

5656
let expectedPrice = 12
5757
XCTAssertEqual(expectedVariation.price, "\(expectedPrice)")
@@ -334,14 +334,14 @@ private extension ProductVariationsRemoteTests {
334334
productVariationID: id,
335335
attributes: sampleProductVariationAttributes(),
336336
image: ProductImage(imageID: 2432,
337-
dateCreated: dateFromGMT("2020-03-13T03:13:57"),
338-
dateModified: dateFromGMT("2020-07-21T08:29:16"),
337+
dateCreated: DateFormatter.dateFromString(with: "2020-03-13T03:13:57"),
338+
dateModified: DateFormatter.dateFromString(with: "2020-07-21T08:29:16"),
339339
src: imageSource,
340340
name: "DSC_0010",
341341
alt: ""),
342342
permalink: "https://chocolate.com/marble",
343-
dateCreated: dateFromGMT("2020-06-12T14:36:02"),
344-
dateModified: dateFromGMT("2020-07-21T08:35:47"),
343+
dateCreated: DateFormatter.dateFromString(with: "2020-06-12T14:36:02"),
344+
dateModified: DateFormatter.dateFromString(with: "2020-07-21T08:35:47"),
345345
dateOnSaleStart: nil,
346346
dateOnSaleEnd: nil,
347347
status: .published,
@@ -387,9 +387,4 @@ private extension ProductVariationsRemoteTests {
387387
let createVariation = CreateProductVariation(regularPrice: "5.0", attributes: sampleProductVariationAttributes())
388388
return createVariation
389389
}
390-
391-
func dateFromGMT(_ dateStringInGMT: String) -> Date {
392-
let dateFormatter = DateFormatter.Defaults.dateTimeFormatter
393-
return dateFormatter.date(from: dateStringInGMT)!
394-
}
395390
}

Networking/NetworkingTests/Remote/ProductsRemoteTests.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ final class ProductsRemoteTests: XCTestCase {
4848
name: "Product",
4949
slug: "product",
5050
permalink: "https://example.com/product/product/",
51-
date: date(with: "2020-09-03T02:52:44"),
52-
dateCreated: date(with: "2020-09-03T02:52:44"),
53-
dateModified: date(with: "2020-09-03T02:52:44"),
51+
date: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
52+
dateCreated: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
53+
dateModified: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
5454
dateOnSaleStart: nil,
5555
dateOnSaleEnd: nil,
5656
productTypeKey: ProductType.simple.rawValue,
@@ -151,9 +151,9 @@ final class ProductsRemoteTests: XCTestCase {
151151
name: "Product",
152152
slug: "product",
153153
permalink: "https://example.com/product/product/",
154-
date: date(with: "2020-09-03T02:52:44"),
155-
dateCreated: date(with: "2020-09-03T02:52:44"),
156-
dateModified: date(with: "2020-09-03T02:52:44"),
154+
date: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
155+
dateCreated: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
156+
dateModified: DateFormatter.dateFromString(with: "2020-09-03T02:52:44"),
157157
dateOnSaleStart: nil,
158158
dateOnSaleEnd: nil,
159159
productTypeKey: ProductType.simple.rawValue,
@@ -553,11 +553,11 @@ private extension ProductsRemoteTests {
553553
name: "Book the Green Room",
554554
slug: "book-the-green-room",
555555
permalink: "https://example.com/product/book-the-green-room/",
556-
date: date(with: "2019-02-19T17:33:31"),
557-
dateCreated: date(with: "2019-02-19T17:33:31"),
558-
dateModified: date(with: "2019-02-19T17:48:01"),
559-
dateOnSaleStart: date(with: "2019-10-15T21:30:00"),
560-
dateOnSaleEnd: date(with: "2019-10-27T21:29:59"),
556+
date: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
557+
dateCreated: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
558+
dateModified: DateFormatter.dateFromString(with: "2019-02-19T17:48:01"),
559+
dateOnSaleStart: DateFormatter.dateFromString(with: "2019-10-15T21:30:00"),
560+
dateOnSaleEnd: DateFormatter.dateFromString(with: "2019-10-27T21:29:59"),
561561
productTypeKey: "booking",
562562
statusKey: "publish",
563563
featured: false,
@@ -643,8 +643,8 @@ private extension ProductsRemoteTests {
643643

644644
func sampleImages() -> [Networking.ProductImage] {
645645
let image1 = ProductImage(imageID: 19,
646-
dateCreated: date(with: "2018-01-26T21:49:45"),
647-
dateModified: date(with: "2018-01-26T21:50:11"),
646+
dateCreated: DateFormatter.dateFromString(with: "2018-01-26T21:49:45"),
647+
dateModified: DateFormatter.dateFromString(with: "2018-01-26T21:50:11"),
648648
src: "https://somewebsite.com/thuy-nonjtpk.mystagingwebsite.com/wp-content/uploads/2018/01/vneck-tee.jpg.png",
649649
name: "Vneck Tshirt",
650650
alt: "")
@@ -677,11 +677,4 @@ private extension ProductsRemoteTests {
677677

678678
return [defaultAttribute1, defaultAttribute2]
679679
}
680-
681-
func date(with dateString: String) -> Date {
682-
guard let date = DateFormatter.Defaults.dateTimeFormatter.date(from: dateString) else {
683-
return Date()
684-
}
685-
return date
686-
}
687680
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
3+
/// DateFormatter Extensions
4+
///
5+
extension DateFormatter {
6+
7+
/// Date And Time Formatter. Converts String to Date type
8+
///
9+
static public func dateFromString(with dateString: String,
10+
locale: Locale? = .init(identifier: "en_US_POSIX"),
11+
timeZone: TimeZone? = .init(identifier: "GMT"),
12+
dateFormat: String? = "yyyy'-'MM'-'dd'T'HH:mm:ss") -> Date {
13+
14+
let formatter = DateFormatter()
15+
16+
formatter.locale = locale
17+
formatter.timeZone = timeZone
18+
formatter.dateFormat = dateFormat
19+
20+
guard let date = formatter.date(from: dateString) else {
21+
return Date()
22+
}
23+
24+
return date
25+
}
26+
}

WooCommerce/WooCommerceTests/Mocks/MockReviews.swift

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ extension MockReviews {
5656
name: productName,
5757
slug: "book-the-green-room",
5858
permalink: "https://example.com/product/book-the-green-room/",
59-
date: date(with: "2019-02-19T17:33:31"),
60-
dateCreated: date(with: "2019-02-19T17:33:31"),
61-
dateModified: date(with: "2019-02-19T17:48:01"),
62-
dateOnSaleStart: date(with: "2019-10-15T21:30:00"),
63-
dateOnSaleEnd: date(with: "2019-10-27T21:29:59"),
59+
date: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
60+
dateCreated: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
61+
dateModified: DateFormatter.dateFromString(with: "2019-02-19T17:48:01"),
62+
dateOnSaleStart: DateFormatter.dateFromString(with: "2019-10-15T21:30:00"),
63+
dateOnSaleEnd: DateFormatter.dateFromString(with: "2019-10-27T21:29:59"),
6464
productTypeKey: "booking",
6565
statusKey: "publish",
6666
featured: false,
@@ -146,8 +146,8 @@ extension MockReviews {
146146

147147
func sampleImages() -> [Networking.ProductImage] {
148148
let image1 = ProductImage(imageID: 19,
149-
dateCreated: date(with: "2018-01-26T21:49:45"),
150-
dateModified: date(with: "2018-01-26T21:50:11"),
149+
dateCreated: DateFormatter.dateFromString(with: "2018-01-26T21:49:45"),
150+
dateModified: DateFormatter.dateFromString(with: "2018-01-26T21:50:11"),
151151
src: "https://somewebsite.com/thuy-nonjtpk.mystagingwebsite.com/wp-content/uploads/2018/01/vneck-tee.jpg.png",
152152
name: "Vneck Tshirt",
153153
alt: "")
@@ -202,11 +202,11 @@ extension MockReviews {
202202
name: productName,
203203
slug: "book-the-green-room",
204204
permalink: "https://example.com/product/book-the-green-room/",
205-
date: date(with: "2019-02-19T17:33:31"),
206-
dateCreated: date(with: "2019-02-19T17:33:31"),
207-
dateModified: date(with: "2019-02-19T17:48:01"),
208-
dateOnSaleStart: date(with: "2019-10-15T21:30:00"),
209-
dateOnSaleEnd: date(with: "2019-10-27T21:29:59"),
205+
date: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
206+
dateCreated: DateFormatter.dateFromString(with: "2019-02-19T17:33:31"),
207+
dateModified: DateFormatter.dateFromString(with: "2019-02-19T17:48:01"),
208+
dateOnSaleStart: DateFormatter.dateFromString(with: "2019-10-15T21:30:00"),
209+
dateOnSaleEnd: DateFormatter.dateFromString(with: "2019-10-27T21:29:59"),
210210
productTypeKey: "booking",
211211
statusKey: "publish",
212212
featured: false,
@@ -289,14 +289,14 @@ extension MockReviews {
289289

290290
func sampleImagesMutated() -> [Networking.ProductImage] {
291291
let image1 = ProductImage(imageID: 19,
292-
dateCreated: date(with: "2018-01-26T21:49:45"),
293-
dateModified: date(with: "2018-01-26T21:50:11"),
292+
dateCreated: DateFormatter.dateFromString(with: "2018-01-26T21:49:45"),
293+
dateModified: DateFormatter.dateFromString(with: "2018-01-26T21:50:11"),
294294
src: "https://somewebsite.com/thuy-nonjtpk.mystagingwebsite.com/wp-content/uploads/2018/01/vneck-tee.jpg.png",
295295
name: "Vneck Tshirt",
296296
alt: "")
297297
let image2 = ProductImage(imageID: 999,
298-
dateCreated: date(with: "2019-01-26T21:44:45"),
299-
dateModified: date(with: "2019-01-26T21:54:11"),
298+
dateCreated: DateFormatter.dateFromString(with: "2019-01-26T21:44:45"),
299+
dateModified: DateFormatter.dateFromString(with: "2019-01-26T21:54:11"),
300300
src: "https://somewebsite.com/thuy-nonjtpk.mystagingwebsite.com/wp-content/uploads/2018/01/test.png",
301301
name: "ZZZTest Image",
302302
alt: "")
@@ -328,11 +328,11 @@ extension MockReviews {
328328
name: "Paper Airplane - Black, Long",
329329
slug: "paper-airplane-3",
330330
permalink: "https://paperairplane.store/product/paper-airplane/?attribute_color=Black&attribute_length=Long",
331-
date: date(with: "2019-04-04T22:06:45"),
332-
dateCreated: date(with: "2019-04-04T22:06:45"),
333-
dateModified: date(with: "2019-04-09T20:24:03"),
334-
dateOnSaleStart: date(with: "2019-10-15T21:30:00"),
335-
dateOnSaleEnd: date(with: "2019-10-27T21:29:59"),
331+
date: DateFormatter.dateFromString(with: "2019-04-04T22:06:45"),
332+
dateCreated: DateFormatter.dateFromString(with: "2019-04-04T22:06:45"),
333+
dateModified: DateFormatter.dateFromString(with: "2019-04-09T20:24:03"),
334+
dateOnSaleStart: DateFormatter.dateFromString(with: "2019-10-15T21:30:00"),
335+
dateOnSaleEnd: DateFormatter.dateFromString(with: "2019-10-27T21:29:59"),
336336
productTypeKey: "variation",
337337
statusKey: "publish",
338338
featured: false,
@@ -394,8 +394,8 @@ extension MockReviews {
394394

395395
func sampleVariationTypeImages() -> [Networking.ProductImage] {
396396
let image1 = ProductImage(imageID: 301,
397-
dateCreated: date(with: "2019-04-09T20:23:58"),
398-
dateModified: date(with: "2019-04-09T20:23:58"),
397+
dateCreated: DateFormatter.dateFromString(with: "2019-04-09T20:23:58"),
398+
dateModified: DateFormatter.dateFromString(with: "2019-04-09T20:23:58"),
399399
src: "https://i0.wp.com/paperairplane.store/wp-content/uploads/2019/04/paper_plane_black.png?fit=600%2C473&ssl=1",
400400
name: "paper_plane_black",
401401
alt: "")
@@ -421,14 +421,6 @@ extension MockReviews {
421421

422422
return [attribute1, attribute2]
423423
}
424-
425-
func date(with dateString: String) -> Date {
426-
guard let date = DateFormatter.Defaults.dateTimeFormatter.date(from: dateString) else {
427-
return Date()
428-
}
429-
return date
430-
}
431-
432424
}
433425

434426

WooCommerce/WooCommerceTests/Tools/MockOrders.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Yosemite
2+
import Foundation
23

34
final class MockOrders {
45
let siteID: Int64 = 1234
@@ -23,9 +24,9 @@ final class MockOrders {
2324
status: status,
2425
currency: "USD",
2526
customerNote: "",
26-
dateCreated: date(with: "2018-04-03T23:05:12"),
27-
dateModified: date(with: "2018-04-03T23:05:14"),
28-
datePaid: date(with: "2018-04-03T23:05:14"),
27+
dateCreated: DateFormatter.dateFromString(with: "2018-04-03T23:05:12"),
28+
dateModified: DateFormatter.dateFromString(with: "2018-04-03T23:05:14"),
29+
datePaid: DateFormatter.dateFromString(with: "2018-04-03T23:05:14"),
2930
discountTotal: "30.00",
3031
discountTax: "1.20",
3132
shippingTotal: "0.00",
@@ -197,15 +198,6 @@ final class MockOrders {
197198
taxes: [])]
198199
}
199200

200-
/// Converts a date string to a date type
201-
///
202-
func date(with dateString: String) -> Date {
203-
guard let date = DateFormatter.Defaults.dateTimeFormatter.date(from: dateString) else {
204-
return Date()
205-
}
206-
return date
207-
}
208-
209201
func refundsWithNegativeValue() -> [OrderRefundCondensed] {
210202
return [
211203
OrderRefundCondensed(refundID: 0, reason: nil, total: "-1.2"),

0 commit comments

Comments
 (0)