lundi 4 mai 2015

Can't get AspectMock to work

I'm trying to get AspectMock to work for days and can't find the problem..

I've created a new test project with only 2 files, an User and a UserService class which uses User. PHPunit boots normal without problems, AspectMock doens't give any errors registering doubles, but they aren't working.

I've read that it's very important not to include test data inside the folder that also holds the source files. But in my case I separated these in src and test.

From the "manual" AspectMock on gitHub:

It's pretty important to configure AspectMock properly. Otherwise it may not work as expected or you get side effects. Please make sure you included all files that you need to mock, but your test files as well as testing frameworks are excluded.

I can't find what I'm missing here.. Sorry for the long post including all the code, but I think it's needed to find the error..

Can someone help me out?

My folder structure:

├───src
│   ├───User.php
│   ├───UserService.php
├───tests
│   ├───UserTest.php
├───vendor
│   ├───bin
│   ├───codeception
│   │   └───aspect-mock
│   │───more packages that aspect mock uses..
├───autoload.php
├───bootstrap.php
├───composer.json
├───phpunit.xml

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         bootstrap="bootstrap.php"
>
    <testsuites>
        <testsuite name="Package Test Suite">
            <directory suffix=".php">./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

bootstrap.php

$loader = require( __DIR__ . '/vendor/autoload.php' ); // composer autoload

$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'debug' => true,
    'includePaths' => [
        __DIR__ . '/src'
    ],
    'interceptFunctions' => true
]);

$kernel->loadFile('autoload.php');

autoload.php

require_once 'src/User.php';
require_once 'src/UserService.php';

User.php

namespace demo;

class User
{
    private $name;

    public function __construct()
    {
        echo "User->__construct() called\n";
    }
    public function setName($name)
    {
        echo "User->setName() called\n";
        $this->name = $name;
    }
    public function getName()
    {
        echo "User->getName() called\n";
        return $this->name;
    }
    public function save()
    {
        echo "User->save() called (WE DO NOT WANT TO SEE THIS)\n";
    }
}

UserService.php

namespace demo;

class UserService
{
    public function createUserByName($name)
    {
        echo "UserService->createUserByName() called\n";

        $user = new demo\User();
        $user->setName($name);
        $user->save();

        return $user;
    }
}

UserTest.php

namespace demo;

use AspectMock\Test as test;

class UserTest extends \PHPUnit_Framework_TestCase
{
    protected function tearDown()
    {
        test::clean(); // remove all registered test doubles
    }

    public function testUserCreate()
    {
        $userProxy = test::double('demo\User', ['save' => function () {
            echo "MOCKED User->save() called\n";
        }]);

        $service = new UserService;
        $user = $service->createUserByName('Zachary');
    }
}

Output when runned:

PHPUnit 4.6.2 by Sebastian Bergmann and contributors.

Configuration read from E:\Development\delme\phpunit.xml

.UserService->createUserByName() called
User->__construct() called
User->setName() called
User->save() called (WE DO NOT WANT TO SEE THIS)
User->getName() called


Time: 187 ms, Memory: 11.00Mb

OK (1 test, 1 assertion)

Aucun commentaire:

Enregistrer un commentaire