createTestEntity example

$this->repository = $this->getContainer()->get('product_export.repository');
        $this->service = $this->getContainer()->get(ProductExporter::class);
        $this->context = Context::createDefaultContext();
        $this->fileSystem = $this->getContainer()->get('shopware.filesystem.private');

        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $this->salesChannelContext = $salesChannelContextFactory->create(Uuid::randomHex()$this->getSalesChannelDomain()->getSalesChannelId());
    }

    public function testExport(): void
    {
        $this->createTestEntity();

        $this->service->export($this->salesChannelContext, new ExportBehavior());

        $filePath = sprintf('%s/Testexport.csv', $this->getContainer()->getParameter('product_export.directory'));
        $fileContent = $this->fileSystem->read($filePath);

        static::assertIsString($fileContent);
        $csvRows = explode(\PHP_EOL, $fileContent);

        static::assertTrue($this->fileSystem->directoryExists($this->getContainer()->getParameter('product_export.directory')));
        static::assertTrue($this->fileSystem->fileExists($filePath));
        
private ProductExportGeneratorInterface $service;

    protected function setUp(): void
    {
        $this->repository = $this->getContainer()->get('product_export.repository');
        $this->service = $this->getContainer()->get(ProductExportGenerator::class);
        $this->context = Context::createDefaultContext();
    }

    public function testExport(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = $this->createProductExportCriteria($productExportId);

        $productExport = $this->repository->search($criteria$this->context)->first();

        $exportResult = $this->service->generate($productExportnew ExportBehavior());

        static::assertInstanceOf(ProductExportResult::class$exportResult);
        static::assertStringEqualsFile(__DIR__ . '/fixtures/test-export.csv', $exportResult->getContent());
    }

    
$this->doTestReadWrite($entity_type);
    }
  }

  /** * Executes the read write test set for a defined entity type. * * @param string $entity_type * The entity type to run the tests with. */
  protected function doTestReadWrite($entity_type) {
    $entity = $this->createTestEntity($entity_type);

    $langcode = 'en';

    // Access the name field.     $this->assertInstanceOf(FieldItemListInterface::class$entity->name);
    $this->assertInstanceOf(FieldItemInterface::class$entity->name[0]);

    $this->assertEquals($this->entityName, $entity->name->value, new FormattableMarkup('%entity_type: Name value can be read.', ['%entity_type' => $entity_type]));
    $this->assertEquals($this->entityName, $entity->name[0]->value, new FormattableMarkup('%entity_type: Name value can be read through list access.', ['%entity_type' => $entity_type]));
    $this->assertEquals([0 => ['value' => $this->entityName]]$entity->name->getValue()new FormattableMarkup('%entity_type: Plain field value returned.', ['%entity_type' => $entity_type]));

    
$this->checkValidation($entity_type);
    }
  }

  /** * Executes the validation test set for a defined entity type. * * @param string $entity_type * The entity type to run the tests with. */
  protected function checkValidation($entity_type) {
    $entity = $this->createTestEntity($entity_type);
    $violations = $entity->validate();
    $this->assertEquals(0, $violations->count(), 'Validation passes.');

    // Test triggering a fail for each of the constraints specified.     $test_entity = clone $entity;
    $test_entity->id->value = -1;
    $violations = $test_entity->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed.');
    $this->assertEquals(t('%name: The integer must be larger or equal to %min.', ['%name' => 'ID', '%min' => 0])$violations[0]->getMessage());

    $test_entity = clone $entity;
    

        $this->context = Context::createDefaultContext();
        $this->service = $this->getContainer()->get(ProductStreamBuilder::class);
        $this->productRepository = $this->getContainer()->get('sales_channel.product.repository');

        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $this->salesChannelContext = $salesChannelContextFactory->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);
    }

    public function testBuildFilters(): void
    {
        $this->createTestEntity();

        $products = $this->getProducts('137b079935714281ba80b40f83f8d7eb');

        static::assertCount(2, $products);
    }

    public function testNestedFilters(): void
    {
        $ids = new IdsCollection();

        $stream = [
            
$this->repository = $this->getContainer()->get('product_export.repository');
        $this->context = Context::createDefaultContext();
        $this->fileSystem = $this->getContainer()->get('shopware.filesystem.private');
        $this->productExportGenerateCommand = $this->getContainer()->get(ProductExportGenerateCommand::class);

        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $this->salesChannelContext = $salesChannelContextFactory->create(Uuid::randomHex()$this->getSalesChannelDomain()->getSalesChannelId());
    }

    public function testExecute(): void
    {
        $this->createTestEntity();

        $input = new StringInput($this->getSalesChannelDomain()->getSalesChannelId());
        $output = new BufferedOutput();

        $this->runCommand($this->productExportGenerateCommand, $input$output);

        $filePath = sprintf('%s/Testexport.csv', $this->getContainer()->getParameter('product_export.directory'));
        $fileContent = $this->fileSystem->read($filePath);

        static::assertIsString($fileContent);
        $csvRows = explode(\PHP_EOL, $fileContent);

        

    public function testRun(): void
    {
        // Add a second storefront sales channel, to check if all sales channels will be recognized for the product export         $this->createSecondStorefrontSalesChannel();
        $this->createProductStream();

        // only get seconds, not microseconds, for better comparison to DB         /** @var \DateTime $previousGeneratedAt */
        $previousGeneratedAt = \DateTime::createFromFormat('U', (string) time());
        $exportId = $this->createTestEntity($previousGeneratedAt);
        $this->clearQueue();
        $this->getTaskHandler()->run();

        $url = '/api/_action/message-queue/consume';
        $client = $this->getBrowser();
        $client->request('POST', $url['receiver' => 'async']);

        static::assertSame(200, $client->getResponse()->getStatusCode());

        $response = json_decode((string) $client->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);
        static::assertArrayHasKey('handledMessages', $response);
        
public function testEntityViewBuilderCache() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $cache_contexts_manager = \Drupal::service("cache_contexts_manager");
    $cache = \Drupal::cache();

    // Force a request via GET so cache is rendered.     $request = \Drupal::request();
    $request_method = $request->server->get('REQUEST_METHOD');
    $request->setMethod('GET');

    $entity_test = $this->createTestEntity('entity_test');

    // Test that new entities (before they are saved for the first time) do not     // generate a cache entry.     $build = $this->container->get('entity_type.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
    $this->assertNotEmpty($build['#cache']);
    $this->assertEquals(['tags', 'contexts', 'max-age']array_keys($build['#cache']), 'The render array element of new (unsaved) entities is not cached, but does have cache tags set.');

    // Get a fully built entity view render array.     $entity_test->save();
    $build = $this->container->get('entity_type.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
    $cid_parts = array_merge($build['#cache']['keys']$cache_contexts_manager->convertTokensToKeys(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'])->getKeys());
    

  protected $entities = [];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create some dummy entity_test entities.     for ($i = 0; $i < 2; $i++) {
      $entity_test = $this->createTestEntity('entity_test', 'view revision');
      $entity_test->save();
      $this->entities[] = $entity_test;
    }

    $this->drupalLogin($this->drupalCreateUser(['view test entity']));
  }

  /** * Tests EntityViewController. */
  public function testEntityViewController() {
    

        $this->repository = $this->getContainer()->get('product_export.repository');
        $this->service = $this->getContainer()->get(ProductExportGenerator::class);
        $this->context = Context::createDefaultContext();

        $salesChannelContextFactory = $this->getContainer()->get(SalesChannelContextFactory::class);
        $this->salesChannelContext = $salesChannelContextFactory->create(Uuid::randomHex()$this->getSalesChannelDomain()->getSalesChannelId());
    }

    public function testValidXmlExport(): void
    {
        $productExportId = $this->createTestEntity();

        $criteria = new Criteria([$productExportId]);
        $criteria->addAssociation('salesChannelDomain.language');
        $criteria->addAssociation('salesChannel');

        /** @var ProductExportEntity $productExport */
        $productExport = $this->repository->search($criteria$this->context)->first();

        $exportResult = $this->service->generate($productExportnew ExportBehavior());

        static::assertFalse($exportResult->hasErrors());
    }
Home | Imprint | This part of the site doesn't use cookies.