Skip to content

Commit a98bb8c

Browse files
Copilotswissspidy
andcommitted
Refactor tests to reduce duplication with helper method
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 6d5b7e4 commit a98bb8c

1 file changed

Lines changed: 20 additions & 58 deletions

File tree

tests/PackageAuthTest.php

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,24 @@ public function tear_down() {
4040
}
4141

4242
/**
43-
* Test that GITHUB_TOKEN is added to COMPOSER_AUTH.
43+
* Helper method to invoke the set_composer_auth_env_var method.
4444
*/
45-
public function test_github_token_added_to_composer_auth() {
46-
putenv( 'GITHUB_TOKEN=ghp_test123456789' );
47-
45+
private function invoke_set_composer_auth() {
4846
$package = new Package_Command();
4947
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
5048
if ( PHP_VERSION_ID < 80100 ) {
5149
$method->setAccessible( true );
5250
}
5351
$method->invoke( $package );
52+
}
53+
54+
/**
55+
* Test that GITHUB_TOKEN is added to COMPOSER_AUTH.
56+
*/
57+
public function test_github_token_added_to_composer_auth() {
58+
putenv( 'GITHUB_TOKEN=ghp_test123456789' );
59+
60+
$this->invoke_set_composer_auth();
5461

5562
$composer_auth = getenv( 'COMPOSER_AUTH' );
5663
$this->assertNotFalse( $composer_auth );
@@ -68,12 +75,7 @@ public function test_github_token_added_to_composer_auth() {
6875
public function test_gitlab_oauth_token_added_to_composer_auth() {
6976
putenv( 'GITLAB_OAUTH_TOKEN=glpat_test123456789' );
7077

71-
$package = new Package_Command();
72-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
73-
if ( PHP_VERSION_ID < 80100 ) {
74-
$method->setAccessible( true );
75-
}
76-
$method->invoke( $package );
78+
$this->invoke_set_composer_auth();
7779

7880
$composer_auth = getenv( 'COMPOSER_AUTH' );
7981
$this->assertNotFalse( $composer_auth );
@@ -91,12 +93,7 @@ public function test_gitlab_oauth_token_added_to_composer_auth() {
9193
public function test_gitlab_token_added_to_composer_auth() {
9294
putenv( 'GITLAB_TOKEN=glpat_test123456789' );
9395

94-
$package = new Package_Command();
95-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
96-
if ( PHP_VERSION_ID < 80100 ) {
97-
$method->setAccessible( true );
98-
}
99-
$method->invoke( $package );
96+
$this->invoke_set_composer_auth();
10097

10198
$composer_auth = getenv( 'COMPOSER_AUTH' );
10299
$this->assertNotFalse( $composer_auth );
@@ -115,12 +112,7 @@ public function test_bitbucket_oauth_added_to_composer_auth() {
115112
putenv( 'BITBUCKET_CONSUMER_KEY=test_key' );
116113
putenv( 'BITBUCKET_CONSUMER_SECRET=test_secret' );
117114

118-
$package = new Package_Command();
119-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
120-
if ( PHP_VERSION_ID < 80100 ) {
121-
$method->setAccessible( true );
122-
}
123-
$method->invoke( $package );
115+
$this->invoke_set_composer_auth();
124116

125117
$composer_auth = getenv( 'COMPOSER_AUTH' );
126118
$this->assertNotFalse( $composer_auth );
@@ -142,12 +134,7 @@ public function test_bitbucket_oauth_requires_both_credentials() {
142134
putenv( 'BITBUCKET_CONSUMER_KEY=test_key' );
143135
// BITBUCKET_CONSUMER_SECRET is not set
144136

145-
$package = new Package_Command();
146-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
147-
if ( PHP_VERSION_ID < 80100 ) {
148-
$method->setAccessible( true );
149-
}
150-
$method->invoke( $package );
137+
$this->invoke_set_composer_auth();
151138

152139
$composer_auth = getenv( 'COMPOSER_AUTH' );
153140
// No auth should be set because only one credential was provided
@@ -168,12 +155,7 @@ public function test_http_basic_auth_added_to_composer_auth() {
168155
);
169156
putenv( "HTTP_BASIC_AUTH=$http_basic" );
170157

171-
$package = new Package_Command();
172-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
173-
if ( PHP_VERSION_ID < 80100 ) {
174-
$method->setAccessible( true );
175-
}
176-
$method->invoke( $package );
158+
$this->invoke_set_composer_auth();
177159

178160
$composer_auth = getenv( 'COMPOSER_AUTH' );
179161
$this->assertNotFalse( $composer_auth );
@@ -194,12 +176,7 @@ public function test_http_basic_auth_added_to_composer_auth() {
194176
public function test_invalid_http_basic_auth_json_ignored() {
195177
putenv( 'HTTP_BASIC_AUTH=not-valid-json' );
196178

197-
$package = new Package_Command();
198-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
199-
if ( PHP_VERSION_ID < 80100 ) {
200-
$method->setAccessible( true );
201-
}
202-
$method->invoke( $package );
179+
$this->invoke_set_composer_auth();
203180

204181
$composer_auth = getenv( 'COMPOSER_AUTH' );
205182
// No auth should be set because the JSON was invalid
@@ -213,12 +190,7 @@ public function test_multiple_auth_providers() {
213190
putenv( 'GITHUB_TOKEN=ghp_test123' );
214191
putenv( 'GITLAB_TOKEN=glpat_test456' );
215192

216-
$package = new Package_Command();
217-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
218-
if ( PHP_VERSION_ID < 80100 ) {
219-
$method->setAccessible( true );
220-
}
221-
$method->invoke( $package );
193+
$this->invoke_set_composer_auth();
222194

223195
$composer_auth = getenv( 'COMPOSER_AUTH' );
224196
$this->assertNotFalse( $composer_auth );
@@ -245,12 +217,7 @@ public function test_existing_composer_auth_preserved() {
245217
putenv( "COMPOSER_AUTH=$existing_auth" );
246218
putenv( 'GITLAB_TOKEN=glpat_new_token' );
247219

248-
$package = new Package_Command();
249-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
250-
if ( PHP_VERSION_ID < 80100 ) {
251-
$method->setAccessible( true );
252-
}
253-
$method->invoke( $package );
220+
$this->invoke_set_composer_auth();
254221

255222
$composer_auth = getenv( 'COMPOSER_AUTH' );
256223
$this->assertNotFalse( $composer_auth );
@@ -279,12 +246,7 @@ public function test_env_tokens_dont_override_composer_auth() {
279246
putenv( "COMPOSER_AUTH=$existing_auth" );
280247
putenv( 'GITHUB_TOKEN=new_token' );
281248

282-
$package = new Package_Command();
283-
$method = new \ReflectionMethod( 'Package_Command', 'set_composer_auth_env_var' );
284-
if ( PHP_VERSION_ID < 80100 ) {
285-
$method->setAccessible( true );
286-
}
287-
$method->invoke( $package );
249+
$this->invoke_set_composer_auth();
288250

289251
$composer_auth = getenv( 'COMPOSER_AUTH' );
290252
$this->assertNotFalse( $composer_auth );

0 commit comments

Comments
 (0)