Skip to content

Commit 0c94b7b

Browse files
committed
chore: attempt to fix phpunit
1 parent 141f2b5 commit 0c94b7b

File tree

4 files changed

+188
-170
lines changed

4 files changed

+188
-170
lines changed

inc/media_rename/attachment_db_renamer.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Optml_Attachment_Db_Renamer {
1717
*
1818
* @var array
1919
*/
20-
private $skip_tables = ['users', 'terms', 'term_relationships', 'term_taxonomy'];
20+
private $skip_tables = [ 'users', 'terms', 'term_relationships', 'term_taxonomy' ];
2121

2222
/**
2323
* Columns to skip during replacement
@@ -53,11 +53,11 @@ public function replace( $old_url, $new_url ) {
5353
return 0;
5454
}
5555

56-
if( empty($old_url) || empty($new_url) ) {
56+
if ( empty( $old_url ) || empty( $new_url ) ) {
5757
return 0;
5858
}
5959

60-
if( ! is_string($old_url) || ! is_string($new_url) ) {
60+
if ( ! is_string( $old_url ) || ! is_string( $new_url ) ) { // @phpstan-ignore-line docs require a string but it could be empty
6161
return 0;
6262
}
6363

@@ -392,10 +392,6 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n
392392
* @return string The processed value
393393
*/
394394
private function replace_urls_in_value( $value, $old_url, $new_url ) {
395-
var_dump([
396-
$value,
397-
$old_url,
398-
]);
399395
// Check if the value is serialized
400396
if ( $this->is_serialized( $value ) ) {
401397
$unserialized = @unserialize( $value );

tests/media_rename/test-attachment-model.php

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
* Test class for Optml_Attachment_Model.
44
*/
55

6-
require_once 'attachment_edit_utils.php';
7-
86
/**
97
* Class Test_Attachment_Model.
108
*/
119
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;
1917

2018
const MOCK_REMOTE_ATTACHMENT = [
2119
'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 {
3331
],
3432
];
3533

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 ) {
4066
$this->test_basic_getters( $id, $model );
4167
$this->test_filename_methods( $model );
4268
$this->test_image_sizes_methods( $model );
4369
$this->test_metadata_prefix_path( $model );
44-
45-
46-
4770
$this->assertEquals( $scaled, $model->is_scaled() );
4871
$this->assertIsBool( $model->can_be_renamed_or_replaced() );
49-
5072
$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-
];
7673
}
7774

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+
*/
7883
private function test_basic_getters( $id, $model ) {
7984
$this->assertEquals( $id, $model->get_attachment_id() );
80-
$this->assertNotEmpty( $model->get_main_url() );
8185
$this->assertNotEmpty( $model->get_attachment_metadata() );
8286

8387
if( $model->can_be_renamed_or_replaced() ) {
88+
$this->assertNotEmpty( $model->get_main_url() );
8489
$this->assertNotEmpty( $model->get_source_file_path() );
8590
$this->assertNotEmpty( $model->get_dir_path() );
8691
$this->assertEquals( 'jpg', $model->get_extension() );
8792
} else {
93+
$this->assertFalse( $model->get_main_url() );
8894
$this->assertEmpty( $model->get_source_file_path() );
8995
$this->assertEmpty( $model->get_dir_path() );
9096
$this->assertEmpty( $model->get_extension() );

tests/media_rename/test-attachment-rename.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,41 @@
66
/**
77
* Class Test_Attachment_Rename.
88
*/
9+
class Test_Attachment_Rename extends WP_UnitTestCase {
10+
protected static $scaled_id;
11+
protected static $unscaled_id;
912

10-
require_once 'attachment_edit_utils.php';
13+
protected static $scaled_model;
14+
protected static $unscaled_model;
1115

12-
class Test_Attachment_Rename extends WP_UnitTestCase {
13-
use Attachment_Edit_Utils;
16+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
17+
self::$scaled_id = $factory->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-scaled.jpg' );
18+
self::$unscaled_id = $factory->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' );
19+
20+
self::$scaled_model = new Optml_Attachment_Model( self::$scaled_id );
21+
self::$unscaled_model = new Optml_Attachment_Model( self::$unscaled_id );
22+
}
1423

15-
/**
16-
* @dataProvider rename_provider
17-
*/
18-
public function test_rename( $id, $model, $new_filename, $scaled = false ) {
24+
public static function tear_down_after_class() {
25+
wp_delete_post( self::$scaled_id, true );
26+
wp_delete_post( self::$unscaled_id, true );
27+
parent::tear_down_after_class();
28+
}
29+
30+
public function test_barebones() {
31+
$this->assertInstanceOf( 'WP_Post' , get_post( self::$scaled_id ) );
32+
$this->assertInstanceOf( 'WP_Post' , get_post( self::$unscaled_id ) );
33+
34+
$this->assertEquals('attachment', get_post_type( self::$scaled_id ) );
35+
$this->assertEquals('attachment', get_post_type( self::$unscaled_id ) );
36+
}
37+
38+
public function test_renames() {
39+
$this->test_rename( self::$unscaled_id, self::$unscaled_model, 'renamed-image' );
40+
$this->test_rename( self::$scaled_id, self::$scaled_model, 'big-file-rename', true );
41+
}
42+
43+
private function test_rename( $id, $model, $new_filename, $scaled = false ) {
1944
$renamer = new Optml_Attachment_Rename( $id, $new_filename );
2045
$result = $renamer->rename();
2146

@@ -27,19 +52,6 @@ public function test_rename( $id, $model, $new_filename, $scaled = false ) {
2752
$this->check_rename_with_models( $new_model, $model, $scaled );
2853
}
2954

30-
public function rename_provider( $callee ) {
31-
$unscaled = $this->create_attachment_get_id( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' );
32-
$scaled = $this->create_attachment_get_id( OPTML_PATH . 'tests/assets/rename-scaled.jpg' );
33-
34-
$unscaled_model = new Optml_Attachment_Model( $unscaled );
35-
$scaled_model = new Optml_Attachment_Model( $scaled );
36-
37-
return [
38-
[ $unscaled, $unscaled_model, 'renamed-image' ],
39-
[ $scaled, $scaled_model, 'big-file-rename', true ],
40-
];
41-
}
42-
4355
private function check_rename_with_models( $new, $old, $scaled = false ) {
4456
$this->assertNotEquals( $old->get_filename_no_ext(), $new->get_filename_no_ext() );
4557

0 commit comments

Comments
 (0)