-
-
Notifications
You must be signed in to change notification settings - Fork 4
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 initial barcode validation and tests #2
Conversation
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.
Let me know what all changes you need to merge this PR.
Thank you.
src/Barcode.php
Outdated
@@ -0,0 +1,159 @@ | |||
<?php | |||
declare(strict_types=1); |
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.
I wonder why declare(strict_types=1);
is omitted on other classes. Is that purposeful ?
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.
We haven't yet decided if we will require that.
test/BarcodeTest.php
Outdated
// { | ||
// $this->expectException(InvalidArgumentException::class); | ||
// $this->expectExceptionMessage('not found'); | ||
// $barcode = new Barcode('\Zend\Validate\BarcodeTest\NonExistentClassName'); |
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.
In the old code this send an InvalidArgumentException before calling getAdapter()
. Do we want to do the same ? In that case the adapter is created first even if it may / may not is used.
test/BarcodeTest.php
Outdated
|
||
|
||
|
||
// public function testDefaultArrayConstructWithMissingAdapter() |
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.
I think we can remove these tests. What do you think ?
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.
Yes, as these ports should already the constructor to accept specific arguments, not an option array.
test/BarcodeTest.php
Outdated
$barcode = new Barcode('ean8'); | ||
$result = $barcode->validate('123'); | ||
$this->validateResult($result, false); | ||
// $message = $barcode->getMessages(); |
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.
$result->getMessages()
returns array of ValidationFailureMessage
. If we can keep the key as code
of ValidationFailureMessage
it may be easy to check this. May be there is another way to resolve this.
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.
The code is part of the validation failure messages.
@,hatikt - Thanks for starting this! However, per the RFC, this is one of the validators that will get a bridge component, due to the required dependency it had on zend-barcode. If you want to create that component, you can do so under your own user/org, and then open an issue here (or, once we've moved the component to the zendframework org, on that repo) letting us know, so we can bring it into the zendframework org. The new component would depend on this one and zend-barcode. |
Hi @weierophinney , Thank you for your quick reply. I am a bit confused for I didn't noticed any dependency on zend-barcode when writing a test for the Barcode validation component. I will look more into the discussion on zendframework/zend-validator#1 and https://discourse.zendframework.com/t/rfc-new-validation-component/208/2 . |
Okay, so it turns out I was wrong! In reviewing the various barcode-related validators, none of them have a dependency on zend-barcode. Additionally, it's not a suggested (or required!) dependency either. So... keep up the great work, and I'll review when you've finished! |
@weierophinney I have updated the code. I belive you can do a final review. |
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.
Thanks for the changes, @harikt !
I've got a list of new changes for you. Most importantly:
- The
Barcode
class should be immutable. As such, it should accept a concrete adapter via its constructor. - A number of adapters can benefit from some slight refactoring.
- The tests need a bit of work, and I've provided some notes on how to improve them.
Thanks for tackling this large set of validators!
src/Barcode.php
Outdated
|
||
private $useChecksum; | ||
|
||
public function __construct($adapter = null, bool $useChecksum = null) |
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.
Typehint this argument; only accept AdapterInterface
instances.
src/Barcode.php
Outdated
* | ||
* @return Barcode\AbstractAdapter | ||
*/ | ||
public function getAdapter() |
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.
Add a return typehint.
src/Barcode.php
Outdated
$adapter = 'Ean13'; | ||
} | ||
$this->setAdapter($adapter); | ||
} |
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.
The above becomes unnecessary if a concrete instance is provided via the constructor.
src/Barcode.php
Outdated
* @return Barcode | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function setAdapter($adapter) |
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.
Remove this method; validators should be immutable, with the exception of message templates.
src/Barcode.php
Outdated
|
||
if (! is_null($this->useChecksum)) { | ||
$adapter->useChecksum($this->useChecksum); | ||
} |
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.
Move these lines to the constructor.
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.
Hi @weierophinney ,
If we are passing the concrete adapter, do we really need to pass the $useChecksum
. There is no place where this variable need to be used. Can't we remove this ?
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.
If the main barcode validator doesn't and/or shouldn't override what's in the adapter, then we can remove it.
test/BarcodeTest.php
Outdated
|
||
public function testInvalidChecksumAdapter() | ||
{ | ||
require_once __DIR__ . "/_files/MyBarcode1.php"; |
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.
These require_once
calls can go away once you update the various test adapters to use the ZendTest
namespace. Doing so will require adding a new autoload-dev
rule to composer.json
so they may be autoloaded.
test/BarcodeTest.php
Outdated
* @dataProvider validationProvider | ||
*/ | ||
public function testValidateReturnsExpectedResults( | ||
$adapter, |
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.
Typehint this argument.
test/BarcodeTest.php
Outdated
public function testInvalidChecksumAdapter() | ||
{ | ||
require_once __DIR__ . "/_files/MyBarcode1.php"; | ||
$barcode = new Barcode('MyBarcode1'); |
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.
Here and throughout this file, you'll need to switch from using string "short" names to new <CLASSNAME>
.
test/BarcodeTest.php
Outdated
$this->expectExceptionMessage('does not implement'); | ||
require_once __DIR__ . "/_files/MyBarcode5.php"; | ||
$barcode->setAdapter('MyBarcode5'); | ||
} |
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.
This method can be removed after the constructor changes are made, and the setAdapter()
method is removed.
test/BarcodeTest.php
Outdated
$this->validateResult($barcode->validate('123'), true); | ||
$this->validateResult($barcode->validate('123a'), false); | ||
|
||
$barcode->useChecksum(true); |
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.
Once the change to useChecksum()
is made in the Barcode
class, you'll instead change the flag on the adapter within these tests.
Thank you for the detailed review @weierophinney . I will send an update in the coming days. |
Hi @weierophinney , Have updated code as requested and added documentation ( copied from old barcode validator , updated with the current ones ) Thank you. I will add the documentation |
src/Bitwise.php
Outdated
|
||
use Traversable; | ||
|
||
class Bitwise extends AbstractValidator |
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.
Oh shit, I committed this file accidentally 👎 . @weierophinney what do you want to do about this ?
Remove this file from this PR and send a different PR .
or add unit test / fix for this Bitwise one ?
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.
Remove it.
I've taken the liberty of rebasing your branch off of current master, and I did an interactive rebase to remove the Bitwise
class file from the commit where it was introduced.
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.
So do I want to do anything here ? I am a bit confused with the sentence.
I've taken the liberty of rebasing your branch off of current master, and I did an interactive rebase to remove the Bitwise class file from the commit where it was introduced.
So did you removed the Bitwise
class or do you want me to remove it ? or when you are doing the rebasing you will do ?
- Use 2018 for copyright date - Use `Result` named constructors - Use `TestAsset` namespace for barcode test classes
It looks like you have removed the Bitwise class. Is there anything else that makes it a blocker for not merging ? I got only one email, but this one still shows requested changes. Not sure what are the changes requested newly. I assume all changes have been already integrated earlier itself. Let me know if I have missed anything. |
Extracts checksum functionality to a new interface, `ChecksummableInterface`, which defines the various methods for determining if a checksum is in use, as well as how to invoke it. Most checksummable barcode validators now accept a boolean flag, `$useChecksum` to its constructor, defaulting it to whatever the default behavior should be (generally `true`, indicating checksum validation should occur). Some override `useChecksum()` to determine if the value requires a checksum validation, usually based on length or an initial or final character in the barcode. **ALL** barcode validators now have only constructor injection for defining behavior. This ensures they are always in a consistent state. Finally, all barcode validators now have strict_types enabled, which caught a number of errors in how the various checksum operations were being calculated and compared.
Extracts a trait from each of Code39 and Code93 for the purposes of validating the checksum, and then updates the `ext` variants to allow checksum verification.
- Puts usage docs at top - Ensures each class documents any constructor arguments present - Updates examples to match code usage
Using `ZendTest\DataValidator\Barcode\TestAsset` namespace for custom barcode validators, which does not require an additional rule.
- Removes unnecessary result assignments. - Combines conditionals - Extracts the method `getAllowedLength()`, and simplifies the logic (as it was doing the same work as `implode()`)
Removes redundant "barcode" verbiage.
@harikt Sorry for the delay getting back to you! I'd started on some refactoring, but then had other obligations last week that prevented me from finishing, as well as providing you details of what I was doing. What I've done, in general, is as follows:
I've also updated the documentation to list out any constructor arguments, and to push the usage example to the top of the page. Thanks a ton for your work on this! |
No issues. The problem with too much delay is we all will be carried away to other stuffs and forget about what we did and why we did ( Probably a failure at my end to document things ) . I always forget in 2-3 days and find it hard to get back to it. This is not a complaint. Just telling about my personal thing. And thank you for your good work here. Any plans about release date etc ? |
Hi @weierophinney ,
This PR contains Barcode validation .
Some of the tests are commented out which need to be addressed. I am still thinking how we can resolve those.
CHANGELOG.md
entry for the new feature.