| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/vendor/dimsav/laravel-translatable/tests/ |
Upload File : |
<?php
use Dimsav\Translatable\Test\Model;
use Dimsav\Translatable\Test\Model\City;
use Dimsav\Translatable\Test\Model\Company;
use Dimsav\Translatable\Test\Model\Country;
use Dimsav\Translatable\Test\Model\Continent;
use Dimsav\Translatable\Test\Model\Vegetable;
use Dimsav\Translatable\Test\Model\CountryStrict;
use Dimsav\Translatable\Test\Model\CountryGuarded;
use Dimsav\Translatable\Test\Model\CityTranslation;
use Dimsav\Translatable\Test\Model\CountryTranslation;
class TestCoreModelExtension extends TestsBase
{
// Saving
public function test_it_saves_empty_instances()
{
$company = new Company();
$company->save();
$this->assertGreaterThan(0, $company->id);
$country = new Continent();
$country->save();
$this->assertGreaterThan(0, $country->id);
}
public function test_it_saves_translations_when_existing_and_dirty()
{
$country = Country::find(1);
$country->code = 'make_model_dirty';
$country->name = 'abc';
$this->assertTrue($country->save());
$country = Country::find(1);
$this->assertEquals($country->name, 'abc');
}
// Failing saving
/**
* @expectedException \Exception
*/
public function test_it_throws_query_exception_if_code_is_null()
{
$country = new Country();
$country->name = 'Belgium';
$country->code = null;
$country->save();
}
/**
* @expectedException \Exception
*/
public function test_it_throws_query_exception_if_saving_and_name_is_null()
{
$country = new Country();
$country->code = 'be';
$country->name = null;
$country->save();
}
public function test_it_returns_false_if_exists_and_dirty_and_parent_save_returns_false()
{
$that = $this;
$event = App::make('events');
$event->listen('eloquent*', function ($event, $models) use ($that) {
return get_class(reset($models)) == 'Dimsav\Translatable\Test\Model\Country' ? false : true;
});
$country = Country::find(1);
$country->code = 'make_model_dirty';
$country->name = 'abc';
$this->assertFalse($country->save());
}
public function test_it_returns_false_if_does_not_exist_and_parent_save_returns_false()
{
$that = $this;
$event = App::make('events');
$event->listen('eloquent*', function ($event, $models) use ($that) {
return get_class(reset($models)) == 'Dimsav\Translatable\Test\Model\Continent' ? false : true;
});
$continent = new Continent();
$this->assertFalse($continent->save());
}
// Filling
/**
* @expectedException Illuminate\Database\Eloquent\MassAssignmentException
*/
public function test_it_throws_exception_if_filling_a_protected_property()
{
$country = new CountryGuarded();
$this->assertTrue($country->totallyGuarded());
$country->fill(['code' => 'it', 'en' => ['name' => 'Italy']]);
}
/**
* @expectedException Illuminate\Database\Eloquent\MassAssignmentException
*/
public function test_translation_throws_exception_if_filling_a_protected_property()
{
$country = new Country();
$country->translationModel = Model\CountryTranslationGuarded::class;
$country->fill(['code' => 'it', 'en' => ['name' => 'Italy']]);
}
// Deleting
public function test_it_deletes_translations()
{
$city = City::find(1);
$cityId = $city->id;
$translation = $city->translate('en');
$this->assertTrue(is_object($translation));
$city->delete();
$city = City::find($cityId);
$this->assertNull($city);
$translations = CityTranslation::where('city_id', '=', $cityId)->get();
$this->assertEquals(0, count($translations));
}
public function test_it_does_not_delete_translations_when_attempting_to_delete_translatable()
{
$country = Country::find(1);
$countryId = $country->id;
$translation = $country->translate('en');
$this->assertTrue(is_object($translation));
try {
$country->delete();
} catch (\Exception $e) {
}
$country = Country::find(1);
$this->assertNotNull($country);
$translations = CountryTranslation::where('country_id', '=', $countryId)->get();
$this->assertEquals(4, count($translations));
}
public function test_it_does_not_delete_translations_while_force_deleting()
{
$country = CountryStrict::find(2);
$country->forceDelete();
$after = CountryTranslation::where('country_id', '=', 2)->get();
$this->assertEquals(0, count($after));
}
public function test_to_array_returns_translated_attributes()
{
$country = Country::find(1);
$this->assertArrayHasKey('name', $country->toArray());
$this->assertArrayHasKey('code', $country->toArray());
}
public function test_to_array_wont_break_if_no_translations_exist()
{
$country = new Country(['code' => 'test']);
$country->save();
$country->toArray();
}
// Forms
public function test_it_fakes_isset_for_translated_attributes()
{
$country = Country::find(1);
$this->assertEquals(true, isset($country->name));
}
// Hidden attributes
public function test_it_should_hide_attributes_after_to_array()
{
$country = Country::find(1);
$this->assertEquals(true, isset($country->toArray()['name']));
// it is equivalent to set
// protected $hidden = ['name'];
// in Eloquent
$country->setHidden(['name']);
$this->assertEquals(false, isset($country->toArray()['name']));
}
public function test_it_finds_custom_primary_keys()
{
$vegetable = new Vegetable;
$this->assertSame('vegetable_identity', $vegetable->getRelationKey());
}
public function test_setAttribute_returns_this()
{
$country = new Country;
$this->assertSame($country, $country->setAttribute('code', 'ch'));
$this->assertSame($country, $country->setAttribute('name', 'China'));
}
}