Skip to content

Commit

Permalink
Support binary content which have in memory path (sonata-project#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksom authored and greg0ire committed May 25, 2016
1 parent 1ef5010 commit 4b87b5e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Provider/FileProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ protected function setFileContents(MediaInterface $media, $contents = null)
return;
}

if ($media->getBinaryContent() instanceof File) {
$file->setContent(file_get_contents($media->getBinaryContent()->getRealPath()), $metadata);
$binaryContent = $media->getBinaryContent();
if ($binaryContent instanceof File) {
$path = $binaryContent->getRealPath() ?: $binaryContent->getPathname();
$file->setContent(file_get_contents($path), $metadata);

return;
}
Expand Down
81 changes: 81 additions & 0 deletions Tests/Provider/FileProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,85 @@ public function mediaProvider()
array(null, $media3),
);
}

public function testBinaryContentWithRealPath()
{
$media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');

$media->expects($this->any())
->method('getProviderReference')
->willReturn('provider');

$media->expects($this->any())
->method('getId')
->willReturn(10000);

$media->expects($this->any())
->method('getContext')
->willReturn('context');

$binaryContent = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
->setMethods(array('getRealPath', 'getPathname'))
->disableOriginalConstructor()
->getMock();

$binaryContent->expects($this->atLeastOnce())
->method('getRealPath')
->willReturn(__DIR__.'/../fixtures/file.txt');

$binaryContent->expects($this->never())
->method('getPathname');

$media->expects($this->any())
->method('getBinaryContent')
->willReturn($binaryContent);

$provider = $this->getProvider();

$setFileContents = new \ReflectionMethod('Sonata\MediaBundle\Provider\FileProvider', 'setFileContents');
$setFileContents->setAccessible(true);

$setFileContents->invoke($provider, $media);
}

public function testBinaryContentStreamWrapped()
{
$media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');

$media->expects($this->any())
->method('getProviderReference')
->willReturn('provider');

$media->expects($this->any())
->method('getId')
->willReturn(10000);

$media->expects($this->any())
->method('getContext')
->willReturn('context');

$binaryContent = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
->setMethods(array('getRealPath', 'getPathname'))
->disableOriginalConstructor()
->getMock();

$binaryContent->expects($this->atLeastOnce())
->method('getRealPath')
->willReturn(false);

$binaryContent->expects($this->atLeastOnce())
->method('getPathname')
->willReturn(__DIR__.'/../fixtures/file.txt');

$media->expects($this->any())
->method('getBinaryContent')
->willReturn($binaryContent);

$provider = $this->getProvider();

$setFileContents = new \ReflectionMethod('Sonata\MediaBundle\Provider\FileProvider', 'setFileContents');
$setFileContents->setAccessible(true);

$setFileContents->invoke($provider, $media);
}
}

0 comments on commit 4b87b5e

Please sign in to comment.