Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Sonata Laravel Package

ekino/sonata

View on GitHub
Deep Wiki
Context7

Test Helper Traits

Tests are a big part of the development process. In order to facilitate their implementation, here is a list of traits that you can use.

ClassTestHelper

It contains 1 public static functions.

getInstanceWithId

Return an instance of a class where the id is set. Useful to mock Doctrine entities.

$foo = ClassTestHelper::getInstanceWithId(Foo::class, 10);
echo $foo->getId() // 10

TranslationTestHelper

It contains 1 public static functions.

getTranslationString

Takes the same parameters as the trans function of Symfony TranslatorInterface and return a formatted string.

echo TranslationTestHelper::getTranslationString('foo', [], 'AppBundle');
// Key    : foo
// Params : {}
// Domain : AppBundle

MockBuilderTrait

It contains 1 public functions.

mockTranslator

Mock Symfony TranslatorInterface and assign getTranslationString as callback to the `trans method.

// Init the test
$translatorMock = $this->mockTranslator();
$myTestedService = new TestedService($translatorMock);
$result = $myTestedService->executeFunction();

// Validate the result
$this->assertSame(
  $result['translation_to_validate'], 
  TranslationTestHelper::getTranslationString('translation_key', $paramters, 'domain')
);

AdminFormFieldTrait

It contains 2 protected functions. It must be used in a class that extends \PHPUnit_Framework_TestCase.

mockFormMapper

Returns a mock of the FormMapper that stores all added fields in a public property fields (it also skips the groups).

expectInOrder

Use that previously created mock to test the configureFormFields method of the admin.

public function testConfigureFormFields()
{
  // ...
  $formMapper = $this->mockFormMapper($admin);
  $this->expectInOrder($formMapper, [
    ['title', TextType::class],
    ['position', NumberType::class],
  ]);
}

AdminTestCase

It contains 1 protected functions. It must be used in a class that extends \PHPUnit_Framework_TestCase.

mockDefaultServices

The trait contains already all properties to store the services passed to an admin by Sonata compiler pass. This method create all the mocks and add them to the admin.

// ...
$admin = new MyAdmin();
$this->mockDefaultServices($admin);
// If you need to override a service
$admin->setMyOverride($myOverride);

FragmentFormFieldTestTrait

It contains 1 protected functions. It must be used in a class that extends \PHPUnit_Framework_TestCase.

expectInOrder

Usually, in the buildForm of the FragmentService you will add fields to the setting hey of the form. This trait will help you validating the fields created by calling the buildForm method of the FragmentService.

// ...
public function testBuildForm()
{
  $fragment = new Fragment();
  $fragmentService = new MyFragmentService();
  // The $fragment paramerter is optional here
  // but sometimes, the result of the buildForm
  // depend on its values.
  $this->expectInOrder(
    $fragmentService, 
    [
      ['title', TextType::class],
      ['position', NumberType::class],
    ], 
    $fragment
  );
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui