From 4852cab27919c179eb1922229fe2eb45e0483d2b Mon Sep 17 00:00:00 2001 From: Joe Maller Date: Sun, 4 Aug 2024 23:07:08 -0400 Subject: [PATCH] add __call magic method, tests. Closes #22 --- src/SVG.php | 9 +++++ tests/CaseNormalizationTest.php | 11 ----- tests/MagicMethodTest.php | 71 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 tests/MagicMethodTest.php diff --git a/src/SVG.php b/src/SVG.php index bb80b79..1275ca6 100644 --- a/src/SVG.php +++ b/src/SVG.php @@ -387,6 +387,12 @@ public function __get($name) return $this->embed($name); } + public function __call($name, $arguments) + { + $attributes = $arguments[0] ?? []; + return $this->fetch($name, $attributes); + } + /** * TODO: Alternate name, getSVG(). * @param string $key @@ -418,6 +424,9 @@ public function fetch($key, $attributes = []) * NOTE: The magic __get method can only accept a single argument, so embed must be * called directly if args are being used. * + * This is now mostly just a wrapper for the fetch method but returns the svg + * property instead of the entire SVG object. + * * @param string $key * @param array $attributes * @return mixed diff --git a/tests/CaseNormalizationTest.php b/tests/CaseNormalizationTest.php index 4da8de9..3ec2f1e 100644 --- a/tests/CaseNormalizationTest.php +++ b/tests/CaseNormalizationTest.php @@ -24,17 +24,6 @@ public function setUp(): void public function beforeEach() { } - /** - * Test magic methods for embedding SVGs - * - * Exact matches should work when quoted. - * All names should also be directly callable using camelCase - */ - public function testMagicMethodsFound() - { - $arrow = $this->SVG->arrow; - $this->assertStringContainsString('SVG = new SVG(); + } + + /** + * Test magic methods for embedding SVGs + * + * Exact matches should work when quoted. + * All names should also be directly callable using camelCase + */ + public function testMagicGet() + { + $arrow = $this->SVG->arrow; + $this->assertStringContainsString(' 25]; + + $svg = $this->getMockBuilder('\IdeasOnPurpose\WP\SVG') + ->disableOriginalConstructor() + ->onlyMethods(['fetch']) + ->getMock(); + + $svg->expects($this->once()) + ->method('fetch') + ->with($this->equalTo('arrow'), $this->equalTo($widthArg)); + + $svg->arrow($widthArg); + // $this->assertStringContainsString('assertStringContainsString('width="25"', $arrow); + } + + public function testMagicCallNoAttributes() + { + $svg = $this->getMockBuilder('\IdeasOnPurpose\WP\SVG') + ->disableOriginalConstructor() + ->onlyMethods(['fetch']) + ->getMock(); + + $svg->expects($this->once()) + ->method('fetch') + ->with($this->equalTo('arrow'), $this->equalTo([])); + + $svg->arrow(); + } +}