Skip to content

Commit

Permalink
add __call magic method, tests. Closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Aug 5, 2024
1 parent 9c4366c commit 4852cab
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions tests/CaseNormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', $arrow);
}

// public function testMagicMethodsNotFound()
// {
Expand Down
71 changes: 71 additions & 0 deletions tests/MagicMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php namespace IdeasOnPurpose\WP;

use PHPUnit\Framework\TestCase;
use IdeasOnPurpose\WP\Test;

Test\Stubs::init();
/**
/**
* @covers \IdeasOnPurpose\WP\SVG
*/
final class MagicMethodTest extends TestCase
{
public $SVG;

public function setUp(): void
{
$this->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('<svg', $arrow);
}

/**
* Test magic methods for embedding SVGs
*
* Exact matches should work when quoted.
* All names should also be directly callable using camelCase
*/
public function testMagicCall()
{
$widthArg = ['width' => 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('<svg', $arrow);
// $this->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();
}
}

0 comments on commit 4852cab

Please sign in to comment.