Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active December 25, 2015 14:09
Show Gist options
  • Save mnapoli/6988778 to your computer and use it in GitHub Desktop.
Save mnapoli/6988778 to your computer and use it in GitHub Desktop.
Anonymous classes FTW
<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testSomeStuff()
{
// Build my mock
$mock = new class extends CrawlerInterface {
public function crawl($url) {
return ['some', 'data'];
}
} ();
// Do my test
$myService = new MyService($mock);
$this->assertTrue($myService->foo());
}
}
@MarcelloDuarte
Copy link

<?php

namespace spec;

use PHPSpec\ObjectBehavior;
use Prophecy\Argument;

use CrawlerInterface;

class MyServiceSpec extends ObjectBehavior
{
    function let(CrawlerInterface $crawler)
    {
        $this->beConstructedWith($crawler);
    }

    function it_do_some_stuff(CrawlerInterface $crawler)
    {
        $crawler->crawl(Argument::any())->willReturn(['some', 'data']);
        $this->foo()->shouldBe(true);
    }
}

@docteurklein
Copy link

phpspec/prophecy could take advantage of anon classes in its class generator, in that it would not have to find unique mock class names.

@stof
Copy link

stof commented Oct 16, 2013

@docteurklein no it cannot, as the RFC has been rejected

@docteurklein
Copy link

@stof yeah :) I should have said: "could have had".

@docteurklein
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment