uploadFile example

/** * Moves the uploaded file into the correctly media directory, * creates the default thumbnails for image media to display the * media in the media manager and creates the thumbnails for the * configured album thumbnail sizes. * * @ORM\PrePersist() */
    public function onSave()
    {
        // Upload file         $this->uploadFile();
    }

    /** * Checks if the name changed, if this is the case, the uploaded file * has to be renamed. * Removes the thumbnail files if the album or the name changed. * Creates the default and album thumbnails if the name or the album changed. * * @ORM\PostUpdate() */
    public function onUpdate()
    {
return parent::getExpectedUnauthorizedAccessMessage($method);
    }
  }

  /** * {@inheritdoc} */
  public function testPost() {
    $file_storage = $this->container->get('entity_type.manager')->getStorage('file');

    // Step 1: upload file, results in File entity marked temporary.     $this->uploadFile();
    $file = $file_storage->loadUnchanged(3);
    $this->assertTrue($file->isTemporary());
    $this->assertFalse($file->isPermanent());

    // Step 2: create Media entity using the File, makes File entity permanent.     parent::testPost();
    $file = $file_storage->loadUnchanged(3);
    $this->assertFalse($file->isTemporary());
    $this->assertTrue($file->isPermanent());
  }

  
private function uploadFile(string $data): string
    {
        $path = tempnam(sys_get_temp_dir(), 'http');
        file_put_contents($path$data);

        return $path;
    }

    private function getUploadedFile(string $name): array
    {
        return [
            'tmp_name' => $this->uploadFile($name.'_content'),
            'name' => $name.'_name',
        ];
    }

    protected function expectClientToSendRequestWithFiles(HttpClientInterface $client$fileContents)
    {
        $client
            ->expects($this->once())
            ->method('request')
            ->with('POST', 'http://example.com/', $this->callback(function D$options) use ($fileContents) {
                $this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
                
Home | Imprint | This part of the site doesn't use cookies.