Skip to content

Commit

Permalink
Merge pull request #9 from fruitcake/feat-setOptions
Browse files Browse the repository at this point in the history
Test setOptions
  • Loading branch information
barryvdh authored Feb 20, 2022
2 parents 42ea548 + 7cf2a9d commit 6d8623f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0
## 1.1-dev (Unreleased)

### Added
- setOptions() method to overwrite or set the options later.

## 1.0.0 (2020-02-19)

### Changed since split from asm89/stack-cors

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"extra": {
"branch-alias": {
"dev-main": "1.0-dev"
"dev-main": "1.1-dev"
}
}
}
69 changes: 69 additions & 0 deletions tests/CorsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,75 @@ public function itCanHaveOptions(): void
$this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
}

/**
* @test
*/
public function itCanSetOptions(): void
{
$service = new CorsService();
$normalized = $this->getOptionsFromService($service);
$this->assertEquals([], $normalized['allowedOrigins']);

$this->assertInstanceOf(CorsService::class, $service);

$options = [
'allowedOrigins' => ['localhost'],
'allowedOriginsPatterns' => ['/something/'],
'allowedHeaders' => ['x-custom'],
'allowedMethods' => ['PUT'],
'maxAge' => 684,
'supportsCredentials' => true,
'exposedHeaders' => ['x-custom-2'],
];

$service->setOptions($options);

$normalized = $this->getOptionsFromService($service);

$this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']);
$this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']);
$this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']);
$this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']);
$this->assertEquals($options['maxAge'], $normalized['maxAge']);
$this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']);
$this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
}

/**
* @test
*/
public function itCanOverwriteSetOptions(): void
{
$service = new CorsService(['allowedOrigins' => ['example.com']]);
$normalized = $this->getOptionsFromService($service);

$this->assertEquals(['example.com'], $normalized['allowedOrigins']);

$this->assertInstanceOf(CorsService::class, $service);

$options = [
'allowedOrigins' => ['localhost'],
'allowedOriginsPatterns' => ['/something/'],
'allowedHeaders' => ['x-custom'],
'allowedMethods' => ['PUT'],
'maxAge' => 684,
'supportsCredentials' => true,
'exposedHeaders' => ['x-custom-2'],
];

$service->setOptions($options);

$normalized = $this->getOptionsFromService($service);

$this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']);
$this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']);
$this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']);
$this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']);
$this->assertEquals($options['maxAge'], $normalized['maxAge']);
$this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']);
$this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']);
}

/**
* @test
*/
Expand Down

0 comments on commit 6d8623f

Please sign in to comment.