Skip to content

createDrawingShape has no container defined #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
- Word2007 Reader: Fixed cast of color by [@Progi1984](https://github.com/Progi1984) fixing [#826](https://github.com/PHPOffice/PHPPresentation/pull/826) in [#840](https://github.com/PHPOffice/PHPPresentation/pull/840)
- Word2007 Reader: Fixed panose with 20 characters by [@Progi1984](https://github.com/Progi1984) fixing [#798](https://github.com/PHPOffice/PHPPresentation/pull/798) in [#842](https://github.com/PHPOffice/PHPPresentation/pull/842)
- `Gd::setImageResource()` : Fixed when imagecreatetruecolor returns false by [@jaapdh](https://github.com/jaapdh) in [#843](https://github.com/PHPOffice/PHPPresentation/pull/843)
- Word2007 Writer: LineChart supports LabelPosition for Series by [@pal-software](https://github.com/jaapdh) fixing [#606](https://github.com/PHPOffice/PHPPresentation/pull/606) in [#8434](https://github.com/PHPOffice/PHPPresentation/pull/844)
- Word2007 Writer: LineChart supports LabelPosition for Series by [@pal-software](https://github.com/pal-software) fixing [#606](https://github.com/PHPOffice/PHPPresentation/pull/606) in [#8434](https://github.com/PHPOffice/PHPPresentation/pull/844)
- `createDrawingShape` has no container defined by [@Progi1984](https://github.com/Progi1984) fixing [#820](https://github.com/PHPOffice/PHPPresentation/pull/820) in [#845](https://github.com/PHPOffice/PHPPresentation/pull/845)

## Miscellaneous

Expand Down
7 changes: 6 additions & 1 deletion src/PhpPresentation/Traits/ShapeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PhpOffice\PhpPresentation\Traits;

use PhpOffice\PhpPresentation\AbstractShape;
use PhpOffice\PhpPresentation\ShapeContainerInterface;

trait ShapeCollection
{
Expand Down Expand Up @@ -80,7 +81,11 @@ public function setShapeCollection(array $shapeCollection = []): self
*/
public function addShape(AbstractShape $shape)
{
$this->shapeCollection[] = $shape;
if (!$shape->getContainer() && $this instanceof ShapeContainerInterface) {
$shape->setContainer($this);
} else {
$this->shapeCollection[] = $shape;
}

return $this;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/PhpPresentation/Tests/Slide/AbstractSlideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class AbstractSlideTest extends TestCase
public function testCollection(): void
{
/** @var AbstractSlide $stub */
$stub = $this->getMockForAbstractClass('PhpOffice\\PhpPresentation\\Slide\\AbstractSlide');
$stub = $this->getMockForAbstractClass(AbstractSlide::class);

$array = [];
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Slide\\AbstractSlide', $stub->setShapeCollection($array));
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
self::assertIsArray($stub->getShapeCollection());
self::assertCount(count($array), $stub->getShapeCollection());

Expand All @@ -48,22 +48,22 @@ public function testCollection(): void
new RichText(),
new RichText(),
];
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Slide\\AbstractSlide', $stub->setShapeCollection($array));
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));
self::assertIsArray($stub->getShapeCollection());
self::assertCount(count($array), $stub->getShapeCollection());
}

public function testsearchShapes(): void
public function testSearchShapes(): void
{
/** @var AbstractSlide $stub */
$stub = $this->getMockForAbstractClass('PhpOffice\\PhpPresentation\\Slide\\AbstractSlide');
$stub = $this->getMockForAbstractClass(AbstractSlide::class);

$array = [
(new RichText())->setName('AAA'),
(new Table())->setName('BBB'),
(new Chart())->setName('AAA'),
];
self::assertInstanceOf('PhpOffice\\PhpPresentation\\Slide\\AbstractSlide', $stub->setShapeCollection($array));
self::assertInstanceOf(AbstractSlide::class, $stub->setShapeCollection($array));

// Search by Name
$result = $stub->searchShapes('AAA', null);
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpPresentation/Tests/SlideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace PhpOffice\PhpPresentation\Tests;

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Drawing\File;
use PhpOffice\PhpPresentation\ShapeContainerInterface;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Slide\AbstractBackground;
use PhpOffice\PhpPresentation\Slide\Animation;
Expand Down Expand Up @@ -143,4 +145,40 @@ public function testVisible(): void
self::assertInstanceOf(Slide::class, $object->setIsVisible());
self::assertTrue($object->isVisible());
}

public function testAddShape(): void
{
$slide = new Slide();
self::assertInstanceOf(ShapeContainerInterface::class, $slide);
$shape = new File();

self::assertIsArray($slide->getShapeCollection());
self::assertCount(0, $slide->getShapeCollection());

$slide->addShape($shape);
self::assertInstanceOf(File::class, $shape);
self::assertEquals($slide, $shape->getContainer());
self::assertInstanceOf(Slide::class, $shape->getContainer());

self::assertIsArray($slide->getShapeCollection());
self::assertCount(1, $slide->getShapeCollection());
self::assertEquals([$shape], $slide->getShapeCollection());
}

public function testCreateDrawingShape(): void
{
$slide = new Slide();

self::assertIsArray($slide->getShapeCollection());
self::assertCount(0, $slide->getShapeCollection());

$shape = $slide->createDrawingShape();
self::assertInstanceOf(File::class, $shape);
self::assertEquals($slide, $shape->getContainer());
self::assertInstanceOf(Slide::class, $shape->getContainer());

self::assertIsArray($slide->getShapeCollection());
self::assertCount(1, $slide->getShapeCollection());
self::assertEquals([$shape], $slide->getShapeCollection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public function testAxisTitleRotation(): void
$this->assertZipXmlAttributeNotExists($pathShape, $element, 'rot');

$this->resetPresentationFile();
$value = mt_rand(0, 360);
$value = mt_rand(1, 360);
$oShape->getPlotArea()->getAxisX()->setTitleRotation($value);

$pathShape = 'ppt/charts/' . $oShape->getIndexedFilename();
Expand Down
Loading