Skip to content

u/celtric

In this archive from Jan 2010 to Nov 2016

20
posts
257
comments
3
media
1.1k
score

That's an interesting concern. I personally work with simple assertions that are quick to visually check if they are valid or not, but even in the more complex cases I tend to think they are tested as a by-product of the tests that are calling them. (Basically if I change an assertion I should probably see something failing somewhere.)

It could very well make sense to verify that the most complex of assertions are actually working as intended. But then, should we also check the assertions that are inside each individual test (and not bundled together in this kind of "assertables")? I personally think the old "comment the production code and see the test fail" should cover most of the cases in which you're modifying an assertion.

1 points

Yes, that's a nice example! There are a few differences (basically having a single WebAssert vs one per each concept -AssertableUser, AssertableSession, etc.- and that by convention I prefer to start with assert...), but it's the closest thing I've seen so far.

Thanks for the link!

1 points

Yes, that's why I find it surprising that no one I've talked to uses this approach.

BTW, I'd find it helpful if you had a link at hand (in Github for example) of a codebase that uses this "pattern".

1 points

I was worried about some people not being able to follow the examples by using a specific language they didn't understand, but the same could be said about a fictional generic language like the one I used.

Though if I write them in, say, Java, some people will have even more reasons to complain about this article being posted in /r/PHP (something I want to avoid; the actual language is irrelevant in this post).

As for examples in multiple languages, I fear that the post sees its number of lines explode for little gain (the differences between languages would boil down to language constructs, and they'd end up being very similar).

I'll think about your suggestions, I'll see if I can improve the readability. Thanks!

1 points

Thanks for your input.

In this case, would you prefer to have one post with examples in PHP and another article written in some major language (or even one per language)? Or just have one single article written in a well-known language (probably not PHP)?

1 points

I started writing this post with examples in PHP, but I soon realized that the concepts were language-independent and that by switching to a fake generic languages I could focus on the concept rather than the implementation.

Given that this a place I know better and is much smaller than /r/programming, I've prefered to get a first feedback from /r/php before sending it to a bigger subreddit.

5 points

I haven't seen any of the links you posted, but AFAIK the correct place to put these commands is the application layer. The application service acts as a "translator" between external use cases (for ex., RegisterUserCommand) and your domain model. This way your domain model is kept "pure" and unaware of external clients.

Edit: just wanted to add, RegisterUserCommand seems like an standard user management context, which probably is not worth investing time for. If it's for learning purposes, sure, go for it, but in a normal application you should probably use third-party package to solve this case. It's important to be sensible about where to focus your efforts, as your (time/human) resources are limited.

0 points

We have a similar system (we also support different types of payment, including manually-validated bank accounts), so knowing that it works in your use-case gives me confidence in this approach. I'll study these changes and also try to add "fulfillment" to our vocabulary! Thanks for your help :)

1 points

Thanks for the input (and your time taken to answer). I'd like to add the expected flow of the process, in case it adds some light on what I want to achieve and why I "need" the SubscriptionRequest to never be created if it cannot be created on PayPal:

StoreOwner attempts to add a PayPal subscription -> A SubscriptionRequest is created locally and on PayPal -> StoreOwner is redirected to URL provided by PayPal (linked to newly created subscription request) -> SubscriptionRequest status is updated depending on response from PayPal -> If StoreOwner has validated the subscription, a Subscription is created

Thinking about your proposed approaches, I think the possibility of creating the SubscriptionRequest and then marking it as invalid if it cannot be validated on PayPal could be a solution. The problem is that right now I'm not exactly using a command rather than an "action" (don't know how to call it, I'm referring to a command that returns something), and I'm doing it synchronously. If I called a true command and expected it to be executed asynchronously, the client (the controller in this case) should perform queries until the newly created SubscriptionRequest is updated with PayPal data, and then redirect if successful or show an error message if it failed. Is that right?

Right now, if my "action" fails, it throws an exception that gives immediate feedback to the controller (it expects a redirection URL in order to immediately redirect the user). It makes the controller simpler, but forces me to add the PayPal calls inside a domain service (implemented in an adapter, PaypalSubscriptionService in this case). I don't have enough experience on these scenarios to fully understand the trade-offs...

1 points

In my case, if the subscription creation fails on Paypal (due to bad credentials, invalid parameters, etc.) I wouldn't want the subscription to be created. Would that work with what you're saying?

If I understand correctly, you're suggesting to modify the application service to work as following:

final class CommandHandler
{
    ...

    public function execute(RequestNewSubscriptionCommand $command)
    {
        $storeOwner = new StoreOwner();

        $subscriptionRequest = $storeOwner->requestNewSubscription();

        $this->paypalGateway->createSubscription($subscriptionRequest);
        $this->requestRepository->persist($subscriptionRequest);
    }
}
1 points

Which one do you prefer?

$versionA = ($x) ~> $x + $y;
$versionB = fn($x) => $x + $y;
$versionC = func($x) => $x + $y;
$versionD = function($x) => $x + $y;
0 points

I think they behaved slightly different, and they preferred to keep the syntax different so Hack could support both versions at the same time.

1 points

Having the following code:

private function constrainValueBetweenLimits(int $value, int $min, int $max): int
{
    return min(max($min, $value), $max));
}

Would you feel the need to add an explanation?

10 points

"Don't comment the _what_, comment the _why_."

The what should be extracted from the code itself. In this case, just put the code inside a method enforceRange (as others have said) and use whatever implementation is faster to write.

8 points

I'd probably create a factory to delegate the invoice creation from your domain model, at least from a testing point of view. I would be uncomfortable working with such a large handler, knowing that you have already stripped some lines from your example. Note: I prefer command handlers so delegate as much as possible, this is just a personal preference and not a rule.

Some other comments (take them with a grain of salt):

  • Invoice probably doesn't need whole User or Store instances, it probably just needs its ID. Pass only what you need. (If you need billing data, you should abstract that info out of these classes.)
  • Not having a customer, an author and a store might leave the invoice in an invalid state. If they are required, mark them as such in the Invoice constructor. (If they are not, ignore this.)
  • Currency could be extracted from each item's price, depending on what kind of usage you are giving to your invoices.
  • As others have said, you shouldn't be telling the invoice what's the total/subtotal, that's his job.
  • You should probably raise the event at the end of Invoice's constructor, but right now it would be meaningless as it lacks useful information. You could think about making the constructor ask for all required info, and create a builder to prepare everything in an easy way.
  • You should add docblocks to your properties to gain IDE autocompletion.
  • You should name your properties and variables to define what they are. Is $customers a Customer collection? No, it's a $customerRepository.
1 points

Hi! I've too been unable to find such information, in fact your package is still our best resource so far. I was thinking about downloading all postcodes from geonames.org and deriving validating regular expressions from that data, but as you say, the few seconds we gain from a simplified form exceed the effort required to do that.

At the moment we have discarded the idea of doing this validation and will require visitors to select a predefined subdivision for their country. Hope we can solve this in the future though!

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.