Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.80 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Tests\Feature;
  4.  
  5. use App\Collection;
  6. use App\User;
  7. use Tests\TestCase;
  8. use Illuminate\Foundation\Testing\DatabaseMigrations;
  9.  
  10. class CollectionAuthorizedTest extends TestCase
  11. {
  12.     use DatabaseMigrations;
  13.  
  14.     /**
  15.      * Test Setup will create a user, login the user before all the tests
  16.      *
  17.      */
  18.  
  19.     public function setUp()
  20.     {
  21.         parent::setUp();
  22.         factory(User::class)->create([
  23.             'username' => 'TestingAuthCollection',
  24.             'email' => 'testingauthcollection@testing.com',
  25.             'password' => bcrypt('testauthcollection')
  26.         ]);
  27.  
  28.         $this->followingRedirects()
  29.             ->post('/login', ['email' => 'testingauthcollection@testing.com', 'password' => 'testauthcollection']);
  30.     }
  31.  
  32.     /**
  33.      * Test tear down will remove the user from the database
  34.      *
  35.      */
  36.     public function tearDown()
  37.     {
  38.         $userdelete = User::where('username', 'TestingAuthCollection')->first();
  39.         $userdelete->delete();
  40.  
  41.         parent::tearDown();
  42.     }
  43.  
  44.     /**
  45.      *
  46.      * Test Collection Visible
  47.      *
  48.      * This test will create 5 new collections
  49.      * with 5 new users each with a unique owner_id
  50.      * Once the user logs in it will see the name
  51.      * of all the collections (all 5).
  52.      *
  53.      */
  54.  
  55.     public function testAllCollectionsVisible()
  56.     {
  57.         $collections = factory('App\Collection', 5)->create();
  58.  
  59.         foreach ($collections as $collection) {
  60.             $user = User::where('id', $collection->owner_id)->first();
  61.             $this->followingRedirects()
  62.                 ->actingAs($user)
  63.                 ->post(route('login'), ['email' => $user['email'], 'password' => 'secret'])
  64.                 ->assertStatus(200)
  65.                 ->assertSee(e($collection->name));
  66.         }
  67.     }
  68.  
  69.     /**
  70.      *
  71.      * Test Create Collection
  72.      *
  73.      * This test will create a new user and perform
  74.      * a post /login and also a post /collections
  75.      * with name of collection as 'testingcreate' and description as 'testcreate phpunit'
  76.      * The test should see the name of the collection.
  77.      *
  78.      */
  79.  
  80.     public function testCreateCollection()
  81.     {
  82.         $this->followingRedirects()
  83.             ->post('/collections', ['name' => 'testingauthcreate', 'description' => 'testauthcreate phpunit'])
  84.             ->assertSee('testingauthcreate');
  85.     }
  86.  
  87.     /**
  88.      *
  89.      * Test View Collection
  90.      *
  91.      * This test create a new user and perform
  92.      * a post /login and also a post /collections
  93.      * with name of collection as 'testingview' and description as 'testview phpunit'
  94.      * and it will collect information from the database with the name 'testingview'
  95.      * and perform a get /collection/$collection_id->id and it should see 'testview phpunit'
  96.      *
  97.      */
  98.  
  99.     public function testViewCollection()
  100.     {
  101.         $this->followingRedirects()
  102.             ->post('/collections', ['name' => 'testingauthview', 'description' => 'testauthview phpunit'])
  103.             ->assertSee('testingauthview');
  104.  
  105.         $testuser = User::where('username' , 'TestingAuthCollection')->first();
  106.  
  107.         $collection_id = Collection::where('name', 'testingauthview' )->where('owner_id', $testuser->id)->first();
  108.         $this->followingRedirects()
  109.             ->get('/collections/' . $collection_id->id)
  110.             ->assertSee('testingauthview')
  111.             ->assertSee('testauthview phpunit');
  112.     }
  113.  
  114.     /**
  115.      *
  116.      * Test Update Collection
  117.      *
  118.      * This test create a new user and perform
  119.      * a post /login and also a post /collections
  120.      * with name of collection as 'testingupdate' and description as 'testupdate phpunit'
  121.      * and it will collect information from the database with the name 'testingupdate'
  122.      * and perform a put /collection/$collection_id->id to make changes to see
  123.      * name 'testingupdate1' and description 'testupdate phpunit1'
  124.      *
  125.      */
  126.  
  127.     public function testUpdateCollection()
  128.     {
  129.         $this->followingRedirects()
  130.             ->post('/collections', ['name' => 'testingauthupdate', 'description' => 'testauthupdate phpunit']);
  131.  
  132.         $testuser = User::where('username' , 'TestingAuthCollection')->first();
  133.         $collection = Collection::where('name', 'testingauthupdate')->where('owner_id', $testuser->id)->first();
  134.  
  135.         $this->followingRedirects()
  136.             ->put('/collections/' . $collection['id'], ['name' => 'testingauthupdate1', 'description' => 'testauthupdate phpunit1'])
  137.             ->assertSee('testingauthupdate1')
  138.             ->assertSee('testauthupdate phpunit1');
  139.     }
  140.  
  141.     /**
  142.      *
  143.      * Test Edit Collection
  144.      *
  145.      * this is testing if the collection is successfully
  146.      * a post /login and also a post /collections
  147.      * with name of collection as 'testingedit' and description as 'testedit phpunit'
  148.      * and it will collect information from the database with the name 'testingedit'
  149.      * and check if the data in the collection has been edited which is for for displaying
  150.      * a form to apply changes via get method.
  151.      *
  152.      */
  153.  
  154.     public function testEditCollection()
  155.     {
  156.         $this->followingRedirects()
  157.             ->post('/collections', ['name' => 'testingauthedit', 'description' => 'testauthedit phpunit'])
  158.             ->assertSee('testingauthedit');
  159.  
  160.         $testuser = User::where('username' , 'TestingAuthCollection')->first();
  161.  
  162.         $collection_id = Collection::where('name', 'testingauthedit' )->where('owner_id', $testuser->id)->first();
  163.         $this->followingRedirects()
  164.             ->get('/collections/' . $collection_id->id .'/edit')
  165.             ->assertSee($collection_id->name)
  166.             ->assertSee($collection_id->description);
  167.     }
  168.  
  169.     /**
  170.      *
  171.      * Test Delete Collection
  172.      *
  173.      * This test create a new user and perform
  174.      * a post /login and also a post /collections
  175.      * with name of collection as 'testingdelete' and description as 'testdelete phpunit'
  176.      * and it will collect information from the database with the name 'testingdelete'
  177.      * and perform a delete /collection/$collection_id->id to delete the collection
  178.      * and the name of the collection should not be visible anymore.
  179.      *
  180.      */
  181.  
  182.     public function testDeleteCollection()
  183.     {
  184.         $this->followingRedirects()
  185.             ->post('/collections', ['name' => 'testingauthdelete', 'description' => 'testauthdelete phpunit'])
  186.             ->assertSee('testingauthdelete');
  187.  
  188.         $testuser = User::where('username' , 'TestingAuthCollection')->first();
  189.  
  190.         $collection_id = Collection::where('name', 'testingauthdelete')->where('owner_id', $testuser->id)->first();
  191.         $this->followingRedirects()
  192.             ->DELETE('/collections/' . $collection_id->id)
  193.             ->assertDontSee('testingauthdelete');
  194.     }
  195.  
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement