-
Notifications
You must be signed in to change notification settings - Fork 13
Fix for issue #35. Corrected ActiveRecordTest's so that capitalizatio… #36
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
Conversation
…at capitalization for attribute names are retained. So the short form for New York is now NY instead of ny. test/model/Author, Book, and Venue now have those strtoupper() workarounds in getter/setter methods removed as it's done in Model.php
…rcased in Model.php in order to be case insensitive.
@@ -349,13 +349,12 @@ public function __construct(array $attributes=[], $guard_attributes=true, $insta | |||
public function &__get($name) | |||
{ | |||
// check for getter | |||
$name = strtolower($name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be something like:
$methodName = "get_" . strtolower($name) ;
if (method_exists($methodName) {
$retValue = $this->$name();
return $retValue;
...that way you don't have to worry about stuff breaking if you make it down to line 358
, right? (maybe we should add tests around non-lowercase attribute names?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, fun TODO: make the casing configurable (eg. I use camelCase in my own codebase, not snake_case).
Edit: when I say TODO I mean for some far-off day, not this PR!
…at capitalization for attribute names are retained. So the short form for New York is now NY instead of ny. test/model/Author, Book, and Venue now have those strtoupper() workarounds in getter/setter methods removed as it's done in Model.php
…rcased in Model.php in order to be case insensitive.
# Conflicts: # lib/Model.php
Not sure what happened, but can you see if you can rebase so only your changes remain in this PR? |
Yep, much better, but could you address the comment I left? |
I thought I did about method_exists(). If it's about camel case vs snake case configuration, I think what you're asking would lead to a bug. SQL distinguishes between attributeName and attribute_name as two different columns in a table; it's only the case that is insensitive, not the camel vs snake spelling. Collapsing them into one column though a configuration would prevent the use of ActiveRecord in the case where a table has attributeName and attribute_name as columns. Not that I would recommend anyone designing a table that way, but if they did, it should be treated as two columns, not one through a configuration. |
Ah, no, actually I was just talking about the style of the magic set methods. In my codebase I use camelCase for everything, but because ActiveRecord assumes snake_case, I have this jarring mix of styles on my methods: class Animal extends Model {
function forageForFood() {
// do stuff
}
public function set_name(string $name): void
{
$this->assign_attribute('name', $name);
$this->forageForFood();
}
} But that whole concept may or may not make sense, and is a topic for another day. The comment I was referring to was this: |
As the comment above, $this->$name() should be calling the lowercase version of $name as $methodName is lowercase. My original code does this and passes regression. The PR allows you to do $model->attrName or $model->attrname or $model->ATTRNAME. Getting and setting attributes are no longer case sensitive, just like the generated SQL. |
So, I'm still trying to wrap my head around what it is you're trying to fix, here, but my first thought is that you may be misunderstanding what's happening in some of the tests. Let's start with this one: public function test_getter()
{
$book = Book::first();
$this->assertEquals(strtoupper($book->name), $book->upper_name);
} This isn't a workaround for anything. What it's doing is confirming that the custom getter is working, specifically this one: // Book.php
public function get_upper_name()
{
return strtoupper($this->name);
} Is that clear? We're just testing that the custom getter works. The uppercase thing is just a way to transform the value in a way that's easy to verify in our test. We could have just as easily reversed the string, or hashed it, or whatever. It has nothing to do with the internal handling of casing of column names. All that said, that test is sort of breaking one of the laws of testing, which is not to do any business logic in the test, so it should probably be rewritten as: public function test_getter()
{
$book = Book::first();
$this->assertEquals("THE ANCIENT ART OF MAIN TANKING", $book->upper_name);
} |
Fixed here: |
- Correct link to contributing.md
Okay, let me see if I can summarize the situation, here:
$author = Author::find(1);
$authorId = $author->Author_Id; // exception ...then it kerplodes. Do I have that right? |
Yes. And for those of use using camel case, $model->longvariablenamewithoutcamelcaseishardtoread == true! |
Okay, then let's proceed, but can you undo the changes to the singular/array/null tests? If you want to keep those tests, then please move them to your |
Yes, I can do that, but need clarity as to which is the minimum set of tests you want to keep. Do you want any of the tests in ActiveRecordFindTest.php? They are more documentation purposes than anything else, and perhaps it should just be stated that attribute names are case insensitive in the documentation. Do you want any tests outside of test_case_insensitivity()? None of the rest are necessary. |
Let's start by keeping the ones in |
…vity() is enough.
Ok, reverted. Fixed readme.md to have lists in alphabetical order and for contributing.md to point to the the right file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Fix for issue #35. Corrected ActiveRecordTest's so that capitalization for attribute names are retained. So the short form for New York is now NY instead of ny. test/model/Author, Book, and Venue now have those strtoupper() workarounds in getter/setter methods removed as it's done in Model.php