PHP code example of robvanaarle / php-object-seam

1. Go to this page and download the library: Download robvanaarle/php-object-seam library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

robvanaarle / php-object-seam example snippets


class FooTest
{
    use PHPObjectSeam\CreatesObjectSeams;
    
    public function testBar(): void
    {
        $foo = $this->createObjectSeam(Foo::class);
        // $foo has type Foo&PHPObjectSeam\ObjectSeam
        
        // Access seam through seam() to alter the behaviour of the object
        $foo->seam()
          ->override('connect', fn ($username, $password) => 'dummy_token')
          ->customConstruct(function($arg1) {
              $this->url = 'http://www.dummy.url/' . $arg1;
          }, 'api/v1/');
          
        // do something with $foo and perform an assertion
    }
}

$foo = $this->createObjectSeam(Foo::class);  
$result = $foo->seam()->call('nonPublicMethod', $arg1, $arg2);

$foo = $this->createObjectSeam(Foo::class);
$result = $foo->seam()->callStatic('protectedStaticMethod', $arg1, $arg2);

$foo = $this->createObjectSeam(Foo::class);
$result = $foo->seam()->override('protectedMethod', function(int $arg1) {
  return $this->otherMethod($arg1) * 5;
});

$foo = $this->createObjectSeam(Foo::class);
$result = $foo->seam()->override('protectedMethod', 42);

$foo = $this->createObjectSeam(Foo::class);
$result = $foo->seam()->overrideStatic('protectedStaticMethod', function(int $arg1) {
  return parent::protectedMethod($arg1) * 3;
});

$foo = $this->createObjectSeam(Foo::class);
$result = $foo->seam()->overrideStatic('protectedStaticMethod', 9);

$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->customConstruct(function($arg1) {
    $this->url = 'http://www.dummy.url/' . $arg1;
}, 'api/v1/');

// i.e. in the setup of your test
$this->foo = $this->createObjectSeam(Foo::class);
$this->foo->seam()->setCustomConstructor(function($arg1) {
    $this->url = 'http://www.dummy.url/' . $arg1;
});

// in a specific test case
$this->foo->callCustomConstructor('api/v1/');

$this->foo = $this->createObjectSeam(Foo::class);
$this->foo->seam()->call('methodThatDoesNotUseThisKeyword');

$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->call('__construct', 'bar');

$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->callConstruct('bar');

$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->captureCalls('publicOrProtectedMethod');

// do something with $foo
$foo->publicMethod();

$calls = $foo->seam()->getCapturedCalls('publicOrProtectedMethod');
// assert that $calls contains a certain combination of arguments.

$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->captureStaticCalls('publicOrProtectedStaticMethod');

// do something with $foo
$foo::publicMethod();

$calls = $foo->seam()->getCapturedStaticCalls('publicOrProtectedMethod');
// assert that $calls contains a certain combination of arguments.