-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add per-test fixture definition #6600
base: develop
Are you sure you want to change the base?
Conversation
ae8d779
to
c78712e
Compare
c41ae83
to
f753e02
Compare
Ping @luke- |
@martin-rueegg Thanks for the PR.
|
I too favor typed variables. In this case I see it as a convenience, as IDEs do easier support different types at the top level of an variable as they do within an array. But yes, writing 'default' rather than ['default'] is not a big gain in convenience, it just makes it more intuitive, unless the field is actually strongly typed to e.g. So, would the following better suit you?
I see.
In comparison to my solution, this places the definition for a given method to a different place within the code. I liked the fact that the definition of the fixture is visible and obvious straight away. But I'm sure we could still do something along the lines and the author of the test can still add a The suggested format ( $fixtureMethodConfig = [
// option 1: multiple methods with non-associative index:
[
// methods in key 0
['method1', 'method2'],
// fixtures in key 1 (following the rules of $fixtureConfig)
['fixture1', 'fixture4'],
]
// option 2: single-method configuration with associative index:
'method3' => [
// fixtures in key 0 (following the rules of $fixtureConfig)
]
]; Or we always put the fixture configuration in key 0, and the method name(s) in either the parent key or as an array in key 1 (simply changing the order of the arrays in option 1 above): $fixtureMethodConfig = [
// option 1: multiple methods with non-associative index:
[
// fixtures in key 0 (following the rules of $fixtureConfig)
['fixture1', 'fixture4'],
// methods in key 1
['method1', 'method2'],
]
// option 2: single-method configuration with associative index:
'method3' => [
// fixtures in key 0 (following the rules of $fixtureConfig)
]
]; |
Looks good for me. Thank you!
Maybe annotations could be a cool solution?
Unfortunately, I have not found anything for Codeception and PHPUnit. Perhaps worth to contribute there?
I'm open two both variants. It is somewhat similar to the Yii2 Model Rules. |
Attributes would even be cooler, I think. As they support PHP values and can be accessed using reflection. However, they're only available as of PHP v8. When do you plan to drop v7.4 support? Otherwise, yes, annotations might be something, as there are tools to read them. But it would require more engineering.
Yes, exactly. My suggestion 1 is maybe a bit more intuitive to use. Suggestion 2 would be slightly easier to evaluate. Maybe we should go for suggestion 1 then. Unless we wait for PHP 8 and use Attributes. |
Actually the plan is to "soft" drop PHP 7.4 support with v1.16. So for me we can use Attributes |
I see. So the v7.4 tests will be dropped, too? Because those would fail. Or would it mean to somehow exclude such tests from 7.4 test suite? E.g. by extending the |
Hmm, I'm not sure how we're going to handle it yet. |
c905e1f
to
0590ea5
Compare
@martin-rueegg Should we really mark "fixtureConfig" as deprecated. Shouldn't we just allow the attribute as an additional overwrite for methods? |
Usually it is you in favor of less options ... :-) I personally think whether adding a line #[FixtureDefault]
// which is an "alias" for
#[FixtureConfig(['default'])] or a line protected ?array $fixtureConfig = ['default']; does not really make a difference in terms of effort/convenience. The advantage of the attribute-only solution is that
I guess the attribute-base mechanism could be proposed to codeception, as you suggested? |
Hehe, that's right. Besides Keep it Simple, I want to change or refactor as few things as possible :-)
Ok, I just saw that '$fixtureConfig` is not used in our modules and only a little in the core. This means we could also remove it directly. Without marking it as deprecated first.
I'm not that familiar with the project, but it would of course be great to implement it there as a feature and to have the documentation for it there too. Would you like to try that? |
Awesome.
It is being discussed in |
Currently, test data is configured in the test config
yaml
files and can be tweaked in theHumHubDbTestCase::$fixtureConfig
, a feature that is hardly documented.Loading and unloading of data takes time and makes tests slow. So it would be preferred to do it as little as possible.
To test a class that has both methods that need DB access and other that don't, you either need to create two test classes, one with and one without fixtures to be loaded. Alternatively you can tweak
HumHubDbTestCase::$fixtureConfig
duringsetUp
based on theUnit::getName()
. However, while the former solution might still load unnecessary data, the latter solution disconnects the specification of the data-set from the test.Suggested Feature (update 2023-11-30)
This PR doe two things:
HumHubDbTestCase::$fixtureConfig
in favor of a new attribute-based solution.FixtureConfig
) that is supported on class and method level and allows to customize the fixture configuration on the respective level.The following functionality is supported:
#[FixtureEmpty]
to disable fixtures for the current unit/testFixtureConfig
allows to$useConfig
a flag to fall back to the configuration-level setting$useDefault
a flag to fall back to the default configuration of the attribute that extends fromFixtureConfig
and defines such a default set (seeFixtureDefault
)$fixtures
an array to cusomize the configuration['user' => ['class' => UserFixture::class], ...]
default
(as array value) which will mix in the default setuser
)Examples
Disable fixtures on class-level, but use fixtures from configuration for one test
Define fixtures on class-level, but disable them on one test
Define a default set of fixtures and use that in the test
The advantage of the attribute-only solution is that
Suggested Feature(outdated)This PR doe two things:
$fixtureConfig
:'default'
rather than just['default']
'alias'
or['alias']
: the alias denotes the fixture definition key as specified in the current class'getDefaultFixtures()
. It will be replaced with the respective fixture definition.false
: fixtures are disabledtrue
: If configured, the fixtures from the configuration will be used, or thedefault
set otherwise. Thisdiffers from
null
insofar as it will fall back todefault
if there is no fixture specified in theconfiguration.
$fixtures
parameter, e.g. like so:PR Admin
What kind of change does this PR introduce?
Does this PR introduce a breaking change?
The PR fulfills these requirements
develop
branch, not themaster
branch if no hotfixFix #xxx[,#xxx]
, where "xxx" is the Github issue number)If adding a new feature, the PR's description includes:
Other information: