-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate at() matcher #4297
Comments
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but they order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but they order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but they order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but the order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but the order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but the order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
It has been deprecated as of PHPUnit 9 See sebastianbergmann/phpunit#4297 Note that we still rely on the call order here, I am not the original author of this test but the order looks like it is pretty important for this test, or maybe at least for understand it. Also note that we no longer check that save() is called after all calls to fetch().
I agree with the deprecation of this unholy method. Mostly But what about this scenario?
At which the second Maybe something with |
@Levivb This will work: $this->pageBuilder
->expects($this->exactly(3))
->method('create')
->withConsecutive([$this->collection[0]], [$this->collection[1]], [$this->collection[2]]);
->willReturnOnConsecutiveCalls(
null,
$this->throwException(new RuntimeException('Some message')),
null
); The only downside is that you'll have to define an explicit (and valid) return value for all calls. |
@allcode Thanks for your suggestion! I did consider that. It's just that the method in question has typehint |
If we describe calling one method with different parameters, we don't need $this->connection->expects($this->at(1))->method('begin');
$this->connection->expects($this->at(2))->method('commit'); After removing $transactionStarted = false;
$this->connection->expects($this->once())->method('begin')->willReturnCallback(function () use (&$transactionStarted) {
$transactionStarted = true;
});
$this->connection->expects($this->once())->method('commit')->willReturnCallback(function () use (&$transactionStarted) {
$this->assertTrue($transactionStarted);
}); In more complex tests, this code will become even uglier, so I think we definitely need an alternative to the |
it would be extraordinary nice if you guys could create some sort of documentation about how to properly migrate from using this method (what methods to use instead of the deprecated one). |
I could not agree more with @ossinkine, Sometimes I exactly want to test if a function is called on a specific position. For instance I have this code:
If I wanted to test that functionNameB has been called as 3rd when testing this first method, I would have a very difficult time, since I want to test that it has been called for that specific value. (value == 2) and not value == 1 or value == 3 Having tested that it is called one thing, but knowing the order of execution is in some cases as important as knowing it has been called Please reconsider |
It has been deprecated in PHPUnit 9, see sebastianbergmann/phpunit#4297
@ossinkine what would happen if a client would call the If throwing an exception, you could explicitly expect the exception to be thrown if you cann After all, you're testing your code, and how it reacts to being called with functions out-of-order. @pwellingelastique where does your The idea of removing Your example is such a brittle example: Let's say you refactor and introduce a new function Now you have to change your tests for Deprecating |
@artjomsimon My code calls the functions in certain order. I want to write a test which check the order is still correct. If I change my code where order was changed I want the test fails. If the code with wrong function calls run on production the exception will be thrown. |
@ossinkine it sounds like checking if function A was called is a responsibility of the class containing the two functions that need to be called in order:
Now, in your tests, you can test this behavior:
With the exception, you will make sure that anyone (you, your colleague, your end users, people who will work on your code after you leave) gets the Your Tests for the other class ( What if you refactor the Again, deprecating |
@artjomsimon I don't want to test class MyService
{
private SomeService $service;
public function __construct(SomeService $service)
{
$this->service = $service;
}
public function doSomething(): void
{
$this->functionNameA();
$this->functionNameB();
}
}
class MyServiceTest extends TestCase
{
public function testSomething(): void
{
$service = $this->createMock(SomeService::class);
$myService = new MyService($service);
$service->expects($this->at(1))->method('functionNameA');
$service->expects($this->at(2))->method('functionNameB');
$myService->doSomething();
}
} |
I believe that |
OK, thanks for the feedback |
Any estimations of when |
The first has an example at https://phpunit.readthedocs.io/en/9.5/test-doubles.html?highlight=withConsecutive#mock-objects and the latter behaves like But yeah, documentation is sketchy in places, especially when digging deeper… 😬 |
I have a legacy project with more than 500 uses of the If I copy the phpunit matcher class and add an alias for |
… to use non `->at` function, 'cause it deprecated in 9.0 and will be deleted in phpunit 10. Ref to sebastianbergmann/phpunit#4297
…emoved in PHPUnit 10. Please refactor your test to not rely on the order in which methods are invoked." See also sebastianbergmann/phpunit#4297 The replacement using `withConsecutive` ought to do it.
sebastianbergmann/phpunit#4297 The last assertion was not reached and now removed Change-Id: I81e32ba668802c26afe9a73db8c5243e8136f373
…emoved in PHPUnit 10. Please refactor your test to not rely on the order in which methods are invoked." See also sebastianbergmann/phpunit#4297 The replacement using `withConsecutive` ought to do it.
The behaviour of
at()
is confusing. Usingat()
leads to brittle tests at best and lying tests at worst. The documentation even states:Furthermore, the implementation of
at()
impedes improvements to other aspects of the test doubles support in PHPUnit, see #4291 (comment) for example.We should therefore deprecate
PHPUnit\Framework\TestCase::at()
(as well as thePHPUnit\Framework\at()
wrapper)PHPUnit\Framework\MockObject\Rule\InvokedAtIndex
in PHPUnit 9 and remove it in PHPUnit 10.
If we realize that a mechanism for specifying the order in which expected invocations occur then, and only then, shall we think about how to best implement this. A replacement for
at()
is explicitly outside the scope of this deprecation.The text was updated successfully, but these errors were encountered: