Browse Source

Documentation fix. More unit tests added.

tags/1.1.0
Alban Jubert 9 years ago
parent
commit
75450aa185
  1. 37
      README.md
  2. 53
      tests/SaveRelationsBehaviorTest.php

37
README.md

@ -131,9 +131,42 @@ Validation
----------
Every declared related models will be validated prior to be saved. If any validation fails, for each related model attribute in error, an error associated with the named relation will be added to the owner model.
For `hasMany()` relations, the index of the related model will be used to identifiy the associated error message.
For `hasMany()` relations, the index of the related model will be used to identify the associated error message.
> **Tips :**
> For relations not involving a junction table by using the `via()` or `viaTable()` methods, you should remove the attributes pointing to the owner model to be able to pass the validations.
> For relations not involving a junction table by using the `via()` or `viaTable()` methods, you should remove the attributes pointing to the owner model from the 'required' validation rules to be able to pass the validations.
Populate the model and its relations with input data
----------------------------------------------------
This behavior adds a convenient method to load relations models attributes in the same way that the load() method does.
Simply call the `loadRelations()` with the according input data.
For instance:
```php
$project = Project::findOne(1);
/**
* $_POST could be something like:
* [
* 'Company' => [
* 'name' => 'YiiSoft'
* ],
* 'ProjectLink' => [
* [
* 'language' => 'en',
* 'name' => 'yii',
* 'link' => 'http://www.yiiframework.com'
* ],
* [
* 'language' => 'fr',
* 'name' => 'yii',
* 'link' => 'http://www.yiiframework.fr'
* ]
* ]
* ];
*/
$project->loadRelations(Yii::$app->request->post());
```
You can even further simplify the process by adding the `SaveRelationsTrait` to your model.
In that case, a call to the `load()` method will also automatically trigger a call to the `loadRelations()` method by using the same data, so you basically won't have to change your controllers.

53
tests/SaveRelationsBehaviorTest.php

@ -8,6 +8,7 @@ use SebastianBergmann\GlobalState\RuntimeException;
use tests\models\Company;
use tests\models\Link;
use tests\models\Project;
use tests\models\ProjectNoTransactions;
use tests\models\User;
use Yii;
use yii\base\Model;
@ -205,8 +206,27 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$project->company = $company;
$this->assertTrue($company->isNewRecord, 'Company should be a new record');
$this->assertFalse($project->save(), 'Project could be saved');
$this->assertArrayHasKey('company', $project->getErrors(),
'Validation errors do not contain a message for company');
$this->assertArrayHasKey(
'company',
$project->getErrors(),
'Validation errors do not contain a message for company'
);
$this->assertEquals('Company: Name cannot be blank.', $project->getFirstError('company'));
}
public function testSaveInvalidModelWithNoTransactionsSetShouldFail()
{
$project = new ProjectNoTransactions();
$project->name = "Java";
$company = new Company();
$project->company = $company;
$this->assertTrue($company->isNewRecord, 'Company should be a new record');
$this->assertFalse($project->save(), 'Project could be saved');
$this->assertArrayHasKey(
'company',
$project->getErrors(),
'Validation errors do not contain a message for company'
);
$this->assertEquals('Company: Name cannot be blank.', $project->getFirstError('company'));
}
@ -290,8 +310,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(3, $project->links, 'Project should have 3 links after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertCount(3, $project->links, 'Project should have 3 links after save');
$this->assertEquals("https://www.microsoft.com/fr-fr/windows/features", $project->links[2]->link,
'Second link should be https://www.microsoft.com/fr-fr/windows/features');
$this->assertEquals(
"https://www.microsoft.com/fr-fr/windows/features",
$project->links[2]->link,
'Second link should be https://www.microsoft.com/fr-fr/windows/features'
);
}
public function testSaveNewHasManyRelationWithCompositeFksAsArrayShouldSucceed()
@ -306,10 +329,16 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(4, $project->links, 'Project should have 4 links after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertCount(4, $project->links, 'Project should have 4 links after save');
$this->assertEquals("https://www.microsoft.com/fr-fr/windows/features", $project->links[2]->link,
'Second link should be https://www.microsoft.com/fr-fr/windows/features');
$this->assertEquals("https://www.microsoft.com/en-us/windows/features", $project->links[3]->link,
'Third link should be https://www.microsoft.com/en-us/windows/features');
$this->assertEquals(
"https://www.microsoft.com/fr-fr/windows/features",
$project->links[2]->link,
'Second link should be https://www.microsoft.com/fr-fr/windows/features'
);
$this->assertEquals(
"https://www.microsoft.com/en-us/windows/features",
$project->links[3]->link,
'Third link should be https://www.microsoft.com/en-us/windows/features'
);
}
public function testSaveUpdatedHasManyRelationWithCompositeFksAsArrayShouldSucceed()
@ -321,8 +350,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$project->links = $links;
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertCount(2, $project->links, 'Project should have 2 links before save');
$this->assertEquals("http://www.otherlink.com/", $project->links[1]->link,
'Second link "Link" attribute should be "http://www.otherlink.com/"');
$this->assertEquals(
"http://www.otherlink.com/",
$project->links[1]->link,
'Second link "Link" attribute should be "http://www.otherlink.com/"'
);
}
public function testSaveMixedRelationsShouldSucceed()
@ -387,5 +419,4 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(2, $project->projectLinks, "Project should have 2 links");
}
}

Loading…
Cancel
Save