-
Notifications
You must be signed in to change notification settings - Fork 130
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
Problem running test on models using phpUnit #12
Comments
Can you provide the test class including the related test method? |
class PhoneVarModelTest extends TestCase { public function testInsert() Function testInsert fails with the error I reported. This is the model class that I am testing /** /** /** /** /**
David Suna |
I haven't completely figured this out myself, but try doing as suggested in this issue: class PhoneVarModelTest extends PHPUnit_Framework_TestCase {
// ...
} Let me know if it helped |
Unfortunately this didn't help. On 4/2/2014 2:26 PM, Felix Kiss wrote:
David Suna |
I tried to create a similar scenario to yours (ardent + uniquewith-validator) in a fresh copy of laravel. It works. What are the differences between my proof of concept and your application? |
Laravel version is v4.1.24 Here is my composer.json David Suna |
I think I found the problem. I had implemented some of my own custom validations and that caused the validation methods defined in your package to not get loaded. I am not exactly sure why but that seems to be what happened. Rather than try to figure out how to resolve the conflict I just implemented my own unique_with type of validation. |
I'd like to reopen this Issue. As i seem to get the same error. I'm now trying to fix it somehow, or i'm just mistaken and diggin' in the wrong place :| |
Do you use a BaseModel class for shared functionality? It should be something like this class BaseModel extends Ardent
{
/**
* When given an ID and a Laravel validation rules array, this function
* appends the ID to the 'unique' rules given. The resulting array can
* then be fed to a Ardent save so that unchanged values
* don't flag a validation issue. Rules can be in either strings
* with pipes or arrays, but the returned rules are in arrays.
*
* @param int $id
* @param array $rules
*
* @return array Rules with exclusions applied
*/
protected function buildUniqueExclusionRules(array $rules = array()) {
if (!count($rules))
$rules = static::$rules;
foreach ($rules as $field => &$ruleset) {
// If $ruleset is a pipe-separated string, switch it to array
$ruleset = (is_string($ruleset))? explode('|', $ruleset) : $ruleset;
foreach ($ruleset as &$rule) {
if (strpos($rule, 'unique') === 0) {
$params = explode(',', $rule);
$uniqueRules = array();
// Append table name if needed
$table = explode(':', $params[0]);
$ruleName = $table[0]; // Updated
if (count($table) == 1)
$uniqueRules[1] = $this->table;
else
$uniqueRules[1] = $table[1];
// Append field name if needed
if (count($params) == 1)
$uniqueRules[2] = $field;
else
$uniqueRules[2] = $params[1];
if (isset($this->primaryKey)) {
$uniqueRules[3] = $this->{$this->primaryKey};
$uniqueRules[4] = $this->primaryKey;
}
else {
$uniqueRules[3] = $this->id;
}
$rule = $ruleName . ':' . implode(',', $uniqueRules); // Updated
} // end if strpos unique
} // end foreach ruleset
}
return $rules;
}
} |
okay, apparently i was misusing part of the thing. I thought it would act as INSERT on DUPLICATE KEY UPDATE. i.e. ovewritten on save... but It seems that at first you override a findOrNew or findOrCreate with ability to find by multiple UNIQUE columns+values. and only Then you try to updateUniques() (which replaces the "unique_with" with "unique" and a proper primaryKey and it's value), and THEN it works as expected. |
@felixkiss yes i was trying to override this method, but it seems that it has a different purpose :| if you wish, we can continue with this issue (Ardent and unique_with) as it seems to not support multi-column primary keys at all. i.e. in the code shown, multy-column primary key will fail,.. hard :) |
From my understanding, Ardent tries to append the id to the
Anyways, make sure you test your code with some values that would violate the multiple UNIQUE declaration of your SQL schema to make sure it does what you expect it to. Please close the issue whenever you feel it is resolved for you. Cheers |
Seems like a resolved issue to me. Reopen if more help is needed. |
I am using the uniquewith validation without a problem in my application. I am trying to write some tests now. However, when I try to run a test in PHPUnit it gives me the following error:
BadMethodCallException: Method [validateUniqueWith] does not exist
What do I need to do in order for this to work in my unit tests?
Thanks
The text was updated successfully, but these errors were encountered: