janmarek / mockista Goto Github PK
View Code? Open in Web Editor NEWFork of PHP mocking framework https://bitbucket.org/jiriknesl/mockista
Fork of PHP mocking framework https://bitbucket.org/jiriknesl/mockista
Mám problém s tím, že když volám v testech mock s jinými parametry, než které definuji. Tak dostanu např. výjimku: "Mockista\MockException: Expected method Flame\Doctrine\EntityDao::findOneBy() should be called exactly once but not called at all." přitom volána byla jednou, pouze s jinými parametry.
Tohle je očekávané chování? Nebo je někde problém?
Předem děkuji za pomoc.
Hello,
do you think it is possible to add support for PHP 7 scalar typehints?
Code sample:
<?php
class Repository
{
/**
* @param int
* @return int
*/
public function getImagesCountByUrlId(int $urlId) : int
{
}
}
Mocking class above throws this error:
Fatal error: Declaration of Repository_1813543779::getImagesCountByUrlId($urlId) must be compatible with Repository::getImagesCountByUrlId(int $urlId): int in vendor/janmarek/mockista/Mockista/MockBuilder.php(49) : eval()'d code on line 22
see here: http://forum.nette.org/cs/14953-mock-tridy-dedici-od-reflectionclass
I have prepared some workaround and test for this, but should be probably solved with ability to user to pass a complete declaration of mocked method, something like
$mockista->addMethodMap('InternalClass', 'method(arg1, arg2 = InternalClass::CONST')
When upgraded to PHP 5.4.21 (win), I started to getting this error:
Fatal error: Cannot redeclare class XY in ...\janmarek\mockista\Mockista\MockBuilder.php(41) : eval()'d code on line 220
When looking for the error source I found out that it's outside the Mockista - the problem is in the uniqid function. It could be generating same results when called quickly enough.
I met a situation where I do suspect mockista of an error behavior. I've got something like this
interface IExample {
public function x();
public function y();
}
class Something {
private $ex;
function __construct(IExample ex) {
$this->ex = $ex;
}
function something() {
$this->ex->c();
}
}
...
public function testSomething() {
$mock = $this->mockista->create('XY\IExample');
$mock->expects('c')
->once()
->andReturn(NULL);
$this->sut->something();
// and some assertion
}
...
I would expect mockista to tell me there is not such a method c in this mocked interface. Instead everything works as there is the method 'c', which is called in tested.
I met this during the refactoring session. I was refactoring the classes that implement the interface, changed the interface signature (removed that method) and come to this problem - I'm not capable to find out what classes do I need to change through test.
I could easily find it with IDE, but I guess this should be working too?
Is this a bug or did I make a mistake?
This is feature we just working on. It is not final yet, but I would like to have your input.
Working name for this is ProxyMock.
Main idea is that sometimes you do not want your mock object to mock all calls for some object just intercepts some calls and let everything else pass to some real instance. Basically in test you only care about part of the object behaviour and do not want to be forced to set expects for all calls.
Most useful is this if mocked object has some complex behavior or computation or if call sequence and/or internal state is important for return values.
Creating of proxy mock is simple. Just pass existing instance instead of class/interface name when creating mock:
class MyObject {
public function multipleTwo($param) {
return $param * 2;
}
public function plusOne($param) {
return $param +1;
}
}
$object = new MyObject();
$mock = $mockista->create($object);
When we set no expects it will just passthrought all calls to original object:
echo $mock->multipleTwo(10); // => 20
echo $mock->plusOne(10); // => 11
We can set expect for some calls and return different values:
$mock->expect('multipleTwo')->andReturn(100);
echo $mock->multipleTwo(10); // => 100
echo $mock->plusOne(10); // => 11
Of couse we can set expectations in number of calls as normal
$mock->expect('multipleTwo')->once();
echo $mock->multipleTwo(10); // => null
$mock->assertExpectations();
For case when we want to set expectation but still return value computed by original object, new and* method will be added:
$mock->expect('multipleTwo')->once()->andPassThrough();
echo $mock->multipleTwo(10); // => 20
$mock->assertExpectations();
Of course we can use some fake implementation of original object as well:
class MyDummyObject extends MyObject {
... some dummy behaviour
}
$mock = $mockista->create(new MyDummyObject());
So what do you think?
please :)
After couple of hours checking what is wrong (cpu high, everything freezed) I have found that implementation of throwing exception when method is wrong. Here is my (WIP) proposal - move the responsibility to Tracy.
Also there are some improvements with text of exception (see commit log).
Zdravim,
mam jednoduchy mock:
$page = $this->mockista->create('\Harvester\Page\Page', array(
'getUrl' => function() use($url) { return new Nette\Http\Url($url);},
));
a dostavam tento error:
Strict standards: Declaration of _Harvester_Page_Page_525bb7a224d22::__call()
should be compatible with LeanMapper\Entity::__call($name, array $arguments)
in /vendor/janmarek/mockista/Mockista/MockBuilder.php(41) : eval()'d code on line 148
pouzivam:
"tharos/leanmapper": "dev-master",
"janmarek/mockista": "dev-master",
Hello,
I've started working with mockista and I've rewritten tests from codeception to nette/tester. In codeception I've used PhpUnit's getMock()
method and tests were passing. When I use mockista it does not. Please see this test: https://github.com/annotatecms/packages/blob/nette-tester/tests/AnnotateCmsTests/Packages/PackageLoader.phpt#L215-L227. Do I use mockery in wrong way or is there a bug?
Edit: When I use jiriknesl/mockista it works and tests are passing. But I like the usage of your mockista more.
Hi,
I test create mock for Nette\Application\UI\Presenter
I can't use method getName()
because this method returns mock name instead of original value.
$presenterMock = $this->mockista->create('Nette\Application\UI\Presenter');
$presenterMock->expects('getName')->andReturn('Module:Presenter');
dump($presenterMock->getName()); // returns Nette\Application\IPresenter#4
Could you please create a new release containing the Matchers? :)
Invalid, sorry :)
Hi,
I found bug in returned exception, when I have simple code:
$obj = $mockista->create("Foo");
$obj->expects("bar")->with(1)->once();
$obj->bar(2);
I except message of exception:
Unexpected call in mock Foo::bar(), args: array (0 => 2)
but I get:
Expected method Foo::bar() should be called exactly once but not called at all.
But if I have same code, without checking calls count:
$obj = $mockista->create("Foo");
$obj->expects("bar")->with(1);
$obj->bar(2);
returned message is correct!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.