PHP code example of adrhumphreys / silverstripe-fixtures
1. Go to this page and download the library: Download adrhumphreys/silverstripe-fixtures 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/ */
adrhumphreys / silverstripe-fixtures example snippets
namespace App\Fixtures;
use AdrHumphreys\Fixtures\AbstractFixture;
use SilverStripe\Dev\TestOnly;
class PageFixture extends AbstractFixture implements TestOnly
{
public function load(): void
{
$page = \Page::create();
$page->Title = 'Example title';
$page->URLSegment = 'example-page';
$page->write();
$page->publishRecursive();
}
public function getClassesToClear(): ?array
{
return [\Page::class];
}
}
namespace App\Fixtures;
use AdrHumphreys\Fixtures\AbstractFixture;
use AdrHumphreys\Fixtures\DependentFixtureInterface;
use SilverStripe\Dev\TestOnly;
class MyOtherPageFixture extends AbstractFixture implements DependentFixtureInterface, TestOnly
{
public function load(): void
{
// Example
}
public function getDependencies(): array
{
return [PageFixture::class];
}
}
namespace App\Fixtures;
use AdrHumphreys\Fixtures\AbstractFixture;
use AdrHumphreys\Fixtures\OrderedFixtureInterface;
use SilverStripe\Dev\TestOnly;
class PageFixture extends AbstractFixture implements OrderedFixtureInterface, TestOnly
{
public function load(): void
{
// Example
}
public function getOrder(): int
{
return 2;
}
}
namespace App\Fixtures;
use AdrHumphreys\Fixtures\AbstractFixture;
use SilverStripe\Dev\TestOnly;
class PageFixture extends AbstractFixture implements TestOnly
{
public const PAGE_REF = 'my-page-ref';
public function load(): void
{
$page = \Page::create();
$page->Title = 'Example title';
$page->URLSegment = 'example-page';
$page->write();
$page->publishRecursive();
$this->addReference(self::PAGE_REF, $page);
}
}
namespace App\Fixtures;
use AdrHumphreys\Fixtures\AbstractFixture;
use SilverStripe\Dev\TestOnly;
class PageFixtureTwo extends AbstractFixture implements TestOnly
{
public function load(): void
{
$refPage = $this->getByReference(PageFixture::PAGE_REF);
$page = \Page::create();
$page->Title = 'Example title';
$page->URLSegment = 'example-page';
$page->Body = 'PageID: ' . $refPage->ID;
$page->write();
$page->publishRecursive();
}
}