PHP code example of open-dxp / test-foundation

1. Go to this page and download the library: Download open-dxp/test-foundation 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/ */

    

open-dxp / test-foundation example snippets




declare(strict_types=1);

namespace Acme\BlogBundle\Tests\Application;

use Acme\BlogBundle\AcmeBlogBundle;
use OpenDxp\HttpKernel\BundleCollection\BundleCollection;
use OpenDxp\TestFoundation\Kernel\TestKernel as BaseTestKernel;

final class TestKernel extends BaseTestKernel
{
    public function registerBundlesToCollection(BundleCollection $collection): void
    {
        $collection->addBundle(new AcmeBlogBundle());
    }
}



declare(strict_types=1);

use OpenDxp\TestFoundation\TestCase;

pest()->extend(TestCase::class)->in('Feature');



declare(strict_types=1);

use Acme\BlogBundle\Manager\PostManager;
use OpenDxp\TestFoundation\Container;

it('boots with the bundle and its services', function () {
    expect(Container::get(PostManager::class))->toBeInstanceOf(PostManager::class);
});



declare(strict_types=1);

namespace App\Tests;

use App\Kernel;
use OpenDxp\TestFoundation\Kernel\Testable;

final class TestKernel extends Kernel
{
    use Testable;
}

use DAMA\DoctrineTestBundle\PHPUnit\SkipDatabaseRollback;
use OpenDxp\TestFoundation\DatabaseSnapshot;
use OpenDxp\TestFoundation\EnvironmentTestCase;

#[SkipDatabaseRollback]
abstract class FullPageCacheWithoutRollbackTestCase extends EnvironmentTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        DatabaseSnapshot::save();
    }

    protected function tearDown(): void
    {
        DatabaseSnapshot::restore();

        parent::tearDown();
    }

    protected static function environment(): string
    {
        return 'full_page_cache';
    }
}

pest()->extend(WithoutRollbackTestCase::class)->in('Feature/Concurrency');

use OpenDxp\TestFoundation\Concurrency;
use OpenDxp\TestFoundation\Container;

it('redeems a voucher code only once', function () {

    VoucherFactory::createOne(['code' => 'WELCOME']);

    $redeem = function (): int {
        $client = Container::testClient();
        $client->request('POST', '/voucher/WELCOME/redeem');

        return $client->getResponse()->getStatusCode();
    };

    expect(Concurrency::run($redeem, $redeem))
        ->toEqualCanonicalizing([
            200,
            409,
        ]);
});

use OpenDxp\TestFoundation\EnvironmentTestCase;

abstract class FullPageCacheTestCase extends EnvironmentTestCase
{
    protected static function environment(): string
    {
        return 'full_page_cache';
    }
}

Container::get(PostManager::class);
Container::parameter('acme_blog.posts_per_page');

use OpenDxp\TestFoundation\PHPUnit\BootKernelTrait;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

final class InstallSearchIndex implements Extension
{
    use BootKernelTrait;

    public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
    {
        $kernel = self::bootKernel();

        // install the index

        $kernel->shutdown();
    }
}

$path = Files::create('upload.pdf', megabytes: 25);

$browser = Browser::start();
$browser
    ->client()
    ->setServerParameter('HTTP_CLIENT_IP', GeoIp::addressIn('CH'));
$browser->visit('/');

Browser::visit('/en/about-us')->assertSee('About us');

Browser::playwrightActingAs(UserFactory::new()->admin()->create())
    ->visit('/admin')
    ->waitUntilVisible('div#opendxp_panel_tree_objects');

$page = Page::open('/en/contact')
    ->fillIn('Name', 'Ada')
    ->select('Topic', 'Support')
    ->press('Send');

expect($page->notifications())->toBe(['Thank you']);

Admin::actingAs(UserFactory::new()->admin()->create());
xml
<bootstrap class="OpenDxp\TestFoundation\PHPUnit\InstallDefinitions">
    <parameter name="opendxp-definitions" value="true"/>
</bootstrap>