-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hotfix: Shift options feature to new namespace
- Loading branch information
Pretzlaw
committed
Sep 10, 2019
1 parent
4585377
commit 5e01065
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace RmpUp\WordPress\Fixtures\Entity; | ||
|
||
class Option extends \stdClass | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | ||
|
||
/** | ||
* CRUD for options | ||
* | ||
* LICENSE: This source file is created by the company around Mike Pretzlaw | ||
* located in Germany also known as rmp-up. All its contents are proprietary | ||
* and under german copyright law. Consider this file as closed source and/or | ||
* without the permission to reuse or modify its contents. | ||
* This license is available through the world-wide-web at the following URI: | ||
* https://mike-pretzlaw.de/license-generic.txt . If you did not receive a copy | ||
* of the license and are unable to obtain it through the web, please send a | ||
* note to [email protected] so we can mail you a copy. | ||
* | ||
* @package wp-fixtures | ||
* @copyright 2018 Mike Pretzlaw | ||
* @license https://mike-pretzlaw.de/license-generic.txt | ||
* @link https://project.mike-pretzlaw.de/wp-fixtures | ||
* @since 2019-02-02 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RmpUp\WordPress\Fixtures\Repository; | ||
|
||
use RmpUp\WordPress\Fixtures\Entity\Option; | ||
|
||
class Options extends AbstractRepository | ||
{ | ||
|
||
/** | ||
* @param Option $object Fixture to lookup. | ||
* @param string $fixtureName | ||
* | ||
* @return int|null ID or null when not found | ||
*/ | ||
public function find($object, string $fixtureName) | ||
{ | ||
// There is no such thing. | ||
return null; | ||
} | ||
|
||
/** | ||
* Remove options | ||
* | ||
* @param Option $object Associative property-value store. | ||
* @param string $fixtureName Name of the fixture for logging purposes. | ||
*/ | ||
public function delete($object, string $fixtureName) | ||
{ | ||
foreach (array_keys((array) $object) as $option) { | ||
delete_option($option); | ||
} | ||
} | ||
|
||
/** | ||
* Store given options. | ||
* | ||
* @param Option $object Associative property to value store. | ||
* | ||
* @return int | ||
*/ | ||
protected function create($object): int | ||
{ | ||
foreach (get_object_vars($object) as $option => $value) { | ||
update_option($option, $value); | ||
} | ||
|
||
// The option container shall never have an ID. | ||
return 0; | ||
} | ||
|
||
/** | ||
* Update option values. | ||
* | ||
* @param Option $object Associative property to value store. | ||
*/ | ||
protected function update($object) | ||
{ | ||
foreach (get_object_vars($object) as $option => $value) { | ||
update_option($option, $value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace RmpUp\WordPress\Fixtures\Test\Option; | ||
|
||
use RmpUp\WordPress\Fixtures\Entity\Option; | ||
use RmpUp\WordPress\Fixtures\Test\AbstractTestCase; | ||
|
||
/** | ||
* Options | ||
* | ||
* Setting options in WordPress can be done using a simple table like this: | ||
* | ||
* ```yaml | ||
* \RmpUp\WordPress\Fixtures\Entity\Option: | ||
* default: | ||
* home_url_perhaps: 'https://example.org' | ||
* some_plugin_api_token: afbdec456ddebdc84 | ||
* ``` | ||
* | ||
* The "default" is just a necessary placeholder while the actual options are listed below. | ||
* | ||
* @package RmpUp\WordPress\Fixtures\Test\Option | ||
*/ | ||
class PrimitiveValueTest extends AbstractTestCase { | ||
/** | ||
* @var Option | ||
*/ | ||
private $options; | ||
|
||
protected function setUp() { | ||
parent::setUp(); | ||
|
||
$this->options = $this->loadFromDocComment( 0 ); | ||
} | ||
|
||
public function testOptionsLoaded() { | ||
static::assertInstanceOf( Option::class, $this->options['default'] ); | ||
static::assertEquals( 'https://example.org', $this->options['default']->home_url_perhaps ); | ||
static::assertEquals( 'afbdec456ddebdc84', $this->options['default']->some_plugin_api_token ); | ||
} | ||
|
||
public function testOptionsAreStored() { | ||
// Flush all those options. | ||
$this->removeOptions(); | ||
|
||
static::assertInstanceOf( Option::class, $this->options['default'] ); | ||
$this->repo()->persist( $this->options['default'], 'default' ); | ||
|
||
// Flush cache after writing option to ensure that options are loaded from database again. | ||
wp_cache_flush(); | ||
|
||
static::assertEquals( 'https://example.org', get_option( 'home_url_perhaps' ) ); | ||
static::assertEquals( 'afbdec456ddebdc84', get_option( 'some_plugin_api_token' ) ); | ||
} | ||
|
||
public function testOptionsWillBeRemoved() { | ||
$this->testOptionsAreStored(); | ||
|
||
$this->repo()->delete( $this->options['default'], 'default' ); | ||
|
||
// Flush cache after writing option to ensure that options are looked up in the database again. | ||
wp_cache_flush(); | ||
|
||
static::assertFalse( get_option( 'home_url_perhaps' ) ); | ||
static::assertFalse( get_option( 'some_plugin_api_token' ) ); | ||
} | ||
|
||
private function removeOptions() { | ||
foreach ( $this->options['default'] as $option => $value ) { | ||
delete_option( $option ); | ||
} | ||
} | ||
|
||
protected function tearDown() { | ||
$this->removeOptions(); | ||
|
||
parent::tearDown(); | ||
} | ||
} |