1. Go to this page and download the library: Download helsingborg-stad/wpservice library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
helsingborg-stad / wpservice example snippets
use WpService\Implementations\NativeWpService;
$wpService = new NativeWpService();
$wpService->getTheId();
use WpService\Implementations\WpServiceWithTextDomain;
use WpService\Implementations\NativeWpService;
$service = new NativeWpService();
$service = new WpServiceWithTextDomain($service, 'my-text-domain');
$service->__('Hello World'); // Will automatically use 'my-text-domain' as the text domain.
use WpService\Contracts\GetTheId;
class MyService
{
public function __construct(private GetTheId $wpService)
{
}
public function getCurrentPostId(): int
{
return $this->wpService->getTheId();
}
}
use WpService\Implementations\FakeWpService;
use PHPUnit\Framework\TestCase;
class MyServiceTest extends TestCase
{
public function testGetCurrentPostId()
{
// Given
$fakeWpService = new FakeWpService(['getTheId' => 123]);
$myService = new MyService($fakeWpService);
// When
$postId = $myService->getCurrentPostId();
// Then
$this->assertEquals([123], $wpService->methodCalls['isSingle'][0]);
$this->assertEquals(123, $postId);
}
}
# Using a generic return value for all calls to the method.
$fakeWpService = new FakeWpService(['getTheTitle' => 'Test Title']);
$fakeWpService->getTheTitle(); // Returns 'Test Title'
$fakeWpService->getTheTitle(321); // Returns 'Test Title'
$fakeWpService->getTheTitle(123); // Returns 'Test Title'
# Using a callback to determine the return value based on the arguments passed to the method.
$return = fn($postId) => $postId === 123 ? 'Test Title' : '';
$fakeWpService = new FakeWpService(['getTheId' => $return]);
$fakeWpService->getTheTitle(); // Returns ''
$fakeWpService->getTheTitle(321); // Returns ''
$fakeWpService->getTheTitle(123); // Returns 'Test Title'
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.