{"issue":{"created_at":"2011-11-17T16:15:03Z","state":"open","updated_at":"2011-11-21T16:42:21Z","user":{"gravatar_id":"95ee5a741eef2851af634ab1a4e06251","id":696765,"url":"https://api.github.com/users/iigorr","login":"iigorr","avatar_url":"https://secure.gravatar.com/avatar/95ee5a741eef2851af634ab1a4e06251?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"comments":3,"body":"Scope of Change\r\n---------------\r\nA mocking framework will be added to the unittest API.\r\n\r\nRationale\r\n---------\r\nEasily mock interfaces and verify expected behaviour similar to EasyMock (Java),\r\nRhino Mocks (.Net) and many other mocking frameworks.\r\n\r\nFunctionality\r\n-------------\r\nA new namespace 'unittest.mock' is introduced containing everything that enables\r\nmock functionallity. A new class 'unittest.mock.Mockery' provides a method\r\n'createMock($interfaceName)'. This method generates a mock object which\r\nimplements the required interface and provides methods for defining and\r\nverifying expected behaviour.\r\n\r\nThe mocking framework should follow the record/replay/verfiy paradigm as\r\ndescribed here http://martinfowler.com/articles/mocksArentStubs.html or here\r\nhttp://ayende.com/Blog/archive/2007/12/26/The-RecordReplayVerify-model.aspx\r\n\r\nIn the first step, the mocking of return values will be implemented. Later,\r\nfollowing features may follow:\r\n* Execution order verification\r\n* Strick mocks (unsatisfied expectation)\r\n* Rewinding the replay state\r\n* Switching back to record after replay started\r\n\r\nExample (Return values):\r\nConsider an interface like the following.\r\n\r\n```\r\ninterface Session {\r\n  ... //many methods\r\n  public function getId();\r\n}\r\n```\r\nNow we have the following method which we would like to unittest:\r\n\r\n```php\r\npublic function foo(Session $session) {\r\n  $this->bar(); //do something else\r\n  $this->baz($session->getId()); //do something with the session id\r\n}\r\n```\r\n\r\nTo cover this method it is irrelevant how the session object looks like.\r\nOnly the session id is of interest. Thus it would be nice if one could quickly\r\ncreate a mock object for this:\r\n\r\n```\r\n[@test]\r\npublic function fooIsCalledWithoutException() {\r\n  $mockery= new Mockery();\r\n  $sessionMock= $mockery->createMock('Session');\r\n  $sessionMock->getId()->returns(7); //return 7 if getId is called\r\n  $sessionMock->replay();\r\n\r\n  $this->sut->foo($sessionMock);\r\n}\r\n```\r\n\r\nSecurity considerations\r\n-----------------------\r\nn/a\r\n\r\nSpeed impact\r\n------------\r\nUnittests will run faster if simple mocks are used instead of complex\r\ninstantiation of dependency objects.\r\n\r\nDependencies\r\n------------\r\nXP Framework\r\n\r\nRelated documents\r\n-----------------\r\nhttp://experiments.xp-forge.net/xml/view?arena,mocks,INTRODUCTION.txt\r\n\r\nComments\r\n--------\r\n","assignee":null,"id":2270458,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":219,"labels":[{"name":"discussion","color":"327230","url":"https://api.github.com/repos/xp-framework/rfc/labels/discussion"},{"name":"framework","color":"118f5b","url":"https://api.github.com/repos/xp-framework/rfc/labels/framework"},{"name":"implemented","color":"807370","url":"https://api.github.com/repos/xp-framework/rfc/labels/implemented"}],"title":"Mocking Library","closed_at":null,"html_url":"https://github.com/xp-framework/rfc/issues/219","url":"https://api.github.com/repos/xp-framework/rfc/issues/219","milestone":null},"action":"created","comment":{"created_at":"2011-11-21T16:42:21Z","updated_at":"2011-11-21T16:42:21Z","user":{"gravatar_id":"26f07e3b262e9a80436f368cb11bd0c2","id":696742,"url":"https://api.github.com/users/thekid","login":"thekid","avatar_url":"https://secure.gravatar.com/avatar/26f07e3b262e9a80436f368cb11bd0c2?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"Can you add the tutorial to this RFC? The experiments checkout no longer exists as central repository here on GitHub and this link will most probably lead to nowhere in the future. Sorry for being pedantic about this, but we want to keep all the necessary information in the RFC repository to be able to look back at our decisions in their entirety in the future. Thanks for understanding!","id":2817452,"url":"https://api.github.com/repos/xp-framework/rfc/issues/comments/2817452"}}