From e416aef6188eb56e2e009ed2a34d1447588e1b8a Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Sat, 23 Nov 2019 23:05:38 +0100 Subject: [PATCH] add codeception and example tests --- .env.testing | 45 +++++++ codeception.yml | 10 ++ composer.json | 9 +- tests/Browser/ExampleTest.php | 32 ----- tests/Browser/Pages/HomePage.php | 41 ------ tests/Browser/Pages/Page.php | 20 --- tests/Browser/ProductCategoryTest.php | 49 ------- tests/Browser/console/.gitignore | 2 - tests/Browser/screenshots/.gitignore | 2 - tests/CreatesApplication.php | 22 ---- tests/DuskTestCase.php | 48 ------- tests/Feature/Auth/AuthTest.php | 124 ------------------ tests/Feature/GeneralTest.php | 88 ------------- tests/TestCase.php | 10 -- tests/Unit/ExampleTest.php | 19 --- tests/_data/.gitkeep | 0 tests/_output/.gitignore | 2 + tests/_support/AcceptanceTester.php | 26 ++++ tests/_support/FunctionalTester.php | 52 ++++++++ tests/_support/Helper/Acceptance.php | 10 ++ tests/_support/Helper/Functional.php | 10 ++ tests/_support/Helper/Unit.php | 10 ++ tests/_support/UnitTester.php | 26 ++++ tests/_support/_generated/.gitignore | 2 + tests/acceptance.suite.yml | 13 ++ tests/functional.suite.yml | 20 +++ .../functional/Shop/ProductControllerCest.php | 70 ++++++++++ tests/unit.suite.yml | 17 +++ .../unit/Product/Helpers/ProductTypeCest.php | 15 +++ 29 files changed, 336 insertions(+), 458 deletions(-) create mode 100644 .env.testing create mode 100644 codeception.yml delete mode 100644 tests/Browser/ExampleTest.php delete mode 100644 tests/Browser/Pages/HomePage.php delete mode 100644 tests/Browser/Pages/Page.php delete mode 100644 tests/Browser/ProductCategoryTest.php delete mode 100644 tests/Browser/console/.gitignore delete mode 100644 tests/Browser/screenshots/.gitignore delete mode 100755 tests/CreatesApplication.php delete mode 100644 tests/DuskTestCase.php delete mode 100644 tests/Feature/Auth/AuthTest.php delete mode 100644 tests/Feature/GeneralTest.php delete mode 100755 tests/TestCase.php delete mode 100755 tests/Unit/ExampleTest.php create mode 100644 tests/_data/.gitkeep create mode 100644 tests/_output/.gitignore create mode 100644 tests/_support/AcceptanceTester.php create mode 100644 tests/_support/FunctionalTester.php create mode 100644 tests/_support/Helper/Acceptance.php create mode 100644 tests/_support/Helper/Functional.php create mode 100644 tests/_support/Helper/Unit.php create mode 100644 tests/_support/UnitTester.php create mode 100644 tests/_support/_generated/.gitignore create mode 100644 tests/acceptance.suite.yml create mode 100644 tests/functional.suite.yml create mode 100644 tests/functional/Shop/ProductControllerCest.php create mode 100644 tests/unit.suite.yml create mode 100644 tests/unit/Product/Helpers/ProductTypeCest.php diff --git a/.env.testing b/.env.testing new file mode 100644 index 000000000..6b1104008 --- /dev/null +++ b/.env.testing @@ -0,0 +1,45 @@ +APP_NAME=Bagisto +APP_ENV=local +APP_VERSION=0.1.8 +APP_KEY=base64:NFtGjjFAqET6RlX3PVC/gFpzHb4jK1OxDc3cuU5Asz4= +APP_DEBUG=true +APP_URL=http://localhost + +LOG_CHANNEL=stack + +DB_CONNECTION=mysql +DB_HOST=mysql +DB_PORT=3306 +DB_DATABASE=bagisto_test +DB_USERNAME=root +DB_PASSWORD=root + +BROADCAST_DRIVER=log +CACHE_DRIVER=file +SESSION_DRIVER=file +SESSION_LIFETIME=20 +QUEUE_DRIVER=sync + +REDIS_HOST=127.0.0.1 +REDIS_PASSWORD=null +REDIS_PORT=6379 + +MAIL_DRIVER=smtp +MAIL_HOST=smtp.mailtrap.io +MAIL_PORT=2525 +MAIL_USERNAME= +MAIL_PASSWORD= +MAIL_ENCRYPTION=tls + +SHOP_MAIL_FROM= +ADMIN_MAIL_TO= + +fixer_api_key= + +PUSHER_APP_ID= +PUSHER_APP_KEY= +PUSHER_APP_SECRET= +PUSHER_APP_CLUSTER=mt1 + +MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" +MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 000000000..f0446cc8a --- /dev/null +++ b/codeception.yml @@ -0,0 +1,10 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +actor_suffix: Tester +extensions: + enabled: + - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json index 03d5e3e62..7df20f637 100755 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "tymon/jwt-auth": "1.0.0-rc.4" }, "require-dev": { + "codeception/codeception": "3.1.*", "barryvdh/laravel-debugbar": "^3.1", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", @@ -116,6 +117,11 @@ "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover" + ], + "test": [ + "set -e", + "vendor/bin/codecept run unit", + "vendor/bin/codecept run functional" ] }, "config": { @@ -123,5 +129,6 @@ "sort-packages": true, "optimize-autoloader": true }, - "minimum-stability": "dev" + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/tests/Browser/ExampleTest.php b/tests/Browser/ExampleTest.php deleted file mode 100644 index 8aa739cfe..000000000 --- a/tests/Browser/ExampleTest.php +++ /dev/null @@ -1,32 +0,0 @@ -all(); - - $customer = $customer->first(); - - $this->browse(function (Browser $browser) use($customer) { - $browser->visit('/customer/login') - ->type('email', $customer->email) - ->type('password', $customer->password) - ->click('input[type="submit"]') - ->screenshot('error'); - }); - } -} diff --git a/tests/Browser/Pages/HomePage.php b/tests/Browser/Pages/HomePage.php deleted file mode 100644 index 4f5a87f4c..000000000 --- a/tests/Browser/Pages/HomePage.php +++ /dev/null @@ -1,41 +0,0 @@ - '#selector', - ]; - } -} diff --git a/tests/Browser/Pages/Page.php b/tests/Browser/Pages/Page.php deleted file mode 100644 index f8d76222c..000000000 --- a/tests/Browser/Pages/Page.php +++ /dev/null @@ -1,20 +0,0 @@ - '#selector', - ]; - } -} diff --git a/tests/Browser/ProductCategoryTest.php b/tests/Browser/ProductCategoryTest.php deleted file mode 100644 index 4c8b2658b..000000000 --- a/tests/Browser/ProductCategoryTest.php +++ /dev/null @@ -1,49 +0,0 @@ -all(); - $products = app('Webkul\Product\Repositories\ProductRepository')->all(); - - $slugs = array(); - - foreach ($categories as $category) { - if ($category->slug != 'root') { - array_push($slugs, $category->slug); - } - } - - $slugIndex = array_rand($slugs); - $testSlug = $slugs[$slugIndex]; - $testProduct = array(); - - foreach ($products as $product) { - $categories = $product->categories; - - if ($categories->last()->slug == $testSlug) { - array_push($testProduct, ['name' => $product->name, 'url_key' => $product->url_key]); - - break; - } - } - - $this->browse(function (Browser $browser) use($testSlug, $testProduct) { - $browser->visit(route('shop.categories.index', $testSlug)); - $browser->assertSeeLink($testProduct[0]['name']); - $browser->pause(5000); - }); - } -} \ No newline at end of file diff --git a/tests/Browser/console/.gitignore b/tests/Browser/console/.gitignore deleted file mode 100644 index d6b7ef32c..000000000 --- a/tests/Browser/console/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/Browser/screenshots/.gitignore b/tests/Browser/screenshots/.gitignore deleted file mode 100644 index d6b7ef32c..000000000 --- a/tests/Browser/screenshots/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php deleted file mode 100755 index 8d8a77e99..000000000 --- a/tests/CreatesApplication.php +++ /dev/null @@ -1,22 +0,0 @@ -make(Kernel::class)->bootstrap(); - - return $app; - } -} \ No newline at end of file diff --git a/tests/DuskTestCase.php b/tests/DuskTestCase.php deleted file mode 100644 index 321941f5d..000000000 --- a/tests/DuskTestCase.php +++ /dev/null @@ -1,48 +0,0 @@ -addArguments([ - '--disable-gpu', - '--headless', - '--no-sandbox' - ]); - - return RemoteWebDriver::create( - 'http://localhost:9515/', DesiredCapabilities::chrome() - ); - - // return RemoteWebDriver::create( - // 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability( - // ChromeOptions::CAPABILITY, $options - // ) - // ); - } -} diff --git a/tests/Feature/Auth/AuthTest.php b/tests/Feature/Auth/AuthTest.php deleted file mode 100644 index cacb2e6e7..000000000 --- a/tests/Feature/Auth/AuthTest.php +++ /dev/null @@ -1,124 +0,0 @@ - 'http://prashant.com']); - - $response = $this->get('/customer/login'); - - $response->assertSuccessful(); - - $response->assertViewIs('shop::customers.session.index'); - } - - public function testCustomerResgistrationPage() - { - config(['app.url' => 'http://prashant.com']); - - $response = $this->get('/customer/register'); - - $response->assertSuccessful(); - - $response->assertViewIs('shop::customers.signup.index'); - } - - public function testCustomerRegistration() { - $faker = \Faker\Factory::create(); - - $allCustomers = array(); - - $customers = app(Customer::class); - - $created = $customers->create([ - 'first_name' => explode(' ', $faker->name)[0], - 'last_name' => explode(' ', $faker->name)[0], - 'channel_id' => core()->getCurrentChannel()->id, - 'gender' => $faker->randomElement($array = array ('Male','Female', 'Other')), - 'date_of_birth' => $faker->date($format = 'Y-m-d', $max = 'now'), - 'email' => $faker->email, - 'password' => bcrypt('12345678'), - 'is_verified' => 1 - ]); - - $this->assertEquals($created->id, $created->id); - } - - public function testCustomerLogin() - { - config(['app.url' => 'http://prashant.com']); - - $customers = app(Customer::class); - $customer = $customers->findOneByField('email', 'john@doe.net'); - - $response = $this->post('/customer/login', [ - 'email' => $customer->email, - 'password' => '12345678' - ]); - - $response->assertRedirect('/customer/account/profile'); - } - - /** - * Test that customer cannot login with the wrong credentials. - */ - public function willNotLoginWithWrongCredentials() - { - $customers = app(Customer::class); - $customer = $customers->findOneByField('email', 'john@doe.net'); - - $response = $this->from(route('login'))->post(route('customer.session.create'), - [ - 'email' => $customer->email, - 'password' => 'wrongpassword3428903mlndvsnljkvsd', - ]); - - $this->assertGuest(); - } - - /** - * Test to confirm that customer cannot login if user does not exist. - */ - public function willNotLoginWithNonexistingCustomer() - { - $response = $this->post(route('customer.session.create'), [ - 'email' => 'fiaiia9q2943jklq34h203qtb3o2@something.com', - 'password' => 'wrong-password', - ]); - - $this->assertGuest(); - } - - /** - * To test that customer can logout - */ - public function allowsCustomerToLogout() - { - $customer = auth()->guard('customer')->user(); - - $this->get(route('customer.session.destroy')); - - $this->assertGuest(); - } -} diff --git a/tests/Feature/GeneralTest.php b/tests/Feature/GeneralTest.php deleted file mode 100644 index 55cbc0bde..000000000 --- a/tests/Feature/GeneralTest.php +++ /dev/null @@ -1,88 +0,0 @@ - 'http://prashant.com']); - - $response = $this->get('/'); - - $response->assertStatus(200); - } - - /** - * Test for customer login - * - * @return void - */ - public function testCustomerLoginPage() - { - config(['app.url' => 'http://prashant.com']); - - $response = $this->get('/customer/login'); - - $response->assertStatus(200); - } - - /** - * Test for categories page - * - * @return void - */ - public function testCategoriesPage() - { - $categoryUrlSlug = 'marvel-figurines'; - - config(['app.url' => 'http://prashant.com']); - - $response = $this->get("/categories/{$categoryUrlSlug}"); - - $response->assertStatus(200); - } - - /** - * Test for customer registration page - * - * @return void - */ - public function testCustomerRegistrationPage() - { - // config(['app.url' => 'http://127.0.0.1:8000']); - - $response = $this->get("/customer/register"); - - $response->assertStatus(200); - } - - /** - * Test for checkout's cart page - * - * @return void - */ - public function testCartPage() - { - // config(['app.url' => 'http://127.0.0.1:8000']); - - $response = $this->get("/checkout/cart"); - - $response->assertStatus(200); - } -} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100755 index 2932d4a69..000000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,10 +0,0 @@ -assertTrue(true); - } -} \ No newline at end of file diff --git a/tests/_data/.gitkeep b/tests/_data/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests/_output/.gitignore b/tests/_output/.gitignore new file mode 100644 index 000000000..c96a04f00 --- /dev/null +++ b/tests/_output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php new file mode 100644 index 000000000..95c00ec21 --- /dev/null +++ b/tests/_support/AcceptanceTester.php @@ -0,0 +1,26 @@ +login($I->grabRecord(Admin::class, ['email' => 'admin@example.com'])); + $I->seeAuthentication('admin'); + } + + public function amOnAdminRoute(string $name, array $params = null): void + { + $I = $this; + $I->amOnRoute($name, $params); + $I->seeCurrentRouteIs($name); + + /** @var RouteCollection $routes */ + $routes = Route::getRoutes(); + $middlewares = $routes->getByName($name)->middleware(); + $I->assertContains('admin', $middlewares, 'check that admin middleware is applied'); + } + +} diff --git a/tests/_support/Helper/Acceptance.php b/tests/_support/Helper/Acceptance.php new file mode 100644 index 000000000..65b94215e --- /dev/null +++ b/tests/_support/Helper/Acceptance.php @@ -0,0 +1,10 @@ +faker = Factory::create(); + } + + public function testCreate(FunctionalTester $I) + { + $I->loginAsAdmin(); + $I->amOnAdminRoute('admin.catalog.products.index'); + $I->click(__('admin::app.catalog.products.add-product-btn-title'), '//*[contains(@class, "page-action")]'); + $I->seeCurrentRouteIs('admin.catalog.products.create'); + + $I->click(__('admin::app.catalog.products.save-btn-title'), '//*[contains(@class, "page-action")]'); + $I->seeFormHasErrors(); + + $testSku = $this->faker->uuid; + $I->selectOption('//select[@id="attribute_family_id"]', 'Default'); + $I->fillField('//input[@id="sku"]', $testSku); + $I->click(__('admin::app.catalog.products.save-btn-title'), '//*[contains(@class, "page-action")]'); + $I->dontSeeFormErrors(); + $I->seeCurrentRouteIs('admin.catalog.products.edit'); + $I->seeRecord(Product::class, ['sku' => $testSku]); + + $I->click(__('admin::app.catalog.products.save-btn-title'), '//*[contains(@class, "page-action")]'); + $I->seeFormHasErrors(); + + $testName = $this->faker->name; + $testUrlKey = $testName; + $testDescription = $this->faker->sentence; + $testDescriptionShop = $this->faker->sentence; + $testPrice = $this->faker->randomFloat(2, 1, 100); + $testWeight = $this->faker->numberBetween(1, 20); + $I->fillField('//input[@id="name"]', $testName); + $I->fillField('//input[@id="url_key"]', $testUrlKey); + $I->fillField('//textarea[@id="description"]', $testDescription); + $I->fillField('//textarea[@id="short_description"]', $testDescriptionShop); + $I->fillField('//input[@id="price"]', $testPrice); + $I->fillField('//input[@id="weight"]', $testWeight); + $I->click(__('admin::app.catalog.products.save-btn-title'), '//*[contains(@class, "page-action")]'); + $I->dontSeeFormErrors(); + $I->seeCurrentRouteIs('admin.catalog.products.index'); + $product = $I->grabRecord(Product::class, ['sku' => $testSku]); + $I->seeRecord(ProductFlat::class, [ + 'sku' => $testSku, + 'name' => $testName, + 'description' => $testDescription, + 'short_description' => $testDescriptionShop, + 'url_key' => $testUrlKey, + 'price' => $testPrice, + 'weight' => $testWeight, + 'product_id' => $product->id, + ]); + } +} diff --git a/tests/unit.suite.yml b/tests/unit.suite.yml new file mode 100644 index 000000000..3bc78d9b8 --- /dev/null +++ b/tests/unit.suite.yml @@ -0,0 +1,17 @@ +# Codeception Test Suite Configuration +# +# Suite for unit or integration tests. + +actor: UnitTester +modules: + enabled: + - Asserts + - Filesystem + - \Helper\Unit + - Laravel5: + environment_file: .env.testing + run_database_migrations: true + run_database_seeder: true + database_seeder_class: DatabaseSeeder + + step_decorators: ~ \ No newline at end of file diff --git a/tests/unit/Product/Helpers/ProductTypeCest.php b/tests/unit/Product/Helpers/ProductTypeCest.php new file mode 100644 index 000000000..da3ba9abb --- /dev/null +++ b/tests/unit/Product/Helpers/ProductTypeCest.php @@ -0,0 +1,15 @@ +assertTrue(ProductType::hasVariants('configurable')); + $I->assertFalse(ProductType::hasVariants('simple')); + } +}