Codeception/AspectMock

test::double not working with basic PHP 5.6 project

danAtInv opened this issue · 1 comments

This is likely a duplicate of #125 so feel free to close and merge. That bug report used Laravel while this one is even simpler - just a basic PHP 5.6 project.

It also used the test code straight from the README:

  function testTableName()
  {
    $this->assertEquals('users', UserModel::tableName());
    $userModel = test::double('UserModel', ['tableName' => 'my_users']);
    $this->assertEquals('my_users', UserModel::tableName());
    $userModel->verifyInvoked('tableName');
  }

UserModel definition:

class UserModel {
    public static function tableName() {
        return 'users';
    }
}

Test fails with:

There was 1 failure:

1) UserModelTest::testTableName
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'my_users'
+'users'

Source code, including bootstrap file here: https://gitlab.com/daniel.lishingman/aspect-mock-problem-demo/tree/NotWorkingWithoutNamespace

I figured out that the problem was that the System Under Test (UserModel class) was not using namespaces. Once I added a namespace (and made the auto-loader also load it using that namespace), test::double started working.
I have updated the code in the example repo with these changes here:
https://gitlab.com/daniel.lishingman/aspect-mock-problem-demo/tree/WorkingUsingNamespace

Feel free to close this issue. I feel like the first example in the README could be updated to make this more clear as that's the code I initially copied. That might help future consumers.