PHP code example of vrok / symfony-addons

1. Go to this page and download the library: Download vrok/symfony-addons 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/ */

    

vrok / symfony-addons example snippets




use Vrok\SymfonyAddons\PHPUnit\ApiPlatformTestCase;

class AuthApiTest extends ApiPlatformTestCase
{
    public function testAuthRequiresPassword(): void
    {
        $this->testOperation([
            'uri'            => '/authentication_token',
            'method'         => 'POST',
            'requestOptions' => ['json' => ['username' => 'fakeuser']],
            'responseCode'   => 400,
            'contentType'    => 'application/json',
            'json'           => [
                'type'   => 'https://tools.ietf.org/html/rfc2616#section-10',
                'title'  => 'An error occurred',
                'detail' => 'The key "password" must be provided.',
            ],
        ]);
    }
}

'prepare' => static function (ContainerInterface $container, array &$params): void {
      $em = $container->get('doctrine')->getManager();

      $log = new ActionLog();
      $log->action = ActionLog::FAILED_LOGIN;
      $log->ipAddress = '127.0.0.1';
      $em->persist($log);
      $em->flush();

      $params['requestOptions']['query']['id'] = $log->id; 
}

'requestOptions' => [
  'json' => [
    'username' => 'Peter',
    'email'    => '[email protected]',
  ],
  
  // or:
  'query' =>  [
    'order' => ['createdAt' => 'asc'],
  ],
  
  // or:
  'headers' => ['content-type' => 'application/json'],
]

'files' => [
  'picture' => [
    'path'         => '/path/to/file.png',
    'originalName' => 'mypicture.png',
    'mimeType'     => 'image/png',
  ]
]

'json' => [
  'username' => 'Peter',
  'email'    => '[email protected]',
]

'

'forbiddenKeys' => ['hydra:member'][0]['password', 'salt']

'schemaClass' => User::class,

'createdLogs'    => [
  ['Failed to validate the provider', Level::Error],
],

 'emailCount' => 2,

 'messageCount' => 2,

'dispatchedMessages' => [
  TenantCreatedMessage::class,

  [TenantCreatedMessage::class, function (object $message, array $data): void {
      self::assertSame($data['id'], $message->tenantId);
  }]
],

'dispatchedEvents' => [
  'kernel.response',
  ProjectPreCreateEvent::class,
],

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Vrok\SymfonyAddons\PHPUnit\RefreshDatabaseTrait;

class DatabaseTest extends KernelTestCase
{
    use RefreshDatabaseTrait;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $entityManager;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

}

    protected static $fixtureGroups = ['test', 'other'];

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use Vrok\SymfonyAddons\PHPUnit\AuthenticatedClientTrait;

class ApiTest extends ApiTestCase
{
    use AuthenticatedClientTrait;

    public function testAccess(): void
    {
        $client = static::createAuthenticatedClient([
            'email' => TestFixtures::ADMIN['email']
        ]);

        $iri = $this->findIriBy(User::class, ['id' => 1]);
        $client->request('GET', $iri);
        self::assertResponseIsSuccessful();
    }
}
 

use Monolog\Level;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Vrok\SymfonyAddons\PHPUnit\MonologAssertsTrait;

class LoggerTest extends KernelTestCase
{
    use MonologAssertsTrait;

    public function testLog(): void
    {      
        self::prepareLogger();

        $logger = static::getContainer()->get(LoggerInterface::class);
        $logger->error('Failed to do something');
        
        self::assertLoggerHasMessage('Failed to do something', Level::Error);
    }
}
 

    public function __invoke(
        Entity $data
        WorkflowInterface $entityStateMachine,
    ): array
    {
      $result = $data->toArray();
      
      $result['transitions'] = WorkflowHelper::getTransitionList($data, $entityStateMachine);
      
      return $result;
    }

    Vrok\SymfonyAddons\VrokSymfonyAddonsBundle::class => ['all' => true],

use Vrok\SymfonyAddons\Event\CronDailyEvent;

class MyEventSubscriber implements EventSubscriberInterface
    public static function getSubscribedEvents(): array
    {
        return [
            CronDailyEvent::class => [
                ['onCronDaily', 100],
            ],
        ];
    }
}

#[ApiFilter(
    filterClass: SimpleSearchFilter::class,
    properties: [
        'description',
        'name',
        'slug',
    ],
    arguments: ['searchParameterName' => 'pattern']
)]

#[ApiFilter(filterClass: ContainsFilter::class, properties: ['numbers'])]

#[ApiFilter(filterClass: JsonExistsFilter::class, properties: ['roles'])]

    Vrok\SymfonyAddons\VrokSymfonyAddonsBundle::class => ['all' => true],

    Vrok\SymfonyAddons\VrokSymfonyAddonsBundle::class => ['all' => true],

    Vrok\SymfonyAddons\VrokSymfonyAddonsBundle::class => ['all' => true],