testcontainers_magento_data/core/image/
builder.rs1use crate::core::{ImageName, ImageRepository, ImageTag};
2
3pub trait ImageBuilder: Sized {
4 fn with_sample_data(self) -> Self;
5 fn with_variation(self, variation: impl AsRef<str>) -> Self;
6 fn with_version(self, version: impl AsRef<str>) -> Self;
7 fn with_repository(self, repository: impl ImageRepository) -> Self;
8}
9
10pub trait ImageBuilderTarget: Sized {
11 fn with_image_tag(self, image_tag: impl FnOnce(ImageTag) -> ImageTag) -> Self;
12 fn with_image_name(self, image_name: impl FnOnce(ImageName) -> ImageName) -> Self;
13}
14
15impl<T> ImageBuilder for T
16where
17 T: ImageBuilderTarget,
18{
19 fn with_sample_data(self) -> Self {
20 self.with_image_tag(|tag| tag.with_variation("sampledata"))
21 }
22
23 fn with_variation(self, variation: impl AsRef<str>) -> Self {
24 self.with_image_tag(|tag| tag.with_variation(variation))
25 }
26
27 fn with_version(self, version: impl AsRef<str>) -> Self {
28 self.with_image_tag(|tag| tag.with_version(version))
29 }
30
31 fn with_repository(self, repository: impl ImageRepository) -> Self {
32 self.with_image_name(|name| name.with_repository(repository))
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 use std::borrow::Cow;
40
41 struct TestImage(ImageName, ImageTag);
42
43 impl ImageBuilderTarget for TestImage {
44 fn with_image_tag(self, image_tag: impl FnOnce(ImageTag) -> ImageTag) -> Self {
45 Self(self.0, image_tag(self.1))
46 }
47
48 fn with_image_name(self, image_name: impl FnOnce(ImageName) -> ImageName) -> Self {
49 Self(image_name(self.0), self.1)
50 }
51 }
52
53 impl TestImage {
54 fn full_name(&self) -> Cow<str> {
55 Cow::Borrowed(self.0.as_ref()) + Cow::Borrowed(":") + Cow::Borrowed(self.1.as_ref())
56 }
57 }
58
59 #[test]
60 fn allows_providing_sample_data_variation() {
61 assert_eq!(
62 TestImage(ImageName::new("mysql"), ImageTag::default())
63 .with_sample_data()
64 .full_name(),
65 "ghcr.io/ecomdev/testcontainer-magento-data/mysql:latest-sampledata",
66 );
67 }
68
69 #[test]
70 fn allows_custom_version() {
71 assert_eq!(
72 TestImage(ImageName::new("mariadb"), ImageTag::default())
73 .with_version("2.4.8-beta1")
74 .full_name(),
75 "ghcr.io/ecomdev/testcontainer-magento-data/mariadb:2.4.8-beta1",
76 );
77 }
78
79 #[test]
80 fn allows_custom_variation() {
81 assert_eq!(
82 TestImage(ImageName::new("mariadb"), ImageTag::default())
83 .with_version("2.4.8")
84 .with_variation("generated-small")
85 .full_name(),
86 "ghcr.io/ecomdev/testcontainer-magento-data/mariadb:2.4.8-generated-small",
87 );
88 }
89
90 #[test]
91 fn allows_custom_repository() {
92 assert_eq!(
93 TestImage(ImageName::new("test"), ImageTag::default())
94 .with_repository("wardenenv/data")
95 .full_name(),
96 "wardenenv/data/test:latest",
97 );
98 }
99}