3
3
* Test class for Optml_Attachment_Model.
4
4
*/
5
5
6
- require_once 'attachment_edit_utils.php ' ;
7
-
8
6
/**
9
7
* Class Test_Attachment_Model.
10
8
*/
11
9
class Test_Attachment_Model extends WP_UnitTestCase {
12
- use Attachment_Edit_Utils ;
13
- /**
14
- * DAM Instance
15
- *
16
- * @var Optml_Dam
17
- */
18
- private $ dam ;
10
+ protected static $ unscaled_id ;
11
+ protected static $ scaled_id ;
12
+ protected static $ remote_id ;
13
+
14
+ protected static $ unscaled_model ;
15
+ protected static $ scaled_model ;
16
+ protected static $ remote_model ;
19
17
20
18
const MOCK_REMOTE_ATTACHMENT = [
21
19
'url ' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/9.jpg ' ,
@@ -33,58 +31,66 @@ class Test_Attachment_Model extends WP_UnitTestCase {
33
31
],
34
32
];
35
33
36
- /**
37
- * @dataProvider models_provider
38
- */
39
- public function test_models ( $ id , $ model , $ scaled = false , $ remote = false ) {
34
+ public static function wpSetUpBeforeClass ( WP_UnitTest_Factory $ factory ) {
35
+ self ::$ unscaled_id = $ factory ->attachment ->create_upload_object ( OPTML_PATH . 'tests/assets/sample-test.jpg ' );
36
+ self ::$ scaled_id = $ factory ->attachment ->create_upload_object ( OPTML_PATH . 'tests/assets/3000x3000.jpg ' );
37
+
38
+ $ plugin = Optml_Main::instance ();
39
+ self ::$ remote_id = $ plugin ->dam ->insert_attachments ( [ self ::MOCK_REMOTE_ATTACHMENT ] )[0 ];
40
+
41
+ self ::$ unscaled_model = new Optml_Attachment_Model ( self ::$ unscaled_id );
42
+ self ::$ scaled_model = new Optml_Attachment_Model ( self ::$ scaled_id );
43
+ self ::$ remote_model = new Optml_Attachment_Model ( self ::$ remote_id );
44
+ }
45
+
46
+ public static function tear_down_after_class () {
47
+ wp_delete_post ( self ::$ unscaled_id , true );
48
+ wp_delete_post ( self ::$ scaled_id , true );
49
+ wp_delete_post ( self ::$ remote_id , true );
50
+ parent ::tear_down_after_class ();
51
+ }
52
+
53
+ public function test_barebones () {
54
+ $ this ->assertInstanceOf ( 'WP_Post ' , get_post ( self ::$ unscaled_id ) );
55
+ $ this ->assertInstanceOf ( 'WP_Post ' , get_post ( self ::$ scaled_id ) );
56
+ $ this ->assertInstanceOf ( 'WP_Post ' , get_post ( self ::$ remote_id ) );
57
+ }
58
+
59
+ public function test_models () {
60
+ $ this ->test_model ( self ::$ unscaled_id , self ::$ unscaled_model );
61
+ $ this ->test_model ( self ::$ scaled_id , self ::$ scaled_model , true );
62
+ $ this ->test_model ( self ::$ remote_id , self ::$ remote_model , false , true );
63
+ }
64
+
65
+ private function test_model ( $ id , $ model , $ scaled = false , $ remote = false ) {
40
66
$ this ->test_basic_getters ( $ id , $ model );
41
67
$ this ->test_filename_methods ( $ model );
42
68
$ this ->test_image_sizes_methods ( $ model );
43
69
$ this ->test_metadata_prefix_path ( $ model );
44
-
45
-
46
-
47
70
$ this ->assertEquals ( $ scaled , $ model ->is_scaled () );
48
71
$ this ->assertIsBool ( $ model ->can_be_renamed_or_replaced () );
49
-
50
72
$ this ->assertEquals ( ! $ remote , $ model ->can_be_renamed_or_replaced () );
51
-
52
- if ( $ remote ) {
53
- $ this ->assertEquals ( self ::MOCK_REMOTE_ATTACHMENT ['url ' ], $ model ->get_main_url () );
54
- }
55
-
56
- $ this ->delete_attachment ( $ id );
57
- }
58
-
59
- public function models_provider () {
60
- $ plugin = Optml_Main::instance ();
61
- $ dam = $ plugin ->dam ;
62
-
63
- $ unscaled_attachment = $ this ->create_attachment_get_id ( OPTML_PATH . 'tests/assets/sample-test.jpg ' );
64
- $ scaled_attachment = $ this ->create_attachment_get_id ( OPTML_PATH . 'tests/assets/3000x3000.jpg ' );
65
- $ remote_attachment = $ dam ->insert_attachments ( [ self ::MOCK_REMOTE_ATTACHMENT ] )[0 ];
66
-
67
- $ unscaled_model = new Optml_Attachment_Model ( $ unscaled_attachment );
68
- $ scaled_model = new Optml_Attachment_Model ( $ scaled_attachment );
69
- $ remote_model = new Optml_Attachment_Model ( $ remote_attachment );
70
-
71
- return [
72
- [ $ unscaled_attachment , $ unscaled_model ],
73
- [ $ scaled_attachment , $ scaled_model , true ],
74
- [ $ remote_attachment , $ remote_model , false , true ],
75
- ];
76
73
}
77
74
75
+ /**
76
+ * Test basic model getters.
77
+ *
78
+ * @param int $id Post ID.
79
+ * @param Optml_Attachment_Model $model The model to test.
80
+ *
81
+ * @return void
82
+ */
78
83
private function test_basic_getters ( $ id , $ model ) {
79
84
$ this ->assertEquals ( $ id , $ model ->get_attachment_id () );
80
- $ this ->assertNotEmpty ( $ model ->get_main_url () );
81
85
$ this ->assertNotEmpty ( $ model ->get_attachment_metadata () );
82
86
83
87
if ( $ model ->can_be_renamed_or_replaced () ) {
88
+ $ this ->assertNotEmpty ( $ model ->get_main_url () );
84
89
$ this ->assertNotEmpty ( $ model ->get_source_file_path () );
85
90
$ this ->assertNotEmpty ( $ model ->get_dir_path () );
86
91
$ this ->assertEquals ( 'jpg ' , $ model ->get_extension () );
87
92
} else {
93
+ $ this ->assertFalse ( $ model ->get_main_url () );
88
94
$ this ->assertEmpty ( $ model ->get_source_file_path () );
89
95
$ this ->assertEmpty ( $ model ->get_dir_path () );
90
96
$ this ->assertEmpty ( $ model ->get_extension () );
0 commit comments